latexify_py
                                
                                 latexify_py copied to clipboard
                                
                                    latexify_py copied to clipboard
                            
                            
                            
                        Automatically detecting imported libraries
Ref: #86
Sometimes users import libraries with custom names, and the current implementation of latexify does not recognize it without passing the custom config (this is not the current feature: tracked by #87)
We can anyway list up the all imported modules through globals(), and check if they contains some well known libraries. For example, if the user invoked import numpy as np, globals()["np"] should point to a module with __name__ == "numpy".
This is clever enough I think, but applying this involves environment-dependent behavior. I think this can be implemented as an additional NodeTransformer with a boolean switch which is turned off by default.
Once this happens, I think we should get rid of _COMMON_PREFIXES. The trimming of prefixes should be by default turned off for all imo.
Once this happens, I think we should get rid of _COMMON_PREFIXES
Though prefixes trimming is a weird behavior, it couldn't be completely replaced by this feature. We couldn't expect that appropriate libraries are imported when @latexify.function is applied, because function can be defined with undefined symbols.
Btw, I guessed we already don't need prefix trimming of especially math, numpy and np because we have attribute-agnostic way to extract function names.
https://github.com/google/latexify_py/blob/ddcbed9cb2105c67b5820d079038dd220559f1b9/src/latexify/ast_utils.py#L146
Tried to remove common prefixes, observed that only this test failed. It is fine to remove this behavior I guess (I will work)
 
                                    
                                    
                                    
                                
Maybe this is a weird behavior though:
 
                                    
                                    
                                    
                                
Though prefixes trimming is a weird behavior, it couldn't be completely replaced by this feature. We couldn't expect that appropriate libraries are imported when
@latexify.functionis applied, because function can be defined with undefined symbols.
Yeah we should definitely keep the prefix trimming feature, just make the user specify which prefixes instead of defaulting to a hardcoded list.
Maybe this is a weird behavior though
That's definitely odd haha. Should maybe only convert in visit_Name and not visit_Attribute?
Should maybe only convert in visit_Name and not visit_Attribute?
We eventually need both: visit_Name for pi (from math import pi) and visit_Attribute for math.pi. But I think this is somewhat too much and may result in unexpected conversion. The same problem may happen in visit_Call though.
As a workaround, users also can get rid of the math prefix manually by specifying prefixes={"math"}, but this is not friendly I guess.
I don't think math.pi -> $\mathrm{math}.\pi$ should be correct behavior, i.e. should not require manual user solution. Maybe we have to disallow certain conversions in specific contexts, e.g. in the visit_Attribute context, we can't allow conversions to math symbols.
This is somewhat weird, but should be fine. The difference between math.pi and pi is only the existence of the prefix and it is better to convert their suffixes to the same expression without any other conditioning. We can simply say that use_math_symbols converts everything in the syntax into math symbols.
也许这是一个奇怪的行为:

How do I generate this formula output?
my IDE is pycharm,Where in the IDE should I run code to output such a formula?
https://github.com/google/latexify_py/issues/88#issuecomment-2073879692
Oh this was an unintended sideeffect introduced in recent changes.
For the quick fix, you can use prefixes option to remove unnecessary math:
How do I generate this formula output?
As this library uses IPython's display functionality, you should use some notebook extensions with LaTeX support (e.g., jupyter) to show appropriate outputs.