Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LaTeX equations do not render in google Colaboratory when using matplotlib

If I take the official example containing latex from the matplotlib website:

from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)
import numpy as np
import matplotlib.pyplot as plt


# Example data
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.plot(t, s)

plt.xlabel(r'\textbf{time} (s)')
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16)
plt.title(r"\TeX\ is Number "
          r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
          fontsize=16, color='gray')
# Make room for the ridiculously large title.
plt.subplots_adjust(top=0.8)

plt.savefig('tex_demo')
plt.show()

and try to run it in a Google Colab notebook it will produce a big stacktrace with the following message at the end:

 [Errno 2] No such file or directory: 'latex': 'latex'

Why does this happen and how can I fix that?

My attempts:
I thought this error might happen because latex is missing in the serving VM so I tried to install texlive before importing matplotlib with:

! sudo apt-get install texlive-latex-recommended 

This finishes successfully. However matplotlib complains about some missing latex *.sty file which after googling should be contained in the texlive-latex-extra package. But during the installation of the extra package some errors occurred:

Err:5 http://security.ubuntu.com/ubuntu bionic-updates/main amd64 ruby2.5 amd64 2.5.1-1ubuntu1.1
  404  Not Found [IP: 91.189.88.162 80]
Get:16 http://archive.ubuntu.com/ubuntu bionic/universe amd64 texlive-latex-extra all 2017.20180305-2 [10.6 MB]
Err:13 http://security.ubuntu.com/ubuntu bionic-updates/main amd64 libruby2.5 amd64 2.5.1-1ubuntu1.1
  404  Not Found [IP: 91.189.88.162 80]
Get:17 http://archive.ubuntu.com/ubuntu bionic/universe amd64 texlive-plain-generic all 2017.20180305-2 [23.6 MB]
Fetched 41.5 MB in 4s (11.3 MB/s)

So I cant finish the installation of texlive-latex-extra. How can I proceed?

like image 787
v.tralala Avatar asked Apr 18 '19 13:04

v.tralala


1 Answers

Actually there is an easier solution, that requires less passages with respect to the answer proposed by @v.tralala. It's indeed enough to install the ubuntu packages containing the required .sty files, that in this case are texlive-latex-extra and dvipng. So do the following installations:

!sudo apt install cm-super dvipng texlive-latex-extra texlive-latex-recommended

To find an ubuntu package that contains a specific .sty file see: https://tex.stackexchange.com/questions/39771/finding-a-ubuntu-package-for-a-sty-file

like image 65
Aelius Avatar answered Sep 18 '22 13:09

Aelius