I want to parse LaTeX formulas and directly use them as SymPy expressions. In other words, what I need is something similar to sympify:
from sympy import sympify
f = sympify('x^2 + sin(y) + 1/2')
print f
but that can take LaTeX expressions (strings) as input, for example:
f = latex_sympify('\frac{x}{1+x}')
Given that sympify is able to convert a string with a regular mathematical expression into a SymPy object, if there is anything that can convert LaTeX into a regular mathematical expression, I guess that would do the trick---but I would prefer to do everything within Python.
Any suggestions will be much appreciated.
Take a look at this Latex -> Sympy converter, it works great for a big subset of Latex:
https://github.com/augustt198/latex2sympy
I would look at tools that convert latex to mathml, such as (via google search): http://www.mathtowebonline.com/
Then you can process the XML that comes out of this. However, you should be careful interpreting the result as a mathematical expression. Such tools may guarantee the textual representation is equal, but the bracketing (grouping) of operands with their operators may not be what you expect. In general you will see less nesting. Operators that are just next to each other that we interpret by convention as being nested will simply be converted to sibling elements in the XML format. If you try to interpret this as formulas there is still some work left to be done (recovering the nested structure in the right way).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With