augustt198 / latex2sympy Goto Github PK
View Code? Open in Web Editor NEWParse LaTeX math expressions
License: MIT License
Parse LaTeX math expressions
License: MIT License
I can't install the package on Mac OS (error : antlr4: command not found)
I've installed antlr4 but not working (error :
Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/v4/Tool
Caused by: java.lang.ClassNotFoundException: org.antlr.v4.Tool
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
)
Thank you for your help
I am having trouble running the tests in the test.py file. I have tried:
$python2 test.py
Traceback (most recent call last):
File "test.py", line 64, in <module>
("f(x)", f(x)),
TypeError: 'Symbol' object is not callable
Does this mean that the tests should be ran in some other environment?
process_sympy("(-1) * 2")
and process_sympy("-2")
return the same result Mul(-1, 2)
. process_sympy("-2")
should return -2 instead of Mul(-1, 2)
.
process_sympy("-2/3")
returns Mul(Mul(-1, 2), Pow(3, -1))
instead of Rational(-2, 3)
.
When parsing back the obtained latex on a sympy Integral object to process_sympy(), it throws an exception. Adding code snippet and error log. Occurs on python 2.7 and 3.
The error occurs due to the extra "," being generated and if I add this line
latex_expr = latex_expr.replace("\,", "")
before passing latex_expr back to process_sympy() it seems to be working fine.
from sympy import *
from sympy.abc import *
from process_latex import process_sympy
expr = Integral((x+1)**2, x)
latex_expr = latex(expr)
back_to_expr = process_sympy(latex_expr)
Exception: I don't understand this
\int \left(x + 1\right)^{2}, dx
----
eg 4,3 + 4,5
Hello,
I hope this is something I'm missing but the process_latex.py file is importing from the gen folder which is empty. Is there I way I can access the functionality of this package?
Thanks,
Jake Noble
I could not find a package on PyPI for this module. Can you create one?
Please add a requirement.txt for basic running.
If I run:
print(process_sympy("B_{\\nu}"))
I get:
B
Can support be added for outputting B_{\\nu}
?
Awesome script BTW!
Hello, thank you for your lib!
Can you please add required python version for your readme. I have had a hard time with python 3.5 and syntax error in lib, python 2.7 (via anaconda) works perfectly.
pm is used to show plus-minus, is yet not implemented into sympy. #5305
Example latex: \frac{-b\pm\sqrt{b^2-4ac}}{2a}
when converted into sympy using latex2sympy, I get
-b*pm*sqrt(-4*a*c + b**2)/(2*a)
So, the undefined pm simply gets multiplied. Hence getting wrong expression.
It should have probably shown an error.
In fact, any undefined thing simply gets multiplied
Ex: -b*pm*blabla*(-4*a*c + b**2)/(2*a)
Table columns are wrongly written (Image |Generated SymPy)
If a product or sum has bounds that consist of only single digits, an error is thrown. Here's an example:
\\sum_{n=0}^9n
Which is valid LaTeX (I don't think it should be...) and would result in: http://latex.codecogs.com/gif.latex?%5Csum_%7Bn%3D0%7D%5E4n
I think the problem originates from not having a space between supexpr
and the function argument. I cannot seem to get this to work either:
\\sum_{n=0}^9 n
Thanks,
John
x^b is a valid Latex expression but process_sympy() requires x^{b} for conversion to sympy.
How to work with python 3?
Hello, I have noticed a few features that are not implemented yet. I have solved most of them by pre-processing input from MathQuill. I use this code:
string.replace('\\right', '') # right tag .replace('\\left', '') # left tag .replace("^x","^{(x)}") # powers .replace("\ ","") #Spaces
However, one thing I couldn't solve without changing latex2sympy code is when a user inputs a function raised to a power (say sin^nx instead of (sinx)^n) latex2sympy crashes and gives NoneType' object has no attribute 'additive'
.
Also, I believe hyperbolic trig functions are not supported yet (might be a very simple addition)
Can you please correct this? Thank you in advance!
2x
in latex format should be convert to 2*x
in sympy format, however, latex2sympy converted it to 2 x
Your software solves a longstanding issue in SymPy:
and is something that we'd really like to be included in SymPy.
Would you be interested in adding this to SymPy instead of having a standalone package? It'd certainly increase the usage and visibility.
当使用print(process_sympy("2\frac{3}{5}"))时,输出为:2*(3/5)
但实际上这是一个假分数,结果应该是2+3/5
This latex gives the following stack trace.
A = \left( \begin{array} { l l l } { 2} & { 7} & { 3} \\ { 3} & { 5} & { 2} \\ { 9} & { 4} & { 1} \end{array} \right)
Traceback (most recent call last):
File "test - Copy.py", line 17, in <module>
out = process_sympy(json.loads(r.text)['latex'])
File "E:\Dropbox\Projects\mathpix\latex2sympy\process_latex.py", line 29, in process_sympy
relation = parser.math().relation()
File "E:\Dropbox\Projects\mathpix\latex2sympy\gen\PSParser.py", line 391, in math
self.relation(0)
File "E:\Dropbox\Projects\mathpix\latex2sympy\gen\PSParser.py", line 480, in relation
self.relation(3)
File "E:\Dropbox\Projects\mathpix\latex2sympy\gen\PSParser.py", line 456, in relation
self.expr()
File "E:\Dropbox\Projects\mathpix\latex2sympy\gen\PSParser.py", line 574, in expr
self.additive(0)
File "E:\Dropbox\Projects\mathpix\latex2sympy\gen\PSParser.py", line 630, in additive
self.mp(0)
File "E:\Dropbox\Projects\mathpix\latex2sympy\gen\PSParser.py", line 726, in mp
self.unary()
File "E:\Dropbox\Projects\mathpix\latex2sympy\gen\PSParser.py", line 925, in unary
self.postfix()
File "E:\Dropbox\Projects\mathpix\latex2sympy\gen\PSParser.py", line 1072, in postfix
self.exp(0)
File "E:\Dropbox\Projects\mathpix\latex2sympy\gen\PSParser.py", line 1474, in exp
self.comp()
File "E:\Dropbox\Projects\mathpix\latex2sympy\gen\PSParser.py", line 1691, in comp
la_ = self._interp.adaptivePredict(self._input,20,self._ctx)
File "C:\Python27\lib\site-packages\antlr4\atn\ParserATNSimulator.py", line 337, in adaptivePredict
alt = self.execATN(dfa, s0, input, index, outerContext)
File "C:\Python27\lib\site-packages\antlr4\atn\ParserATNSimulator.py", line 460, in execATN
input.consume()
File "C:\Python27\lib\site-packages\antlr4\BufferedTokenStream.py", line 97, in consume
if self.sync(self.index + 1):
File "C:\Python27\lib\site-packages\antlr4\BufferedTokenStream.py", line 109, in sync
fetched = self.fetch(n)
File "C:\Python27\lib\site-packages\antlr4\BufferedTokenStream.py", line 121, in fetch
t = self.tokenSource.nextToken()
File "C:\Python27\lib\site-packages\antlr4\Lexer.py", line 125, in nextToken
self.notifyListeners(e) # report error
File "C:\Python27\lib\site-packages\antlr4\Lexer.py", line 280, in notifyListeners
listener.syntaxError(self, None, self._tokenStartLine, self._tokenStartColumn, msg, e)
File "C:\Python27\lib\site-packages\antlr4\error\ErrorListener.py", line 60, in syntaxError
delegate.syntaxError(recognizer, offendingSymbol, line, column, msg, e)
File "E:\Dropbox\Projects\mathpix\latex2sympy\process_latex.py", line 58, in syntaxError
raise Exception(err)
Exception: I don't understand this
A = \left( \begin{array} { l l l } { 2} & { 7} & { 3} \\ { 3} & { 5} & { 2} \\ { 9} & { 4} & { 1} \end{array} \right)
Hello,
This is what I get when I try installing latex2sympy on CentOS
[..latex2sympy]# java -jar antlr-4.5.3-complete.jar PS.g4 -o gen
error(56): PS.g4:113:9: reference to undefined rule: exp
error(56): PS.g4:114:16: reference to undefined rule: exp_nofunc
error(56): PS.g4:118:9: reference to undefined rule: eval_at_sup
error(56): PS.g4:118:37: reference to undefined rule: eval_at_sup
it is very difficult with this readme file
Under some installations, vector lengths do not change in response to parameter changes
allow \int_0^{\infty}\sin x dx instead of \int_{0}^{\infty}\sin x dx
@augustt198 There are missing logical operators, e.g "∨" => "\lor", how can I append it ?
Thanks.
The module process_latex.py
imports a module called antlr4
. What is this? Or, to put it another way, where can this be obtained?
I have installed the antlr4
command and used it to populate the gen directory. I have also used pip
to install the antlr4-python2-runtime" package. But I have no idea where the
antlr4` package might be.
How can we use a multi character variable
in latex suntax we use \mathit, and its not yet supported
Is this the only way??
process_sympy("{\frac {\OperationHours}{\TotalPossibleOperationHours}} \times \Mech 100")
Mod
operator has not been handled. Is there any reason why this has been skipped ?
Mod
is a standard operator in sympy supported operators collection.
Hi,
I have been using Latex2SymPy for a while successfully to handle all sorts of LaTeX inputs, but I have been running into troubles with a few functions. For instance:
from latex2sympy.process_latex import process_sympy
from sympy import *
inputLatex = '\\sin{-x}\\sin{-x}'
trigsimp(process_sympy(inputLatex))
Result: sin(x)**2
That works great: trigsimp handled the simplification well. Now, if I try:
inputLatex = '\\sin{-x}'
trigsimp(process_sympy(inputLatex))
Result: sin(-x)
Even though this is obviously correct, I expected trigsimp() to give me -sin(x) as an answer. In fact, if I run trigsimp straight from a sympy expression:
trigsimp(sin(-x))
Result: -sin(x)
This is what I expect. Even running sin(-x) without the trigsimp() command returns me -sin(x). I checked the object type of my process_sympy('\sin{-x}') call, and it is 'sin'.
I thought this might be something related with the way x is transformed into a Symbol type, but I then tried putting pi in the sin function.
inputLatex = '\\sin{\\pi}'
trigsimp(process_sympy(inputLatex))
Result: sin(pi)
If I run straight sin(pi), I get 0 as an answer, with or without trigsimp().
Has anyone bumped into any issue like that before? Any ideas on how to solve_
It seems that adding multiple expressions for \\int
,\\sum
, or \\prod
together will cause an error to occur. For instance:
process_sympy("\\int_{-3}^{4} \\frac{1}{x}dx+\\int_{1.5}^{2} \\ln(x)dx")
Throws:
UnboundLocalError: local variable 'int_var' referenced before assignment
In PS.g4, I found a way to allow summing multiple integrals.
Change:
| FUNC_INT
(subexpr supexpr | supexpr subexpr)?
(additive? DIFFERENTIAL | frac | additive)
To:
| FUNC_INT
(subexpr supexpr | supexpr subexpr)?
(additive? DIFFERENTIAL)
Unfortunately, this doesn't allow differentials to be in fractional form. For example: \\int_{3}^{5} dx/x
Thanks for your time,
John
This parser translates LaTeX into a string to be parsed again by SymPy.
It would be more efficient (and less error-prone) if process_sympy gave a SymPy expression directly.
\frac a b
is valid Latex, but process_sympy("\\frac a b")
crashes with the exception:
Exception: missing '{' at 'a'
\frac a b
Same goes for \sqrt a
.
process_sympy(r"(\sqrt{9}-1)/2") return -1+3/2.
SymPy has a LaTeX printer, the function latex( ) returns the LaTeX representation of the string.
It would be good to add tests, to check that latex2sympy is able to parse back generated LaTeX strings to SymPy's original expression.
Given a SymPy expression, let's say expr, test could look like:
from sympy import latex
assert eval(process_latex(latex(expr))) == expr
This could be used to both:
\int^{3}_{1} x^{2} + \frac{1}{x} dx
gets parsed to:
integrate (x*_(2),(x,1,3))+((1)/(x))_d*x
It seems the integral is not capturing the whole expression up to dx.
Hello,
I have been checking your examples and LaTex and found out, that frac with single slash (\frac) doesn't work. At the same time \ \frac is working.
Meanwhile, \sum, \int_ , \infty, \sqrt, \pi (with single ) are working, but binom needs \ \ (and convert to binom_(k_n), instead of binomial(k,n) in sympy ).
So, what to use? If we should strictly use , add this please to the documentation.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.