If all you want is the best pretty printing, use the init_printing() function. This will automatically enable the best printer available in your environment. You can also change the printer used in SymPy Live. Just change the “Output Format” in the settings.
In JupyterLab select Help -> Launch classic notebook, then from your browser menu (e.g. 3 dot menu) choose print and print to pdf.
init_printing() method, we are able to print the unicode characters for mathematical expressions. Syntax : sympy.init_printing()
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.
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:
"Not pretty", pure text... "low quality".
This is what one obtains with print(expression)
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.
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:
sym.init_printing(...
affects the output of sym.pprint
.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With