How to pretty print in ipython notebook via sympy?

久未见 提交于 2019-11-28 20:05:41

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!