Songtech-0912 / Latspy

Run LaTeX expressions as SymPy functions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Latspy

Latspy is a fork of latex2sympy with major alterations. Like latex2sympy, it parses LaTeX math expressions and converts it into the equivalent SymPy form.

Getting started

ANTLR is used to generate the parser. If you don't have it installed, run these commands:

cd /usr/local/lib
wget https://www.antlr.org/download/antlr-4.9.2-complete.jar

Then add these lines to your .zshrc (if you use zsh), .fishrc if you use the fish shell, or .bashrc (if you use bash):

export CLASSPATH=".:/usr/local/lib/antlr-4.9.2-complete.jar:$CLASSPATH"
alias antlr4='java -jar /usr/local/lib/antlr-4.9.2-complete.jar'
alias grun='java org.antlr.v4.gui.TestRig'

Open a new terminal, then clone the repo.

git clone https://github.com/Songtech-0912/Latspy.git && cd Latspy/latspy

Generate the parser with antlr - this is optional but recommended in order to get the most up-to-date parser.

antlr4 PS.g4 -o gen

Then you can begin using it! Verify that the setup process was successful by running python process_latex.py. You should see this as the result:

e**(2 + 45)
e + 5
e + 5
e
Derivative(x, y)*Integral(x**2*y, y)
Derivative(x, y)*5
Derivative(Integral(x**2, x), x)
Derivative(x, y)*Integral(x**2, x)
Eq(x*y + Derivative(x**2, y), 0)
Eq(x*y + Derivative(x**2, y), 2)
Derivative(x**3, y)
x**3 + Derivative(x**3, y)
Integral(x**2, (y, 2, 5*x))
Integral(x**2, (x, 5*x, 2))
Integral(x**2, x)
-6 + 2*(4*5)

Usage

Latspy works with Python 3 like this:

from latex2sympy.process_latex import process_sympy
process_sympy("\\frac{d}{dx} x^{2}")

# Result => "Derivative(x**2, x)"

Examples

LaTeX Generated SymPy
x**3
Derivative(x*Abs(t), x)
Sum(i, (i, 1, n))
Integral(1/t, (t, a, b))
z + 51

Contributing

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

About

Run LaTeX expressions as SymPy functions

License:MIT License


Languages

Language:Python 97.7%Language:ANTLR 2.3%