Latex -> Sympy

588 views
Skip to first unread message

Vincent MAILLE

unread,
Nov 11, 2011, 12:52:18 PM11/11/11
to sympy
Hi,

I found the latax command to convert a Sympy object to LaTeX format,
but, I’m looking for convert a LaTeX expression to Sympy format, is it
possible?

Thanks.

Vincent

P.S : is there a French community of Sympy users ?

Ronan Lamy

unread,
Nov 11, 2011, 1:33:22 PM11/11/11
to sy...@googlegroups.com
Le vendredi 11 novembre 2011 à 09:52 -0800, Vincent MAILLE a écrit :
> Hi,
>
> I found the latax command to convert a Sympy object to LaTeX format,
> but, I’m looking for convert a LaTeX expression to Sympy format, is it
> possible?

No, there is no parser for LaTeX yet. Reusing code from mathtex or
plastex would be a start, but I guess that the difficult part is to
rebuild a mathematical expression tree from the printing representation.


>
> Thanks.
>
> Vincent
>
> P.S : is there a French community of Sympy users ?
>

No, there isn't. ISTR the same question being asked before, maybe we
should create a sympy-fr mailing-list?


Aaron Meurer

unread,
Nov 11, 2011, 2:54:31 PM11/11/11
to sy...@googlegroups.com
On Fri, Nov 11, 2011 at 11:33 AM, Ronan Lamy <ronan...@gmail.com> wrote:
> Le vendredi 11 novembre 2011 à 09:52 -0800, Vincent MAILLE a écrit :
>> Hi,
>>
>>       I found the latax command to convert a Sympy object to LaTeX format,
>> but, I’m looking for convert a LaTeX expression to Sympy format, is it
>> possible?
>
> No, there is no parser for LaTeX yet. Reusing code from mathtex or
> plastex would be a start, but I guess that the difficult part is to
> rebuild a mathematical expression tree from the printing representation.

See http://code.google.com/p/sympy/issues/detail?id=2319.

Aaron Meurer

>>
>> Thanks.
>>
>> Vincent
>>
>> P.S : is there a French  community of Sympy users ?
>>
> No, there isn't. ISTR the same question being asked before, maybe we
> should create a sympy-fr mailing-list?
>
>

> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>
>

Joachim Durchholz

unread,
Nov 11, 2011, 3:19:56 PM11/11/11
to sy...@googlegroups.com
Am 11.11.2011 18:52, schrieb Vincent MAILLE:
> I found the latax command to convert a Sympy object to LaTeX format,
> but, I�m looking for convert a LaTeX expression to Sympy format, is it
> possible?

TeX is Turing-complete, so this is not possible in general.

Worse, even if you try a heuristic approach (like what Roman proposed),
you'd never be sure that the result is correct. So you'd need to to a
manual check after conversion, nullifying most (maybe all) of the
advantages of an automatic conversion.

To judge whether these problems are showstoppers or just mild
annoyances, you'd probably need somebody with high wizardry skills in
mathtex/plastex/whatever.
I guess that's why nobody has volunteered for such a project yet. (I
wouldn't. I once did some TeX stuff and concluded that TeX's macro
syntax and semantics are so horrid I never want to do anything with it.)

Regards,
Jo

Aaron Meurer

unread,
Nov 11, 2011, 6:11:53 PM11/11/11
to sy...@googlegroups.com
The problem isn't to convert TeX into SymPy, but just LaTeX math,
which would probably realistically not include any fancy macros or
anything that might make the grammar to complicated.

The real problems, from what I see, are:

- LaTeX does not have a well-defined grammar.
- LaTeX allows for a lot of nice shortcuts (this may be more of an
annoyance than a problem)
- The same thing might be entered in different ways by different
people. As a simple example, someone might use \cos, \operator{cos},
or \mathrm{cos} (the second two might be produced from a LaTeX
printer).
- LaTeX lets you print things that are mathematically nonsensical
(like e.g., \frac{\lim{\sum \to \int}}{+}).

Because of these (especially the last item), I think that any function
that does this would end up being more of a heuristic than a complete
algorithm to convert LaTeX to a mathematical expression tree. That
doesn't mean that it wouldn't be worthwhile to implement, however.

Aaron Meurer

On Fri, Nov 11, 2011 at 1:19 PM, Joachim Durchholz <j...@durchholz.org> wrote:
> Am 11.11.2011 18:52, schrieb Vincent MAILLE:
>>
>>        I found the latax command to convert a Sympy object to LaTeX
>> format,

>> but, I’m looking for convert a LaTeX expression to Sympy format, is it

Vincent MAILLE

