{"id":5292,"library":"latex2sympy2","title":"latex2sympy2","description":"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.","status":"active","version":"1.9.1","language":"en","source_language":"en","source_url":"https://github.com/OrangeX4/latex2sympy","tags":["latex","sympy","parser","mathematics","conversion","antlr","calculator","linear algebra","calculus"],"install":[{"cmd":"pip install latex2sympy2","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for converting LaTeX expressions into SymPy objects.","package":"sympy","optional":false},{"reason":"Required for parsing LaTeX expressions using ANTLR.","package":"antlr4-python3-runtime","optional":false}],"imports":[{"symbol":"latex2sympy","correct":"from latex2sympy2 import latex2sympy"},{"symbol":"latex2latex","correct":"from latex2sympy2 import latex2latex"}],"quickstart":{"code":"from latex2sympy2 import latex2sympy, latex2latex\n\n# Convert a LaTeX expression to SymPy\ntex_expression = r\"\\frac{d}{dx}(x^{2}+x)\"\nsympy_expression = latex2sympy(tex_expression)\nprint(f\"LaTeX: {tex_expression}\\nSymPy: {sympy_expression}\")\n\n# Convert a LaTeX expression, calculate it, and convert back to LaTeX\ncalculated_latex = latex2latex(tex_expression)\nprint(f\"Calculated LaTeX: {calculated_latex}\")\n\n# Example with a simple algebraic expression\ntex_algebra = r\"x + y = 1\"\nsympy_algebra = latex2sympy(tex_algebra)\nprint(f\"LaTeX: {tex_algebra}\\nSymPy: {sympy_algebra}\")","lang":"python","description":"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."},"warnings":[{"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.","message":"latex2sympy2 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}