Search code examples
pythonipythonsymbolic-mathspyder

Python Spyder Display Symbolic Math


In Spyder 2 (Anaconda distribution) and in the IPython QT Console I'm able to print results of symbolic calculations (from an answer I got for a previous post) but I can't get equations in strings to print with the a IPython's Rich Display System:

from sympy import *
from IPython.display import display, Math

init_printing(use_unicode=False, wrap_line=False, no_global=True)

x, y, z = symbols('x y z')

#----- prints correctly
ii = integrate(x**2 + x + 1, x) 
display(ii)

#----- does not print
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')

The above prints the results of the integrate correctly but the Math() does not print (no error -- just skips it). Note it all works in SciPy web notebook.

Thank you!


Solution

  • The Math class doesn't generate the rendered image from your Latex, that's why it doesn't work directly.

    To get what you want you need to use this code

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

    Then you will see the right image