使用 matplotlib 时,LaTeX 方程不会在 google Colaboratory 中呈现

问题描述

如果我从 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'	extbf{time} (s)')
plt.ylabel(r'	extit{voltage} (mV)',fontsize=16)
plt.title(r"TeX is Number "
          r"$displaystylesum_{n=1}^inftyfrac{-e^{ipi}}{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()

并尝试在 Google Colab 笔记本中运行它,它会生成一个大堆栈跟踪,最后会显示以下消息:

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?

我的尝试:
我认为这个错误可能会发生,因为服务虚拟机中缺少乳胶,所以我尝试在导入 matplotlib 之前安装 texlive:

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 

这成功完成.然而 matplotlib 抱怨一些丢失的乳胶 *.sty 文件在谷歌搜索后应该包含在 texlive-latex-extra 包中.但是在安装额外包的过程中出现了一些错误:

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)

所以我无法完成 texlive-latex-extra 的安装.我该如何继续?

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


解决方案

其实有一个更简单的解决方案,相对于@v.tralala 提出的答案需要更少的段落.安装包含所需 .sty 文件的 ubuntu 包确实足够了,在本例中是 texlive-latex-extradvipng.因此,请执行以下安装:

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-get install texlive-latex-recommended 
! sudo apt install texlive-latex-extra
! sudo apt install dvipng

要查找包含特定 .sty 文件的 ubuntu 包,请参阅:https://tex.stackexchange.com/questions/39771/finding-a-ubuntu-package-for-a-sty-file

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

相关文章