LaTeX equations do not render in google Colaboratory when using IPython.display.Latex

glS picture glS · Feb 18, 2018 · Viewed 9.3k times · Source

In a regular jupyter notebook, running, for example, the following:

from IPython.display import display, Math, Latex
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))

produces an equation rendered in LaTeX (via Mathjax).

Even though LaTeX works fine in markdown cells, LaTeX equations produced as above do not seem to render in Google Colaboratory. The same happens to the output of functions for example from qutip, which would normally render in latex (for example, qutip.basis(2, 0) would normally render in latex, but doesn't in Colaboratory).

Why does this happen? Is there a way to have this work?

Answer

user1301930 picture user1301930 · Jun 7, 2018

You can get the Latex to render by including the MathJax library.

from IPython.display import Math, HTML
display(HTML("<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/"
               "latest.js?config=default'></script>"))

Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')

A similar questions was asked here: Rendering LaTeX in output cells in Colaboratory