14

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?

glS
  • 1,209
  • 6
  • 18
  • 45

6 Answers6

8

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}
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
Atsushi Sakai
  • 189
  • 2
  • 7
  • 4
    sadly nowadays that does´t work on google colaboratory :( – SantiagoRodriguez Dec 16 '18 at 15:38
  • 1
    oh yeah, but it must be inside $..$ or $$..$$ – Dee Oct 06 '19 at 11:17
  • 1
    Note that there must not be newlines between your \begin \end and your equation. Alternately you can remove the \begin/end and simply wrap the equation in `$...$`, ie `$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$` as mentioned by @datdinhquoc – marcel Apr 23 '20 at 22:44
8

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

user1301930
  • 81
  • 1
  • 2
7

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:

enter image description here

ijuneja
  • 454
  • 1
  • 5
  • 17
2

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">

enter image description here

mikesneider
  • 772
  • 2
  • 10
  • 28
2

Use handcalcs

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.

enter image description here

What's nice, is now you have both the LaTeX code and the formatted output for your notebook!

Thom Ives
  • 3,642
  • 3
  • 30
  • 29
0

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}.$$
gianni
  • 126
  • 10