LaTeX printing in ipython qtconsole when running external file

1.9k views Asked by At

I solved an analytic problem using sympy and saved everything in a .py file. When I run the code (which includes init_printing()) with ipython filename.py I get nice unicode output whenever I print within the file with pprint. So far so good.

When I import sympy in ipython qtconsole, I can get nice LaTeX outputs, just as stated in the documentation of sympy. But I get this nice printing only if the interactive console does the prining, i.e.:

Integral(sqrt(1/x), x)

produced a LaTeX image, while

pprint(Integral(sqrt(1/x), x))

does produce unicode output.

When running code from a file with

run filename.py

the only way I see to create output is to use pprint, i.e. I do not get the LaTeX output.

Does anyone see a solution? Thanks alot.

2

There are 2 answers

3
Matt On

As in many case I think there is a confusion between returning an object (that trigger display hook) and displaying it. is is the same difference than

def funp():
    print 1

and

def funr():
    return 1

Both will "show" 1 if executed interactivly, but not in a script. In IPython you can see the difference with the Out[] prompt that appear or not depending of wether it is returned or displayed. I think in you case you need from IPython.display import display_pretty

In[1]: display_pretty(I)
⌠           
⎮     ___   
⎮    ╱ 1    
⎮   ╱  ─  dx
⎮ ╲╱   x    
⌡

or maybe from IPython.display import display_latex

0
Abschiedsstein On

I just want to put the solution that worked for me here. Matt's answer somehow includes it: What I wanted is a call that creates the nice latex printout. The following does the job:

from IPython.display import display
import sympy
sympy.init_printing()

display(sympy.symbols("alpha"))

If this snippet is called e.g. with

%run "filename.py"

in qtconsole or ipython notebook, alpha will be displayed nicely.