question about sympy's parsers

49 views
Skip to first unread message

Duncan Steele

unread,
May 25, 2012, 12:19:24 PM5/25/12
to sympy
Hello, I am new to sympy, and I am really impressed by the web demo at
live.sympy.org. I am trying to replicate that shell's ability to
understand latex maths notation, and I have been unsuccessful. I have
combed through both sympy and sympy-live without understanding how
live.sympy.org parses latex maths.

There seems to be some API function that the web shell calls to parse
the string I type in, e.g. 'X = \sum_i x_i = X '. What is it?

Thanks,

Matthew Rocklin

unread,
May 25, 2012, 12:36:00 PM5/25/12
to sy...@googlegroups.com
Hi Duncan, 

To the best of my knowledge SymPy is unable to parse LaTeX. We are however able to generate it; this is what you're seeing on live.sympy.org

You can look at our latex printing by downloading our source and checking out the sympy/sympy/printing/latex.py file. 

I think it would be awesome to have a latex parser for SymPy. This might be challenging though. 

-Matt


--
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.


Aaron Meurer

unread,
May 25, 2012, 4:29:27 PM5/25/12
to sy...@googlegroups.com
What SymPy Live does is return the LaTeX string of expression using
the LaTeX printer (accessible through the latex() function), and then
passes that to MathJax, which converts it to a printed expression.
Any string output is passed to MathJax, so even if you just enter a
string, it will be parsed as LaTeX.

If you want to know how the LaTeX printer works, see sympy/printing/latex.py.

Aaron Meurer

Thomas Ligon

unread,
Dec 8, 2019, 4:04:07 PM12/8/19
to sympy
Hi Aaron,

this answers a question I had, except that I can't see MathJax called anywhere. A search for mathjax in SymPy gives me 4 occurrences in printing.py and 2 in latex.py, but none that call MathJax. Can/should I search the code of Live?
>> For more options, visit this group at
>> http://groups.google.com/group/sympy?hl=en.
>>
>
> --
> 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

Aaron Meurer

unread,
Dec 8, 2019, 6:22:59 PM12/8/19
to sympy
The SymPy Live source code is at https://github.com/sympy/sympy-live

Aaron Meurer
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/0d02f196-67e0-4acb-a1b3-9f0f300a24bd%40googlegroups.com.

Thomas Ligon

unread,
Dec 9, 2019, 5:55:59 AM12/9/19
to sympy
Thanks, this is helpful.


What SymPy Live does is return the LaTeX string of expression using the LaTeX printer (accessible through the latex() function), and then passes that to MathJax

It looks to me like the tutorial web page, which might be Jupyter, does this.

In the SymPy tutorial, the first examples in
https://docs.sympy.org/latest/tutorial/intro.html
print nicely, but, starting with "The Power of Symbolic Computation" and the example
diff(sin(x)*exp(x), x)
the output is no longer nicely formatted, but looks like "Unicode" formatting. Here, you can also see that the output is no longer centered. In addition, the examples do not include print statements, so it looks like the lines in the examples that don't contain ">>>" were not created by the example itself. When I look at the source of the web page for the tutorial, I can see that it contains URLs for live.sympy.org, and also for MathJax. My conclusion: The use of MathJax is not in the SymPy Live code, but in the code of the tutorial web page.
Finally, it looks to me like what I need is not to understand how SymPy Live formats the output, but how the tutorial formats it. If I need immediate formatting in my project, I should probably use Jupyter notebooks.
In comparison, I have done a lot with MATLAB Symbolic Math Toolbox, and now I am just starting to use Python and SymPy. In MATLAB, I got the best formatting using MATLAB "live scripts", whích are a kind of notebook. However, the output was never good enough for publication, so I used it for calculations and searching for a solution, and the created my publication documents in another system.
> To unsubscribe from this group and stop receiving emails from it, send an email to sy...@googlegroups.com.

Aaron Meurer

unread,
Dec 9, 2019, 1:41:52 PM12/9/19
to sympy
If you're referring to the documentation, that is not SymPy Live,
except for the popup that appears on the bottom right. The docs use
Sphinx (the sources are in the sympy/sympy repo). They use the Sphinx
mathjax extension. MathJax is itself a Javascript library, so it only
exists on the webpage. The code that writes the HTML files just writes
the LaTeX into those files, and MathJax renders it in the browser.

SymPy Live is a website at live.sympy.org, and it is also included in
the docs in the bottom right.

The examples in the docs do not use MathJax, but rather just show the
output directly from Python. That way if you copy and paste the
example in a terminal, the output will look exactly the same. The page
you mentioned shows Unicode output so that it is easier to read. If
you don't have the right fonts installed, it can render poorly. We are
looking for better ways to solve this
(https://github.com/sympy/sympy/issues/15700), but for now, I
recommend installing DejaVu Sans Mono. The only MathJax used in the
tutorial is the math in the text surrounding the examples, which is
not computed from SymPy.

For your own computation, I recommend using the Jupyter notebook using
Jupyter Lab. That will automatically render SymPy output using
MathJax, and it has other nice features as well, like the ones you
described from MATLAB.

Aaron Meurer
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/af299d33-6eba-4c11-b6e1-ad50f8b90df1%40googlegroups.com.

Thomas Ligon

unread,
Dec 10, 2019, 2:47:47 AM12/10/19
to sympy
Thanks again! This was a very useful discussion. At the moment, I am using a simple solution for displaying the TeX and spending my time on the real task using SymPy, and I am very happy with the way it is going.
Reply all
Reply to author
Forward
0 new messages