Ich habe pprint
, print
ausprobiert, ersteres gibt nur die Unicode-Version aus und letzteres macht keine hübschen Ausdrucke.
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)
sie müssen Anzeige verwenden:
from IPython.display import display
display(yourobject)
Es wird die entsprechende Darstellung ausgewählt (text/LaTex/png ...). In der aktuellsten Version von IPython (6.0+) wird standardmäßig die Anzeige importiert. Wir empfehlen jedoch, sie explizit zu importieren.
Das Problem liegt bei Ihrer init_printing-Anweisung. In einem Notizbuch möchten Sie kein Latex ausführen. Stattdessen sollten Sie Mathjax verwenden. Versuchen Sie stattdessen Folgendes:
init_printing(use_latex='mathjax')
Wenn ich dies benutze, bekomme ich überall einen normalen hübschen Ausdruck, selbst wenn ich einen Sympy-Ausdruck als letzte Zeile der Zelle habe.
Das funktioniert,
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))
probieren Sie es aus.