latex2sympy2

1.9.1 · active · verified Mon Apr 13

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.

Warnings

Install

Imports

Quickstart

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}")

view raw JSON →