使用matplotlib时,LaTeX方程式不会在Google合作实验室中呈现 [英] LaTeX equations do not render in google Colaboratory when using matplotlib

查看:118
本文介绍了使用matplotlib时,LaTeX方程式不会在Google合作实验室中呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

因此,这是一个非常棘手的解决方案,但我至少可以使用它.问题确实是缺少texlive软件包.安装texlive-latex-recommended之后,仍然需要一个type1cm.sty文件,matplotlib示例才能正常工作.由于无法轻松安装额外的软件包,因此我手动安装了type1cm软件包.为此,我在导入matplotlib之前执行了以下命令:

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:

  1. 仍然可以正常运行的最小texlive安装
  2. 不确定这些软件包是否必要,但不会造成很大的伤害
  3. 从ctan官方下载type1cm程序包
  4. 解压缩到/tmp/type1cm
  5. 通过在type1cm.ins文件上运行latex命令来
  6. 安装软件包.请注意,直接提供指向Latex命令的路径无效.另外,cdlatex命令必须在同一行中执行(在相同的!符号后面),否则cd无效
  7. 在texmf树中为type1cm packge创建文件夹
  8. 在其中复制.sty文件
  9. 更新tex,以便找到新的包
  1. minimal texlive installation which still works
  2. not sure if those packages are necessary, but wont hurt to much
  3. download type1cm package from ctan offically
  4. unzip to /tmp/type1cm
  5. install package by running latex command on type1cm.ins file. Note that providing the path directly to the latex command didnt work. Also the cd and latex command have to be executed in the same line( behind the the same ! symbol), otherwise the cd has no effect
  6. create folder for type1cm packge in texmf tree
  7. copy .sty file there
  8. update tex so that it find the new package

资源:
如何安装type1cm
在Linux texlive安装中将* .sty文件放置在何处

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

这篇关于使用matplotlib时,LaTeX方程式不会在Google合作实验室中呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