latexify-py Documentation

repository·main·Indexed 27 days ago

https://github.com/google/latexify_py

A Python package that converts Python source code or Abstract Syntax Trees (AST) into LaTeX mathematical expressions. It provides decorators like @latexify.function and @latexify.expression for pretty-printing in environments like Jupyter, as well as the latexify.get_latex function for direct string retrieval. Supports Python versions 3.9 to 3.13.

Tokens
1.1K
Snippets
6
Records
10
Agent score
43%

What's inside latexify-py

  1. Install and use latexify-py

    main

    latexify is a Python package that compiles fragments of Python source code into corresponding $\LaTeX$ expressions. It provides libraries to compile Python source code or AST to $\LaTeX$, as well as IPython classes for pretty-printing compiled functions.

    Supported Python versions include 3.9 to 3.13.

    For detailed usage patterns and various use-cases, refer to the example notebook.

  2. Use set operators with `use_set_symbols`

    main

    Set use_set_symbols=True to use LaTeX binary operators for set operations (e.g., converting & to $\cap$, | to $\cup$, - to $\setminus$, etc.) instead of standard Python operators.

    @latexify.function(use_set_symbols=True)
    def f(x, y):
        return x & y, x | y, x - y, x ^ y, x < y, x <= y, x > y, x >= y
  3. Compose assignments using `reduce_assignments`

    main

    Set reduce_assignments=True to compose all variables defined via assignment statements before the return statement into a single mathematical expression.

    Note: The current version of latexify only recognizes assignment statements. Functions using other control flows may raise errors.

    @latexify.function(reduce_assignments=True)
    def f(a, b, c):
        discriminant = b**2 - 4 * a * c
        numerator = -b + math.sqrt(discriminant)
        denominator = 2 * a
        return numerator / denominator
  4. Toggle function signature with `use_signature`

    main

    Control whether the output includes the function signature (e.g., $f(a, b, c) = ...$) or just the expression.

    • In @latexify.function, the default is True.
    • In @latexify.expression, the default is False.
    @latexify.function(use_signature=False)
    def f(a, b, c):
        return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)
  5. Rename identifiers using `identifiers` parameter

    main

    Use the identifiers parameter in @latexify.function to provide a dictionary mapping Python names to desired LaTeX symbols. The keys should be the original Python identifiers (strings), and the values should be the replacement LaTeX strings.

    identifiers = {
        "my_function": "f",
        "my_inner_function": "g",
        "my_argument": "x",
    }
    
    @latexify.function(identifiers=identifiers)
    def my_function(my_argument):
        return my_inner_function(my_argument)
  6. Convert variable names to math symbols with `use_math_symbols`

    main

    Set use_math_symbols=True to automatically convert Python variable names that correspond to symbol names (e.g., alpha, beta, Omega) into their LaTeX symbol equivalents (e.g., $\alpha$, $\beta$, $\Omega$).

    @latexify.function(use_math_symbols=True)
    def greek(alpha, beta, gamma, Omega):
      return alpha * beta + math.gamma(gamma) + Omega
  7. Use @latexify.function to pretty-print functions as LaTeX

    main
    The @latexify.function decorator wraps your Python functions so that they are displayed as LaTeX formulas in environments like Jupyter. The wrapped function remains fully functional and behaves exactly like the original function when called. To get the raw LaTeX source string, apply str() to the wrapped function.