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

Some basic questions about this awesome project #75

Closed
moringspeaker opened this issue Nov 3, 2022 · 4 comments
Closed

Some basic questions about this awesome project #75

moringspeaker opened this issue Nov 3, 2022 · 4 comments
Labels
question Further information is requested

Comments

@moringspeaker
Copy link

Hi, I accidentally found this project and it spends me quite a few time to basically figure out how this works and I'm still a little confused about it. Can I just simply summarize this Python package as a "decorator" which is used when we are writing a function in Python to generate an extra formula in Latex format?

@odashi
Copy link
Collaborator

odashi commented Nov 4, 2022

@moringspeaker
Hi, thanks for asking your question!

First of all, the main branch is a bit different from the released version (v0.1.1). I'd like to explain how it works based on the main branch.

The core functionlity of this library is latexify.frontend.get_latex(). This function works as a "compiler" from Python functions to LaTeX with following steps:

  1. Obtains the source code of the function:
    try:
    source = inspect.getsource(fn)
    except Exception:
    # Maybe running on console.
    source = dill.source.getsource(fn)
    # Remove extra indentation so that ast.parse runs correctly.
    source = textwrap.dedent(source)
  2. Obtains the parse tree of the source code:
    tree = ast.parse(source)
  3. Modifies the parse tree according to the config:
    # Applies AST transformations.
    if identifiers is not None:
    tree = transformers.IdentifierReplacer(identifiers).visit(tree)
    if reduce_assignments:
    tree = transformers.AssignmentReducer().visit(tree)
  4. Generates LaTeX code corresponding to the modified parse tree:
    return codegen.FunctionCodegen(
    use_math_symbols=use_math_symbols,
    use_raw_function_name=use_raw_function_name,
    use_signature=use_signature,
    ).visit(tree)

lateixfy.function (formarly latexify.with_latex) is a function that converts the given function to another callable object that works similarly to the original function, but associated with the generated LaTeX code. This object can communicate with Jupyter to show the associated LaTeX expression (MathJax).

As for "decorator", the Python decorator is a syntax sugar to call the function with decorated stuff and replace the stuff with the return value. So the following two code are basically the same:

@latexify.function
def f(x):
    ...
def f(x):
    ...
f = latexify.function(f)

@odashi odashi added the question Further information is requested label Nov 4, 2022
@moringspeaker
Copy link
Author

Thanks for your patient explaination! I spent some time to read this awesome project's source code and I wonder if you guys plan to support some more complicated formats like a matrix or something? It's always annoyed for me to write my own code to transfer a Numpy matrix into Latex format.

@odashi
Copy link
Collaborator

odashi commented Nov 4, 2022

support some more complicated formats like a matrix or something

Yeah sometimes it is requested, and particularly for arrays, I am still thinking of how/what range of expressions should be supported by this library (e.g., 2x2 arrays are small enough to compile, but large arrays and/or arrays with more than 2 dimensions are basically not suitable for LaTeX).

It would be nice to have a specific issue for arrays. Let me create it and continue discussion.

@odashi odashi mentioned this issue Nov 4, 2022
@odashi
Copy link
Collaborator

odashi commented Nov 8, 2022

Delegated to #78.

@odashi odashi closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants