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

问题描述 投票:0回答:1

如果我从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()

并尝试在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)

所以我无法完成qazxsw poi的安装。我该怎么办?

python matplotlib latex google-colaboratory
1个回答
0
投票

所以这是一个非常hacky的解决方案,但我得到它至少工作。问题确实是丢失的texlive包。安装texlive-latex-extra后,仍然需要一个texlive-latex-recommended文件,matplotlib示例才能工作。由于无法轻松安装额外的软件包,我手动安装了type1cm软件包。为了实现这一点,我在导入matplotlib之前执行了以下命令:

type1cm.sty

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

  1. 最小的texlive安装仍然有效
  2. 不确定这些包是否必要,但不会伤害太多
  3. 从ctan官方下载! 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
  4. 解压缩到/ tmp / type1cm
  5. 通过在type1cm文件上运行latex命令来安装软件包。请注意,直接提供latex命令的路径不起作用。 type1cm.inscd命令也必须在同一行(同一个!符号后面)执行,否则cd无效
  6. 在texmf树中为type1cm包创建文件夹
  7. 在那里复制.sty文件
  8. 更新tex以便找到新包

资源: latex How to install type1cm

© www.soinside.com 2019 - 2024. All rights reserved.