问题
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?
回答1:
So this a very hacky solution, but I got it to work atleast. The problem was indeed the missing texlive package. After installing texlive-latex-recommended
one still needs a type1cm.sty
file for the matplotlib example to work. Since the extra package could not be installed easily, I manually installed the type1cm package. To achieve this I executed following commands before importing matplotlib:
! sudo apt-get install texlive-latex-recommended #1
! sudo apt-get install dvipng texlive-fonts-recommended #2
! wget http://mirrors.ctan.org/macros/latex/contrib/type1cm.zip #3
! unzip type1cm.zip -d /tmp/type1cm #4
! cd /tmp/type1cm/type1cm/ && sudo latex type1cm.ins #5
! sudo mkdir /usr/share/texmf/tex/latex/type1cm #6
! sudo cp /tmp/type1cm/type1cm/type1cm.sty /usr/share/texmf/tex/latex/type1cm #7
! sudo texhash #8
Those commands will do the following:
- minimal texlive installation which still works
- not sure if those packages are necessary, but wont hurt to much
- download
type1cm
package from ctan offically - unzip to /tmp/type1cm
- install package by running
latex
command ontype1cm.ins
file. Note that providing the path directly to the latex command didnt work. Also thecd
andlatex
command have to be executed in the same line( behind the the same ! symbol), otherwise the cd has no effect - create folder for type1cm packge in texmf tree
- copy .sty file there
- update tex so that it find the new package
Resources:
How to install type1cm
Where to place *.sty files in Linux texlive install
来源:https://stackoverflow.com/questions/55746749/latex-equations-do-not-render-in-google-colaboratory-when-using-matplotlib