google/latexify_py

Automatically detecting imported libraries

odashi opened this issue · 11 comments

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.

def extract_function_name_or_none(node: ast.Call) -> str | None:

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.function is 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.