问题
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?
回答1:
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
回答2:
As an alternative, the following description should work on text cell on Colab.
\begin{equation}
F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx
\end{equation}
回答3:
I came across this question unaware of the markdown cell way of rendering latex. So in case someone is looking for that ...
As an easier alternative, Latex can be rendered directly using the text cells feature of Google-colab notebooks.
For example, the following text when entered into a text cell renders as shown, A
python notebook
Equation 1
$$\frac{sin(x)}{x}$$
foo bar
Equation 2
\begin{equation}
F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx
\end{equation}
When rendered by colab, it becomes:
来源:https://stackoverflow.com/questions/48856683/latex-equations-do-not-render-in-google-colaboratory-when-using-ipython-display