Saving an equation as a PNG or JPG?

2,622 views
Skip to first unread message

jeanbi...@gmail.com

unread,
Nov 17, 2014, 3:13:56 AM11/17/14
to sy...@googlegroups.com
I'd love to be able to save a sympy LaTeX-ified result as an image file to export into other documents.  In my business I have to create Powerpoint and am trying python-pptx for this.  It allows incorporating images programmatically.  

I've searched the sympy documentation and other sources but surprisingly have not found anything for equations.  I believe that plots can be saved.  I'm sure I've missed something.

Suggestions greatly appreciated.

--- JBB

Joachim Durchholz

unread,
Nov 17, 2014, 6:14:14 AM11/17/14
to sy...@googlegroups.com
Am 17.11.2014 um 09:13 schrieb jeanbi...@gmail.com:
> I've searched the sympy documentation and other sources but surprisingly
> have not found anything for equations.

You can always take a screenshot.
Well, a windowshot :-)

> I believe that plots can be saved.
> I'm sure I've missed something.

Can't help with that, sorry.

> Suggestions greatly appreciated.

Not the answer you were asking for, but maybe worth mentioning anyway:
Avoid JPG, it cannot deal very well with crisp borders (that's by design).
PNG compresses plots just as well as JPG and is lossless.

It *might* be worth trying different PNG compressor programs. PNG offers
different codecs and different ways to combine them, but does not guide
the programmer in codec and combination choice, so different compression
programs might give you different results. The one guarantee that you
have is that regardless of codec use, the decompressed image will be
pixel-identical to the original.

Alan Bromborsky

unread,
Nov 17, 2014, 7:51:11 AM11/17/14
to sy...@googlegroups.com
If you would send me an example of your code I might be able to suggest a solution.
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/7cd0ae38-1097-4f7b-b3f8-19780965a217%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christophe Bal

unread,
Nov 17, 2014, 7:55:51 AM11/17/14
to sympy-list

Hello.

Have you considered a text in a matplotlib graph ?

Dario Beraldi

unread,
Nov 17, 2014, 9:23:30 AM11/17/14
to sy...@googlegroups.com

Google'ing around I found latexmath2png (A versatile program and Python module to allow conversion of LaTeX math equations in to PNG images) which seems to do what you are after? (I don't anything about it, just found it right now).

Hope it helps!


Alan Bromborsky

unread,
Nov 17, 2014, 10:19:54 AM11/17/14
to sy...@googlegroups.com
Do this -

from sympy import *
from sympy.galgebra import xdvi,Format

Format()

"""
your code with print statements
"""

xdvi(paper='letter')


pdf file with name of python file .pdf will be generated
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.

Jason Moore

unread,
Nov 17, 2014, 10:22:01 AM11/17/14
to sy...@googlegroups.com
There should be some functions in the ipython machinery in sympy that creates pngs from the latex printer using matplotlib. See:

https://github.com/sympy/sympy/blob/master/sympy/interactive/printing.py#L34

Aaron Meurer

unread,
Nov 17, 2014, 2:33:06 PM11/17/14
to sy...@googlegroups.com

jeanbi...@gmail.com

unread,
Nov 17, 2014, 4:13:08 PM11/17/14
to sy...@googlegroups.com
Thanks for all the replies:

Aaron Meurer:
I tried preview.  It looks like a LaTeX installation is required and on this particular system I'm on, I am not allowed to install it - same with the other suggested package.  Is there a workaround, perhaps directing the use of MathJax?

Jason Moore:
I looked at the source code you suggested and did some experimentation.  It led me to:
http://stackoverflow.com/questions/24223695/python-spyder-display-symbolic-math

This is tantalizingly close to what I'd like.  It works for some expressions such as the one in the SO example.

from IPython.display import Image, display
from IPython.lib.latextools import latex_to_png
eq = r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'
data = latex_to_png(eq, wrap=True)
display(Image(data=data))

Expressions involving the \left and \right operators return errors:
eq = r'\begin{bmatrix}\frac{1}{2 a} \left(- b + \sqrt{- 4 a c + b^{2}}\right), & - \frac{1}{2 a} \left(b + \sqrt{- 4 a c + b^{2}}\right)\end{bmatrix}'

...
ValueError: 
\begin{bmatrix}\frac{1}{2 a} \left(- b + \sqrt{- 4 a c + b^{2}}\right), & - \frac{1}{2 a} \left(b + \sqrt{- 4 a c + b^{2}}\right)\end{bmatrix}
^
Unknown symbol: \begin (at char 0), (line:1, col:1)

There is another case where \left and \right cause errors without there being a matrix in the expression.

In any case, supposing I can generate the PNG.  Is there a way to save the image programmatically?  (I tab-completed on several of the imported package functions but nothing jumped out at me). 

Thanks,

-- JBB

Aaron Meurer

unread,
Nov 17, 2014, 5:25:00 PM11/17/14
to sy...@googlegroups.com
On Mon, Nov 17, 2014 at 3:13 PM, <jeanbi...@gmail.com> wrote:
Thanks for all the replies:

Aaron Meurer:
I tried preview.  It looks like a LaTeX installation is required and on this particular system I'm on, I am not allowed to install it - same with the other suggested package.  Is there a workaround, perhaps directing the use of MathJax?

A little Googling reveals https://github.com/agrbin/svgtex, which uses MathJax to generate an SVG (there are plenty of tools you can then use to convert that to PNG).

Also https://github.com/jmorais/mathjax-to-png, although I'm not really clear how that works.


Jason Moore:
I looked at the source code you suggested and did some experimentation.  It led me to:
http://stackoverflow.com/questions/24223695/python-spyder-display-symbolic-math

This is tantalizingly close to what I'd like.  It works for some expressions such as the one in the SO example.

from IPython.display import Image, display
from IPython.lib.latextools import latex_to_png
eq = r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'
data = latex_to_png(eq, wrap=True)
display(Image(data=data))

Expressions involving the \left and \right operators return errors:
eq = r'\begin{bmatrix}\frac{1}{2 a} \left(- b + \sqrt{- 4 a c + b^{2}}\right), & - \frac{1}{2 a} \left(b + \sqrt{- 4 a c + b^{2}}\right)\end{bmatrix}'

...
ValueError: 
\begin{bmatrix}\frac{1}{2 a} \left(- b + \sqrt{- 4 a c + b^{2}}\right), & - \frac{1}{2 a} \left(b + \sqrt{- 4 a c + b^{2}}\right)\end{bmatrix}
^
Unknown symbol: \begin (at char 0), (line:1, col:1)

There is another case where \left and \right cause errors without there being a matrix in the expression.

I guess this means it is using matplotlib under the covers, and it doesn't support matrices.

Aaron Meurer 
Reply all
Reply to author
Forward
0 new messages