2

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 Answers2

3

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.

0

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

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Matt
  • 27,170
  • 6
  • 80
  • 74
  • Thanks for the answer! This means that the display hook method that is called in the qtconsole is able to do the LaTeX output, but I use a different method (pprint) in the script. How can I manually call the display hook method. Btw. alpha prints LaTeX display_latex(alpha) prints unicode – Abschiedsstein Dec 11 '13 at 11:12
  • Simply use in the script display: IPython.display import display display(alpha) #or similar – Abschiedsstein Dec 11 '13 at 12:27
  • display_something will call the display-hook-something for you. It is not always easy nor make sense to try to get the methods that the display_hook will call by yourself. – Matt Dec 11 '13 at 14:31