Search code examples
python-3.xjupyter-notebooksympymathjax

Show formatted unevaluated = evaluated in ipython notebook with sympy


I'd like to take notes in the ipython notebook. Is there a way to make it work so you can write math like if it was for a textbook?

from sympy import init_session
init_session()

expr = Integral(x,x)
print(latex(expr),'=',latex(expr.doit())) 

Gives for a result

\int x\, dx = \frac{x^{2}}{2}

But I'd like to see in the notebook

enter image description here

It works fine if I write each part in separate cells, but the output is over several lines. I'm using Anaconda on Windows XP so it's just mathjax doing the rendering.

I'd like a function that does this and maybe even adds eqn numbering like Maple.

edit: I've just discovered HTML, this formats nicely in the notebook.

from IPython.display import HTML
expr = Integral(x,x)
s = latex(expr) + ' = ' + latex(expr.doit())
HTML('$(1)~~'+ s + '$' + '\n' + '$$(2)~~'+ s + '$$')

I guess now I need to know the best way to do automatic numbering


Solution

  • Perhaps this is what you are looking for:

    In [15]: expr = Integral(x,x)
    
    In [16]: Eq(expr, expr.doit())
    Out[16]: 
              2
    ⌠        x 
    ⎮ x dx = ──
    ⌡        2