Force Sympy to print math in MathJax Jupyter

Viewed 1195

I am using VSCode and Jupyter to work on Sympy. Normally, it prints the math equations in MathJax format. sympy documentation says:

In the IPython notebook, it will use MathJax to render LATEX.

MathJax output:

enter image description here

But, when I install matplotlib, it automatically prints as image using matplotlib.

Matplotlib output:

enter image description here

I have to install matplotlib to plot my results. Is there any way to force sympy to print math in MathJax format? I used

init_printing(use_latex=True)

which didn't work. Any ideas?

1 Answers

Try this code:

from IPython.display import Math, display
import sympy as sy

x = sy.symbols('x')
expression = sy.sin(x)

display(Math('f(x) = ' + sy.latex(expression)))

or, for the case you have, use LaTeX format:

display(Math('\\left[ \\frac{3\\mu_2^2+2\\sigma_2^2\\log(2)}{2\\mu_2} \\right]'))

this is the result:

enter image description here

Related