Repository navigation
Closed
Labels
Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issueshttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesbackend: pgf
Description
With default parameters the script fails to write the pgf file to disk. Running the script triggers an exception (see code block and message below). Setting pgf.rcfonts to False fixes the issue for me, but the parameter is set to True by default.
Maybe related: #2097
Code for reproduction
import matplotlib.pyplot as plt
import matplotlib as mpl
# remove or set to True (default) to trigger exception
rc_xelatex = {'pgf.rcfonts': False}
mpl.rcParams.update(rc_xelatex)
plt.plot([1, 2, 3, 4], label="$test$") # adding latex label
plt.legend(loc='right') # add to plot
plt.show() # works
plt.savefig("latex_pyplot.pdf") # works
plt.savefig("latex_pyplot.pgf") # exception if pgf.rcfonts is TrueTerminal output
kpathsea:make_tex: Invalid fontname `Bitstream Vera Serif', contains ' '
Traceback (most recent call last):
File "latex_pyplot.py", line 16, in <module>
plt.savefig("latex_pyplot.pgf") # exception if pgf.rcfonts is True
File "/Users/jens/dev/matplotlib/lib/matplotlib/pyplot.py", line 710, in savefig
res = fig.savefig(*args, **kwargs)
File "/Users/jens/dev/matplotlib/lib/matplotlib/figure.py", line 1883, in savefig
self.canvas.print_figure(fname, **kwargs)
File "/Users/jens/dev/matplotlib/lib/matplotlib/backend_bases.py", line 2261, in print_figure
**kwargs)
File "/Users/jens/dev/matplotlib/lib/matplotlib/backends/backend_pgf.py", line 834, in print_pgf
self._print_pgf_to_fh(fh, *args, **kwargs)
File "/Users/jens/dev/matplotlib/lib/matplotlib/backends/backend_pgf.py", line 813, in _print_pgf_to_fh
RendererPgf(self.figure, fh),
File "/Users/jens/dev/matplotlib/lib/matplotlib/backends/backend_pgf.py", line 417, in __init__
self.latexManager = LatexManagerFactory.get_latex_manager()
File "/Users/jens/dev/matplotlib/lib/matplotlib/backends/backend_pgf.py", line 229, in get_latex_manager
new_inst = LatexManager()
File "/Users/jens/dev/matplotlib/lib/matplotlib/backends/backend_pgf.py", line 313, in __init__
raise LatexError("LaTeX returned an error, probably missing font or error in preamble:\n%s" % stdout)
matplotlib.backends.backend_pgf.LatexError: LaTeX returned an error, probably missing font or error in preamble:
This is XeTeX, Version 3.14159265-2.6-0.99998 (TeX Live 2017) (preloaded format=xelatex)
restricted \write18 enabled.
**entering extended mode
LaTeX2e <2017-04-15>
Babel <3.16> and hyphenation patterns for 84 language(s) loaded.
*(/usr/local/texlive/2017/texmf-dist/tex/latex/base/minimal.cls
Document Class: minimal 2001/05/25 Standard LaTeX minimal class
)
(Please type a command or say `\end')
*
*(/usr/local/texlive/2017/texmf-dist/tex/latex/fontspec/fontspec.sty
(/usr/local/texlive/2017/texmf-dist/tex/latex/l3packages/xparse/xparse.sty
(/usr/local/texlive/2017/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/local/texlive/2017/texmf-dist/tex/latex/l3kernel/expl3-code.tex)
(/usr/local/texlive/2017/texmf-dist/tex/latex/l3kernel/l3xdvipdfmx.def)))
(/usr/local/texlive/2017/texmf-dist/tex/latex/fontspec/fontspec-xetex.sty
(/usr/local/texlive/2017/texmf-dist/tex/latex/base/fontenc.sty
(/usr/local/texlive/2017/texmf-dist/tex/latex/base/tuenc.def))
(/usr/local/texlive/2017/texmf-dist/tex/latex/fontspec/fontspec.cfg)))
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! fontspec error: "font-not-found"
!
! The font "Bitstream Vera Serif" cannot be found.
!
! See the fontspec documentation for further information.
!
! For immediate help type H <return>.
!...............................................
<*> \setmainfont{Bitstream Vera Serif}
No pages of output.
Transcript written on texput.log.
fc-list
...
/opt/X11/share/fonts/TTF/VeraMoBI.ttf: Bitstream Vera Sans Mono:style=Bold Oblique
/usr/X11R6/lib/X11/fonts/TTF/VeraIt.ttf: Bitstream Vera Sans:style=Oblique
/opt/X11/share/fonts/TTF/Vera.ttf: Bitstream Vera Sans:style=Roman
/usr/X11R6/lib/X11/fonts/TTF/VeraMoBd.ttf: Bitstream Vera Sans Mono:style=Bold
/opt/X11/share/fonts/TTF/VeraMoIt.ttf: Bitstream Vera Sans Mono:style=Oblique
/opt/X11/share/fonts/TTF/VeraSe.ttf: Bitstream Vera Serif:style=Roman
/opt/X11/share/fonts/TTF/VeraMono.ttf: Bitstream Vera Sans Mono:style=Roman
/usr/X11R6/lib/X11/fonts/TTF/VeraSe.ttf: Bitstream Vera Serif:style=Roman
/opt/X11/share/fonts/TTF/VeraIt.ttf: Bitstream Vera Sans:style=Oblique
/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf: Bitstream Vera Sans:style=Roman
/usr/X11R6/lib/X11/fonts/TTF/VeraMoBI.ttf: Bitstream Vera Sans Mono:style=Bold Oblique
/usr/X11R6/lib/X11/fonts/TTF/VeraBI.ttf: Bitstream Vera Sans:style=Bold Oblique
/usr/X11R6/lib/X11/fonts/TTF/VeraMono.ttf: Bitstream Vera Sans Mono:style=Roman
/usr/X11R6/lib/X11/fonts/TTF/VeraBd.ttf: Bitstream Vera Sans:style=Bold
/usr/X11R6/lib/X11/fonts/TTF/VeraSeBd.ttf: Bitstream Vera Serif:style=Bold
/opt/X11/share/fonts/TTF/VeraBd.ttf: Bitstream Vera Sans:style=Bold
/opt/X11/share/fonts/TTF/VeraSeBd.ttf: Bitstream Vera Serif:style=Bold
/opt/X11/share/fonts/TTF/VeraMoBd.ttf: Bitstream Vera Sans Mono:style=Bold
/opt/X11/share/fonts/TTF/VeraBI.ttf: Bitstream Vera Sans:style=Bold Oblique
/usr/X11R6/lib/X11/fonts/TTF/VeraMoIt.ttf: Bitstream Vera Sans Mono:style=Oblique
...
Matplotlib version
- Operating system: macOS 12.13 High Sierra
- Matplotlib version: Matplotlib 2.1.1 (Anaconda default) and master branch bd75712
- Matplotlib backend (
print(matplotlib.get_backend())): MacOSX - Python version: 2.7.14 (Anaconda)
Metadata
Metadata
Assignees
Labels
Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issueshttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesbackend: pgf
Activity
jklymak commentedon Jan 23, 2018
Sorry, can't reproduce; only difference is that I'm using python 3.
anntzer commentedon Jan 23, 2018
I'd guess xetex does not use fontconfig on osx but osx's own font management subsystem; does
system_profiler SPFontsDataTypefind the font for you?Tempted to close as "tex question, not matplotlib".
laborleben commentedon Jan 24, 2018
The
test_backend_pgf.pyruns fine, but also uses the rc settings from above (setting to non-default value,pgf.rcfontstoFalse). I was wondering why a change of the default rc settings is necessary to make the pgf backend working. I recently did a clean install of the macOS. So I assume that my environment is clean.anntzer commentedon Jan 24, 2018
fc-list is reporting fonts in a nonstandard location (from OSX's point of view, which normally puts fonts in /Library/Fonts etc), so it'd still be worth checking whether system_profiler SPFontsDataType is finding them. Likewise, does \setmainfont{...} work in a standalone tex example (independently of mpl)?
laborleben commentedon Jan 24, 2018
I can't find Vera Sans in the output of
system_profiler SPFontsDataType.When I use
fontspecand XeLaTeX I get the same error message ("font-not-found") as printed above:This behavior is expected.
fontspeccan't findBitstream Verasince it is installed in a non-default location. Adding a soft links to~/Library/Fontspointing to/opt/X11/share/fonts/TTF/Vera.ttf/opt/X11/share/fonts/TTF/VeraSe.ttf/opt/X11/share/fonts/TTF/VeraMono.ttffixes the issues for both, XeLaTex and matplotlib.
But at least on macOS, the default rc list settings do not run out of the box.
anntzer commentedon Jan 24, 2018
I see that backend_pgf (line 45) unconditionally uses fc-list to get the list of available fonts on OSX (on Linux too, but that's correct there). This bug report shows that this is incorrect and should be fixed to use e.g. system_profiler; but that is pretty slow so the fix will probably involve playing with the font cache.
Alternatively we could just let xelatex do the check, per https://tex.stackexchange.com/questions/53443/how-to-check-if-a-font-exists-in-xelatex (or other googlable things).
Labeling as medium difficulty as both solutions are fairly self-contained, though touching rather unused parts of the code and possibly involving some tex coding.
On a side note, there's the question of why backend_pgf defaults to xelatex and usetex to latex... (that's going to be more fun to uniformize...)
laborleben commentedon Jan 24, 2018
The underlying issue is here:
matplotlib/lib/matplotlib/backends/backend_pgf.py
Line 45 in d7f69fe
Maybe this is related:
matplotlib.font_manager.findSystemFonts()finds all fonts (which is basically callingfc-list),but
matplotlib.font_manager.OSXInstalledFonts()returns only the installed fonts in~/Library/Fontson macOS.Interestingly,
findSystemFonts()returns all fonts, becausematplotlib/lib/matplotlib/font_manager.py
Line 333 in d7f69fe
anntzer commentedon Jan 26, 2018
Can you try whether
works for you?
(actually we probably want to sanitize the font name to remove raw braces if someone is really wicked, but whatever)
laborleben commentedon Jan 26, 2018
I applied the patch to the master 86083dd. I get the following traceback:
test_inputresults to:However, if I convert
test_inputto a file and compile withxelatex, it runs for a minute and it works.anntzer commentedon Jan 27, 2018
Get rid of the
and
and try again? (we should already be controlling the interactionmode from Python anyways)
anntzer commentedon Jan 28, 2018
By the way, I guess this issue also raises the following question: should matplotlib try to find non-system-installed fonts? The OP has some fonts in /opt/X11/share/... which is nonstandard for OSX and apparently not recognized by OSX's
system_profiler SPFontsDataType.I would argue that we should not, instead just deferring to what the OS uses, but can be convinced otherwise too...
laborleben commentedon Jan 28, 2018
With the latest master:
git diff:
laborleben commentedon Jan 28, 2018
"DejaVu Serif" is not installed at all.
anntzer commentedon Jul 4, 2019
Should be closed by #10339 (see #10339 (comment)). Feel free to reopen if I missed something.