3

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 Customizing matplotlib)

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

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

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.

Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
Stellarator
  • 123
  • 1
  • 10
  • I did a bit more research and also looked inside the python-file C:\WinPython-64bit-3.6.5.0Qt5\python-3.6.5.amd64\lib\site-packages\matplotlib\dviread.py and where the problem comes from: I found out that there is the find_ex_file function inside, which should find a file in the texmf tree. It is called with filename='rsfs10' and format='tfm'. However, the file rsfs10.tfm is missing on my system. However, the LaTeX package rsfs is installed, but this package does not include the file rsfs10.tfm. see [link](https://ctan.org/tex-archive/fonts/rsfs/) – Stellarator Jul 18 '18 at 12:32

3 Answers3

3

I had this exact same issue in matplotlib. While installing texlive is one way to do it, I preferred not to re-install another latex distribution in parallel, especially since I already spent some time getting everything set up and running with other external programs.

I did a little searching within the C:\Program Files\MiKTeX\miktex\bin\x64 folder and it looks like running:

miktex-maketfm.exe rsfs10

did the trick. It built rsfs10.tfm and put it within the C:\Users\XXX\AppData\Local\MiKTeX\fonts\tfm\public\rsfs folder.

This solution is for Miktex 20.10. Once I ran this, matplotlib had no problems. Note that this post is a copy of my answer here.

inverteddy
  • 31
  • 2
1

With the discussion done here on Why are tfm files missing in the LaTeX rsfs package?, I have found a solution:

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.

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

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.

Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
Stellarator
  • 123
  • 1
  • 10
0

I had this happen when I moved from TeX Live 2022 to TeX Live 2023, with very similar rc settings for my Matplotlib figures. The error said missing font metrics file: cmss10. It is thrown by the dviread.py module in the snippet

...
if tfm is None:
    raise FileNotFoundError("missing font metrics file: %s" % fontname)
...

The following two steps worked to fix it for me:

  1. According to this doc page, I performed the following two troubleshooting actions:
  • Try deleting your .matplotlib/tex.cache directory. If you don’t know where to find .matplotlib, see matplotlib configuration and cache directory locations.
  • Make sure LaTeX, dvipng and ghostscript are each working and on your PATH.
  1. This then threw other errors, but since I hadn't restarted PyCharm since my installation of TeX Live 2023 (and deletion of TeX Live 2022), I gave that a shot too. Worked like a charm.
Mew
  • 368
  • 1
  • 11