Install latexify-py via pip
mainInstall the library using pip. Note that the package name is latexify-py, not latexify.
$ pip install latexify-pyrepository·main·Indexed 27 days ago
https://github.com/google/latexify_pyA 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.
Install the library using pip. Note that the package name is latexify-py, not latexify.
$ pip install latexify-pylatexify 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.
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 >= ySet 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 / denominatorControl whether the output includes the function signature (e.g., $f(a, b, c) = ...$) or just the expression.
@latexify.function, the default is True.@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)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)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@latexify.expression decorator works similarly to @latexify.function, but it prints only the mathematical expression without the function name or its signature.@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.latexify.get_latex(func) to directly retrieve the LaTeX expression string corresponding to a given function without using decorators. It accepts the same parameters as latexify.function.