Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically detecting imported libraries #88

Open
odashi opened this issue Nov 12, 2022 · 11 comments
Open

Automatically detecting imported libraries #88

odashi opened this issue Nov 12, 2022 · 11 comments
Assignees
Labels
Milestone

Comments

@odashi
Copy link
Collaborator

odashi commented Nov 12, 2022

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.

@ZibingZhang
Copy link
Contributor

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.

@odashi
Copy link
Collaborator Author

odashi commented Dec 11, 2022

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:

@odashi
Copy link
Collaborator Author

odashi commented Dec 11, 2022

Tried to remove common prefixes, observed that only this test failed. It is fine to remove this behavior I guess (I will work)

無題

@odashi
Copy link
Collaborator Author

odashi commented Dec 11, 2022

Maybe this is a weird behavior though:

無題

@ZibingZhang
Copy link
Contributor

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?

@odashi
Copy link
Collaborator Author

odashi commented Dec 11, 2022

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.

@ZibingZhang
Copy link
Contributor

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.

@odashi
Copy link
Collaborator Author

odashi commented Dec 11, 2022

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.

@sisykeccco
Copy link

也许这是一个奇怪的行为:

无题

@sisykeccco
Copy link

How do I generate this formula output?
image
my IDE is pycharm,Where in the IDE should I run code to output such a formula?

@odashi
Copy link
Collaborator Author

odashi commented May 2, 2024

#88 (comment)

Oh this was an unintended sideeffect introduced in recent changes.
For the quick fix, you can use prefixes option to remove unnecessary math:

prefixes

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants