在Google Colab的Matplotlib中使用LaTeX字体报错求助
Matplotlib LaTeX渲染报错:type1ec.sty文件缺失
问题详情
按Matplotlib官方文档配置LaTeX字体渲染,代码如下:
plt.rcParams.update({ "text.usetex": True, "font.family": "Computer Modern Roman" })
尝试通过!pip install latex安装LaTeX后,在Windows 11系统使用Google Colab时触发错误,核心报错为:
! LaTeX Error: File `type1ec.sty' not found.
完整错误日志:
RuntimeError: latex was not able to process the following string: b'lp' Here is the full report generated by latex: This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian) (preloaded format=latex) restricted \write18 enabled. entering extended mode (/root/.cache/matplotlib/tex.cache/41e3affc34550e765935caf5a875fad6.tex LaTeX2e <2017-04-15> Babel <3.18> and hyphenation patterns for 3 language(s) loaded. (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2014/09/29 v1.4h Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo)) (/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty) ! LaTeX Error: File `type1ec.sty' not found. Type X to quit or <RETURN> to proceed, or enter new name. (Default extension: sty) Enter file name: ! Emergency stop. <read *> l.6 \usepackage {type1ec}^^M No pages of output. Transcript written on 41e3affc34550e765935caf5a875fad6.log.
解决步骤
- 注意:
pip install latex仅安装Python包装器,无法提供完整TeX环境,Colab需安装正规TeX Live组件。 - 在Colab单元格中运行以下命令安装缺失依赖:
!sudo apt-get update !sudo apt-get install texlive-latex-extra texlive-fonts-recommended dvipng cm-super
- 安装完成后,重启Colab运行时(点击顶部菜单
Runtime->Restart runtime),再重新执行Matplotlib配置代码即可。
内容的提问来源于stack exchange,提问作者Dila
相关产品推荐
相关产品推荐

