Reputation: 1299
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?
Upvotes: 15
Views: 33035
Reputation: 754
I found this Google Colab Notebook very useful. Here's an example:
\begin{align}
\mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^n a_i b_i.
\end{align}
Upvotes: 0
Reputation: 124
Can you copy and paste in colab from wikipedia? Just add $$ at the beginning and at the end of a multiline formula.
Yes you can
$$\mathbf J_{\mathbf F}(x_1, x_2, x_3) = \begin{bmatrix}
\dfrac{\partial y_1}{\partial x_1} & \dfrac{\partial y_1}{\partial x_2} & \dfrac{\partial y_1}{\partial x_3} \\[1em]
\dfrac{\partial y_2}{\partial x_1} & \dfrac{\partial y_2}{\partial x_2} & \dfrac{\partial y_2}{\partial x_3} \\[1em]
\dfrac{\partial y_3}{\partial x_1} & \dfrac{\partial y_3}{\partial x_2} & \dfrac{\partial y_3}{\partial x_3} \\[1em]
\dfrac{\partial y_4}{\partial x_1} & \dfrac{\partial y_4}{\partial x_2} & \dfrac{\partial y_4}{\partial x_3} \end{bmatrix}
= \begin{bmatrix}
1 & 0 & 0 \\
0 & 0 & 5 \\
0 & 8 x_2 & -2 \\
x_3\cos x_1 & 0 & \sin x_1 \end{bmatrix}.$$
Upvotes: 0
Reputation: 309
Update (April / 2021):
It's possible to write formulas in Colab just putting them between $
symbols, with no need to import libraries:
$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$
Old answer
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}
Upvotes: 10
Reputation: 3989
I hope you guys appreciate this route as much as I do. Came to me partially thru a post from a connection on LinkedIn.
In a first cell in a colab notebook:
%pip install handcalcs
In a new cell,
import handcalcs.render
Now, in another cell, let's do a simple test,
%%render
a = 23
b = 43
c = 2
d = 3.226
f = d / a + b
The output will be LaTeX code:
\[
\begin{aligned}
a &= 23 \;\textit{ }\\[10pt]
b &= 43 \;\textit{ }\\[10pt]
c &= 2 \;\textit{ }\\[10pt]
d &= 3.226 \;\textit{ }\\[10pt]
f &= \frac{ d }{ a } + b = \frac{ 3.226 }{ 23 } + 43 &= 43.14 \;\textit{ }
\end{aligned}
\]
Copy the part inside the []'s into a text cell,
\begin{aligned}
a &= 23 \;\textit{ }\\[10pt]
b &= 43 \;\textit{ }\\[10pt]
c &= 2 \;\textit{ }\\[10pt]
d &= 3.226 \;\textit{ }\\[10pt]
f &= \frac{ d }{ a } + b = \frac{ 3.226 }{ 23 } + 43 &= 43.14 \;\textit{ }
\end{aligned}
And it renders beautifully formatted equations using markdown in the text cell.
What's nice, is now you have both the LaTeX code and the formatted output for your notebook!
Upvotes: 2
Reputation: 802
An alternative is to use it as markdown with githubusercontent (source) instead a codeline on Colab, add a text (markdown text) and then to use the next line. e.g.
<img src="https://render.githubusercontent.com/render/math?math=e^{i \pi} = -1">
Upvotes: 2
Reputation: 464
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:
Upvotes: 7
Reputation: 191
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
Upvotes: 9