Registry / serialization / latex2sympy2

latex2sympy2

JSON →
library1.9.1pypypi✓ verified 22d ago

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 latex2sympy2
INSTALL
IMPORT
SIG · LATEX2SYMPY2
L
latex2sympy2
serializationpythonv1.9.1
Install
7.3s avg
Import
1486ms
Disk
92MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.9.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 1.228s · 81.5MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 7.3s · import 1.150s · 82MB
92MB installed
● package 92MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

latex2sympy
from latex2sympy2 import latex2sympy
latex2latex
from latex2sympy2 import latex2latex

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.

from latex2sympy2 import latex2sympy, latex2latex # Convert a LaTeX expression to SymPy tex_expression = r"\frac{d}{dx}(x^{2}+x)" sympy_expression = latex2sympy(tex_expression) print(f"LaTeX: {tex_expression}\nSymPy: {sympy_expression}") # Convert a LaTeX expression, calculate it, and convert back to LaTeX calculated_latex = latex2latex(tex_expression) print(f"Calculated LaTeX: {calculated_latex}") # Example with a simple algebraic expression tex_algebra = r"x + y = 1" sympy_algebra = latex2sympy(tex_algebra) print(f"LaTeX: {tex_algebra}\nSymPy: {sympy_algebra}")
Debug
Known issues
gotchalatex2sympy2 performs irreversible calculations when processing certain linear algebra operations such as determinants, transposed matrices, and elementary transformations. The converted SymPy output may not perfectly retain the original structure for these specific LaTeX inputs.
fix
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.
affects: All versions
gotchaThe differential operator 'd' in LaTeX expressions for derivatives might require specific formatting, e.g., `\mathrm{d}` instead of `d`, to be correctly interpreted by the parser. While `d` often works, `\mathrm{d}` can prevent ambiguity and ensure correct parsing in all contexts.
fix
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.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'typing.io'; 'typing' is not a package
This error occurs when `latex2sympy2` (or its dependency `antlr4-python3-runtime`) is used with Python 3.13 or newer, as `typing.io` was removed in Python 3.13.
fix
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`).
AttributeError: 'list' object has no attribute 'free_symbols'
This error happens when `latex2sympy` parses a LaTeX expression that can result in multiple SymPy expressions (like those containing `\pm`), returning a list instead of a single SymPy object, and a method like `.free_symbols` is directly called on this list.
fix
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.
ModuleNotFoundError: No module named 'latex2sympy'
This occurs when trying to import `latex2sympy` which is an older, likely unmaintained package, instead of `latex2sympy2`, or when `latex2sympy2` has not been installed correctly.
fix
Ensure you are using `from latex2sympy2 import latex2sympy` and install the correct package using `pip install latex2sympy2`.
Expected an expression, found 'comma'
This type of error indicates that the LaTeX mathematical expression provided to `latex2sympy2` contains syntax that the parser does not recognize as valid mathematical notation, such as a misplaced comma within an integral or other expression.
fix
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.
Upgrade
Version history
1.9.1latest on PyPI · released Apr 23, 2023
Audit
Dependencies
sympyrequiredRequired for converting LaTeX expressions into SymPy objects.
antlr4-python3-runtimerequiredRequired for parsing LaTeX expressions using ANTLR.
Agent activity
5 hits · last 30 days
node
4
Resources
latex2sympy2 — pip install latex2sympy2 · libregistry