Skip to content Skip to sidebar Skip to footer

LaTeX Equations Do Not Render In Google Colaboratory When Using IPython.display.Latex

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}

Solution 1:

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}

Solution 2:

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


Solution 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:

enter image description here


Solution 4:

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


Solution 5:

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!


Post a Comment for "LaTeX Equations Do Not Render In Google Colaboratory When Using IPython.display.Latex"