How to pretty print in ipython notebook via sympy?
Asked Answered
R

4

61

I tried pprint, print, the former only prints Unicode version, and the latter doesn't do pretty prints.

from sympy import symbols, Function
import sympy.functions as sym
from sympy import init_printing
init_printing(use_latex=True)
from sympy import pprint
from sympy import Symbol

x = Symbol('x')

# If a cell contains only the following, it will render perfectly.
(pi + x)**2

# However I would like to control what to print in a function, 
# so that multiple expressions can be printed from a single notebook cell.
pprint((pi + x)**2)
Ramose answered 7/1, 2014 at 19:20 Comment(0)
O
90

you need to use display:

from IPython.display import display

display(yourobject)

It will choose the appropriate representation (text/LaTex/png...), in recent enough version of IPython (6.0+) display is imported by default, still we recommend to explicitly import it.

Overgrow answered 8/1, 2014 at 10:55 Comment(2)
It should be pointed out that you still need to call init_printing for this to work, as that's what registers the LaTeX display for SymPy objects.Cesspool
Is there a way to limit the width of output? I tried with display(expr, width=1000) without success.Franklynfrankness
C
34

The issue is with your init_printing statement. In a notebook, you do not want to run latex, instead you should use mathjax, so try this instead:

init_printing(use_latex='mathjax')

When I use this, I get normal pretty printing everywhere, even when I have a sympy expression as the last line of the cell.

Corrincorrina answered 26/7, 2014 at 16:44 Comment(1)
Why would you not want to run latex in a notebook? This suggestion prints the unicode prettyprint for me as well. What versions of ipython and sympy are you using? Could be related to the issue mentioned at #32011445Septuplicate
K
15

I have asked a similar question (now linked to this one). After reading the answers and tinkering a bit, I conclude that there are 3 types of output one can get:

  1. "Not pretty", pure text... "low quality". This is what one obtains with print(expression)

  2. Pretty, pure text... "medium quality". This is what one obtains with

     import sympy as sym
     sympy.pprint(expression)
    

It still uses the same font and uses only characters to put together the mathematical expression. But it can, e.g., raise numbers for powers, pull fractions by laying out the horizontal line, etc.

  1. Pretty, with graphics, symbols, etc... "high quality". This is what one obtains with

     import IPython.display as disp
     disp.display(expression)
    

This is the same as what one obtains as an output of the notebook cell, but now as a result of a command. Then, one can have multiple such outputs from a single notebook cell.

It is worth noting that:

  1. sym.init_printing(... affects the output of sym.pprint.

  2. sym.latex(expression) produces a LaTeX string for the expression. disp.Math(... produces the expression from LaTeX. These two may come in useful. Thus, disp.display(disp.Math(sym.latex(expression))) would produce the same output as disp.display(expression).

Kirimia answered 17/1, 2020 at 14:15 Comment(1)
I appreciate your in-depth review of possibilities and their differences. Much better than "use x method" without explaining the ins and outs of the method. For those using Jupyter notebook, display will be available without importing it and can be used like this: display(expression) directly.Blindfish
D
9

This works,

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

x = symbols('x')
display(x)

int_x = Integral(cos(x)*exp(x), x)
result = "$${} = {}$$".format(latex(int_x), latex(int_x.doit()))
display(Latex(result))

derv_x = Derivative(cos(x)*exp(x), x)
result = "$${} = {}$$".format(latex(derv_x), latex(derv_x.doit()))
display(Latex(result))

try it for yourself.

Dehorn answered 22/2, 2019 at 4:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.