unread,
Nov 13, 2011, 6:25:51 AM11/13/11
to sympy
OK, thanks for your answers. It's true that LaTeX is complexe to
parse. My question was juste for very simple expressions :

\frac{..}{..} => (...)/(...)
2x+x^2 => 2*x+x**2
...

Vincent

Ondřej Čertík

unread,
Nov 15, 2011, 10:25:14 AM11/15/11
to sy...@googlegroups.com
On Sun, Nov 13, 2011 at 3:25 AM, Vincent MAILLE <htcv...@gmail.com> wrote:
> OK, thanks for your answers. It's true that LaTeX is complexe to
> parse. My question was juste for very simple expressions :
>
> \frac{..}{..} => (...)/(...)
> 2x+x^2 => 2*x+x**2

I think it's definitely doable, and someone just has to do it for
simple expressions and then (if there is interest) it can be improved
for more complex ones. It will never be able to parse 100% of all
latex, but I can imagine it to work just fine for normal math that
people use.

Ondrej

Aaron Meurer

unread,
Nov 15, 2011, 12:25:05 PM11/15/11
to sy...@googlegroups.com
Does anyone know of a good framework to implement this in, so that it
is easily extensible (something like the printer, only backwards)?
Obviously, we can implement the simple cases like \frac{}{} and ^
using regular expressions, but it would be nice to have a more
powerful (heuristic) parser in place, so that it's easy to extend to
do many more things.

Nonetheless, it would be good to have a module that starts even with
simple regular expressions, because then we will start to see what
needs to be done, and what the real limitations of it are.

Aaron Meurer

2011/11/15 Ondřej Čertík <ondrej...@gmail.com>:

krastano...@gmail.com

unread,
Nov 15, 2011, 12:44:10 PM11/15/11
to sy...@googlegroups.com
On 15 November 2011 18:25, Aaron Meurer <asme...@gmail.com> wrote:
Does anyone know of a good framework to implement this in, so that it
is easily extensible (something like the printer, only backwards)?
Obviously, we can implement the simple cases like \frac{}{} and ^
Actually even the \frac{}{} stuff can not be done with only regular expressions. They can't match parenthesis in nested expressions.

Alexey U. Gudchenko

unread,
Nov 15, 2011, 12:54:44 PM11/15/11
to sy...@googlegroups.com
15.11.2011 21:25, Aaron Meurer пишет:

> Does anyone know of a good framework to implement this in, so that it
> is easily extensible (something like the printer, only backwards)?
> Obviously, we can implement the simple cases like \frac{}{} and ^
> using regular expressions, but it would be nice to have a more
> powerful (heuristic) parser in place, so that it's easy to extend to
> do many more things.
>
> Nonetheless, it would be good to have a module that starts even with
> simple regular expressions, because then we will start to see what
> needs to be done, and what the real limitations of it are.
>
> Aaron Meurer

We can try to use first the Latex-->MathML converter (a few
third-party's examples exists I hope), to avoide parsing *.tex (which
can be used complex nested instructions) and only then translate MathML
to SymPy expressions.


>
> 2011/11/15 Ondřej Čertík <ondrej...@gmail.com>:
>> On Sun, Nov 13, 2011 at 3:25 AM, Vincent MAILLE <htcv...@gmail.com> wrote:
>>> OK, thanks for your answers. It's true that LaTeX is complexe to
>>> parse. My question was juste for very simple expressions :
>>>
>>> \frac{..}{..} => (...)/(...)
>>> 2x+x^2 => 2*x+x**2
>>
>> I think it's definitely doable, and someone just has to do it for
>> simple expressions and then (if there is interest) it can be improved
>> for more complex ones. It will never be able to parse 100% of all
>> latex, but I can imagine it to work just fine for normal math that
>> people use.
>>
>> Ondrej
>>
>> --
>> You received this message because you are subscribed to the Google Groups "sympy" group.
>> To post to this group, send email to sy...@googlegroups.com.
>> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
>> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>>
>>
>


--
Alexey U.

Nicolas Jimenez

unread,
Jan 18, 2016, 12:44:42 AM1/18/16
to sympy
A Latex -> Sympy converter: https://github.com/augustt198/latex2sympy

Francesco Bonazzi

unread,
Feb 8, 2016, 7:24:46 AM2/8/16
to sympy


On Monday, 18 January 2016 06:44:42 UTC+1, Nicolas Jimenez wrote:
A Latex -> Sympy converter: https://github.com/augustt198/latex2sympy

Nice, I found out a parser for Mathematica expressions, also written with ANTLR:
https://github.com/rljacobson/FoxySheep
Reply all
Reply to author
Forward
0 new messages