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

LaTeX equations do not render in google Colaboratory when using matplotlib

如果我从matplotlib website:

中拿官方包含latex的例子
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()

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

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

为什么会发生这种情况,我该如何解决?

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

! sudo apt-get install texlive-latex-recommended 

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

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 的安装。我该如何继续?

所以这是一个非常棘手的解决方案,但我至少让它工作了。问题确实是缺少 texlive 包。安装 texlive-latex-recommended 之后,仍然需要一个 type1cm.sty 文件才能使 matplotlib 示例正常工作。由于无法轻松安装额外的包,我手动安装了 type1cm 包。为此,我在导入 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

这些命令将执行以下操作:

  1. 仍然有效的最小 texlive 安装
  2. 不确定是否需要这些包,但不会造成太大伤害
  3. 从 ctan 官方下载 type1cm
  4. 解压到/tmp/type1cm
  5. 通过 运行 latex 命令在 type1cm.ins 文件上安装包。请注意,直接向 latex 命令提供路径是行不通的。另外 cdlatex 命令必须在同一行执行(在同一个 ! 符号后面),否则 cd 没有效果
  6. 在 texmf 树中为 type1cm 包创建文件夹
  7. 在那里复制 .sty 文件
  8. 更新 tex 以便它找到新包

资源:
How to install type1cm
Where to place *.sty files in Linux texlive install

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

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

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

原因

基本上,问题是 type1cmtype1em 在 colab 环境中默认没有安装 matplotlib。更多关于这个问题的信息:https://github.com/matplotlib/matplotlib/issues/17412

解决方案

!apt install texlive-fonts-recommended texlive-fonts-extra cm-super dvipng

type1cmtexlive-fonts-extra 的一部分,type1eccm-super 的一部分。

所以要在 matplotlib 中设置 latex,你的 colab 笔记本中应该有这个块:

import matplotlib
from matplotlib import rc
import matplotlib.pyplot as plt
%matplotlib inline

rc('text', usetex=True)
matplotlib.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']
!apt install texlive-fonts-recommended texlive-fonts-extra cm-super dvipng