MATPLOTLIB:我必须如何提供字体规格文件以通过TeX渲染文本? [英] MATPLOTLIB: How do I have to provide font metrics files for rendering text by TeX?

查看:384
本文介绍了MATPLOTLIB:我必须如何提供字体规格文件以通过TeX渲染文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用TeX(MikTeX 2.9)渲染文本并使用列表中的字体"Times"时,我遇到了Python(WinPython-64bit-3.6.5.0Qt5)/MATPLOTLIB(版本2.2.2)问题.的标准字体(请参见 https://matplotlib.org/users/customizing.html )

I have a problem with Python (WinPython-64bit-3.6.5.0Qt5)/MATPLOTLIB (version 2.2.2) when rendering text with TeX (MikTeX 2.9) and application of the font "Times", which is in the list of standard fonts (see https://matplotlib.org/users/customizing.html)

在下面的最小示例中,我收到以下错误消息:

In the minimal example below, I get the following error message:

  File "C:\WinPython-64bit-3.6.5.0Qt5\python-3.6.5.amd64\lib\site-packages\matplotlib\dviread.py", line 471, in _fnt_def_real
    raise error_class("missing font metrics file: %s" % fontname)
FileNotFoundError: missing font metrics file: rsfs10

以下是重现该问题的示例:

Here is an example to reproduce the problem:

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

from matplotlib import rc
rc('font',**{'family':'serif','serif':['Times']})
rc('text', usetex=True)

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()

看起来python无法访问TeX字体,例如位于我的MikTeX安装文件夹中的rsfs10 C:\ Program Files \ MiKTeX 2.9 \ fonts \ source \ public \ rsfs

It looks like that python has no access to the TeX fonts, e.g. the rsfs10 which is located in my MikTeX installation folder C:\Program Files\MiKTeX 2.9\fonts\source\public\rsfs

如果我未指定字体名称,则它将与标准衬线字体一起使用(看起来像现代计算机的衬线).

If I do not specify the font name, it works with standard serif font (it look like computer modern serif).

from matplotlib import rc
rc("pdf", fonttype=3)
rc('font',**{'family':'serif'})
rc('text', usetex=True)

我必须添加以下内容:在下面的最小示例中,我首先收到一条警告消息(我没有获得完整的源代码,并且可能与主要问题无关):

I have to add the following: In the minimal example below, I first get a warning message (which I do not get in my full source code, and it is probably not related to the main problem):

C:\WinPython-64bit-3.6.5.0Qt5\python-3.6.5.amd64\lib\site-packages\matplotlib\font_manager.py:1328: UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans (prop.get_family(), self.defaultFamily[fontext]))

我已经尝试过此处描述的Windows解决方案: Matplotlib无法找到基本字体但是删除警告并没有帮助.如果我未将Times指定为字体,则不会引发警告.

I already tried the solution for Windows described here: Matplotlib cannot find basic fonts but it didn't help to remove the warning. If I do not specify Times as font, the warning also is not raised.

推荐答案

此处进行了讨论

With the discussion done here https://tex.stackexchange.com/questions/442179/why-are-tfm-files-missing-in-the-latex-rsfs-package I have found a solution:

在我的计算机上,MikTex安装是一个非常新的安装,并且该字体从未使用过,因此尚未创建所需的tfm文件.

On my computer, the MikTex installation was quite new, and the font has never been used before, therefore the required tfm files had not been created.

但是,matplotlib Python软件包(dviread.py)试图在使用前识别tfm文件,以便创建字体文件缓存.

However, the matplotlib Python package (dviread.py) tried to identify the tfm files before usage in order to create a fontfile cache.

我已经在另一台计算机上安装了完整的TeXLive安装.在安装过程中已经创建了tfm文件,我只是将文件复制到了计算机上的相应位置.

I have installed a complete TeXLive installation on another computer. There the tfm files were already created during installation and I just copied the files to the corresponding location on my computer.

这篇关于MATPLOTLIB:我必须如何提供字体规格文件以通过TeX渲染文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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