latex2sympy2 is a Python library that parses LaTeX mathematical expressions and converts them into equivalent SymPy forms. It leverages ANTLR for parsing and supports a wide range of features including arithmetic, various alphabets, common mathematical functions, calculus operations (limits, derivatives, integrals), linear algebra (matrices, determinants, transposes), and other functions like binomials. The current version is 1.9.1, released in April 2023, and it maintains an active release cadence.
pip install latex2sympy2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to convert a LaTeX mathematical string into a SymPy expression using `latex2sympy`. It also shows how to use `latex2latex` to convert an expression, perform a calculation, and return the result in LaTeX format. Note the usage of raw strings (`r"..."`) for LaTeX input to handle backslashes correctly.
Users should be aware of this behavior and verify the output when dealing with linear algebra expressions that involve these operations. Consider using SymPy's direct functions if exact preservation of the original expression's form is critical before calculation.
For derivatives, consistently use `\mathrm{d}` for the differential operator (e.g., `\frac{\mathrm{d}}{\mathrm{d}x}(x^2)`) to ensure unambiguous parsing and avoid potential misinterpretations by the ANTLR grammar.Downgrade your Python version to 3.12 or below, or install a version of `antlr4-python3-runtime` that is compatible with Python 3.13 (e.g., `pip install antlr4-python3-runtime>=4.9.2`).
Iterate over the list returned by `latex2sympy` if the input LaTeX might produce multiple results, or access the desired element (e.g., `expression[0].free_symbols`) if only one result is expected.
Ensure you are using `from latex2sympy2 import latex2sympy` and install the correct package using `pip install latex2sympy2`.
Review the LaTeX input string for any non-standard or syntactically incorrect mathematical notation. Ensure the LaTeX strictly adheres to a form that represents a single, well-defined mathematical expression for SymPy conversion.