Skip to main content

Logo

latex2sympy2_extended

About

latex2sympy2_extended parses LaTeX math expressions and converts it into the equivalent SymPy form. The latex2sympy2_extended is adapted from OrangeX4/latex2sympy.

ANTLR 4.13.1 is used to generate the parser.

Features

  • Arithmetic: Add (+), Sub (-), Dot Mul (·), Cross Mul (×), Frac (/), Power (^), Abs (|x|), Sqrt (√), etc...
  • Alphabet: a - z, A - Z, α - ω, Subscript (x_1), Accent Bar(ā), etc...
  • Common Functions: gcd, lcm, floor, ceil, max, min, log, ln, exp, sin, cos, tan, csc, sec, cot, arcsin, sinh, arsinh, etc...
  • Function Symbol: f(x), f(x-1,), g(x,y), etc...
  • Calculous: Limit ($lim_{n\to\infty}$), Derivation ($\frac{d}{dx}(x^2+x)$), Integration ($\int xdx$), etc...
  • Linear Algebra: Matrix, Determinant, Transpose, Inverse, Elementary Transformation, etc...
  • Set: Union (∪), Intersection (∩), etc...
  • Other: Binomial...

NOTICE: It will do some irreversible calculations when converting determinants, transposed matrixes and elementary transformations...

NOTICE: comma separated numbers are only supported in standalone form: 1,233, not in expressions: 1,233x.

Installation

Current version supports 3 runtimes:

  • 4.9.3
  • 4.11.0
  • 4.13.2

Use the following command to install with the runtime you need:

pip install latex2sympy2_extended[antlr4_13_2]

Requirements: sympy and antlr4-python3-runtime packages.

Usage

Basic

In Python:

from latex2sympy2_extended import latex2sympy

tex = r"\frac{d}{dx}(x^{2}+x)"
latex2sympy(tex)
# => "Derivative(x**2 + x, x)"

Examples

LaTeX Converted SymPy Calculated Latex
x^{3} $x^{3}$ x**3 x^{3} $x^{3}$
\frac{d}{dx} tx $\frac{d}{dx}tx$ Derivative(x*t, x) t $t$
\sum_{i = 1}^{n} i $\sum_{i = 1}^{n} i$ Sum(i, (i, 1, n)) \frac{n \left(n + 1\right)}{2} $\frac{n \left(n + 1\right)}{2}$
\int_{a}^{b} \frac{dt}{t} Integral(1/t, (t, a, b)) -\log{(a)} + \log{(b)} $-\log{(a)} + \log{(b)}$
`(2x^3 - x + z) {x=3}` $(2x^3 - x + z)|{x=3}$ z + 51

Eval At

# Before
(x+2)|_{x=y+1}

# After
y + 3

Matrix

Determinant

from latex2sympy2 import latex2sympy

tex = r"\begin{vmatrix} x & 0 & 0 \\ 0 & x & 0 \\ 0 & 0 & x \end{vmatrix}"
latex2sympy(tex)
# => "x^{3}"

Transpose

from latex2sympy2 import latex2sympy

tex = r"\begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{pmatrix}^T"
# Or you can use "\begin{pmatrix}1&2&3\\4&5&6\\7&8&9\end{pmatrix}'"
latex2sympy(tex)
# => "Matrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]])"

Elementary Transformation

from latex2sympy2 import latex2sympy

matrix = r'''
    \begin{pmatrix}
        1 & 2 & 3 \\ 
        4 & 5 & 6 \\
        7 & 8 & 9 \\ 
    \end{pmatrix}
'''

# Scale the row with grammar "\xrightarrow{kr_n}"
tex = matrix + r'\xrightarrow{3r_1}'
latex2sympy(tex)
# => "Matrix([[3, 6, 9], [4, 5, 6], [7, 8, 9]])"

# Swap the cols with grammar "\xrightarrow{c_1<=>c_2}"
# Of course, you can use "\leftrightarrow" to replace "<=>" 
tex = matrix + r'\xrightarrow{c_1<=>c_2}'
latex2sympy(tex)
# => "Matrix([[2, 1, 3], [5, 4, 6], [8, 7, 9]])"

# Scale the second row and add it to the first row
# with grammar "\xrightarrow{r_1+kr_2}"
tex = matrix + r'\xrightarrow{r_1+kr_2}'
latex2sympy(tex)
# => "Matrix([[4*k + 1, 5*k + 2, 6*k + 3], [4, 5, 6], [7, 8, 9]])"

# You can compose the transform with comma ","
# and grammar "\xrightarrow[4r_3]{2r_1, 3r_2}"
# Remember the priority of "{}" is higher than "[]"
tex = matrix + r'\xrightarrow[4r_3]{2r_1, 3r_2}'
latex2sympy(tex)
# => "Matrix([[2, 4, 6], [12, 15, 18], [28, 32, 36]])"

Complex Number Support

from latex2sympy2 import set_real

set_real(False)

Contributing

If you want to add a new grammar, you can fork the code from hynky1999/latex2sympy2_extended.

  • To modify parser grammar, view the existing structure in src/latex2sympy2_extended/PS.g4.
  • To modify the action associated with each grammar, look into src/latex2sympy2_extended/latex2sympy2.py.

Contributors are welcome! Feel free to open a pull request or an issue.

Release files for latex2sympy2-extended 1.11.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for latex2sympy2-extended 1.11.0
File Size Uploaded
latex2sympy2_extended-1.11.0.tar.gz 207.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for latex2sympy2-extended 1.11.0
File Interpreter ABI Platform
latex2sympy2_extended-1.11.0-py3-none-any.whl Python 3 none any Details

Total release size: 416.1 kB

Release files / latex2sympy2_extended-1.11.0.tar.gz

Download URL latex2sympy2_extended-1.11.0.tar.gz
Size 207.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9695657c81b50abba2636638638618db59f4663ed2a4a12d62cef74a40e28fec
BLAKE2b-256 checksum
How to use checksums
3075456da2da05f6380ea96e6ea804ab2c03e41fc3ed80052307fe8efe6ea20e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / latex2sympy2_extended-1.11.0-py3-none-any.whl

Download URL latex2sympy2_extended-1.11.0-py3-none-any.whl
Size 209.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aebb77d52ce269e25028e4bea89ddb14d242ba36bcf7b636496fb5fd9728d234
BLAKE2b-256 checksum
How to use checksums
e961f75cd1fa54d8434276126034aed54dd120747de9a8fa013cdd79545ccbeb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release history Release notifications | RSS feed

This release

1.11.0 This release

2 release files

1.10.1

2 release files

1.0.9

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page