2
votes

I want to use latex for my labels in my matplotlib figures. I'm using Mac OS X and thonny. I downloaded a test program but every time I get the error:

RuntimeError: Latex command not found. Install 'xelatex' or change pgf.texsystem to the desired command.

I think it's a path error...

I also tried to change the path

os.environ["PATH"] += os.pathsep + '/usr/local/texlive/2018/bin/x86_64-darwin'

I get this error instead:

kpathsea:make_tex: Invalid filename `DejaVu Serif', contains ' ' /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory is deprecated; in the future, examples will be found relative to the 'datapath' directory. "found relative to the 'datapath' directory.".format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory is deprecated; in the future, examples will be found relative to the 'datapath' directory. "found relative to the 'datapath' directory.".format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory is deprecated; in the future, examples will be found relative to the 'datapath' directory. "found relative to the 'datapath' directory.".format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory is deprecated; in the future, examples will be found relative to the 'datapath' directory. "found relative to the 'datapath' directory.".format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory is deprecated; in the future, examples will be found relative to the 'datapath' directory. "found relative to the 'datapath' directory.".format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory is deprecated; in the future, examples will be found relative to the 'datapath' directory. "found relative to the 'datapath' directory.".format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory is deprecated; in the future, examples will be found relative to the 'datapath' directory. "found relative to the 'datapath' directory.".format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory is deprecated; in the future, examples will be found relative to the 'datapath' directory. "found relative to the 'datapath' directory.".format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory is deprecated; in the future, examples will be found relative to the 'datapath' directory. "found relative to the 'datapath' directory.".format(key)) Traceback (most recent call last): File "/Users/Marten/Downloads/pgf_preamble_sgskip.py", line 33, in plt.tight_layout(.5) File "/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/pyplot.py", line 1382, in tight_layout gcf().tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect) File "/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/figure.py", line 2370, in tight_layout renderer = get_renderer(self) File "/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/tight_layout.py", line 228, in get_renderer renderer = canvas.get_renderer() File "/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/backends/backend_pgf.py", line 946, in get_renderer return RendererPgf(self.figure, None, dummy=True) File "/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/backends/backend_pgf.py", line 393, in init self.latexManager = LatexManagerFactory.get_latex_manager() File "/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/backends/backend_pgf.py", line 208, in get_latex_manager new_inst = LatexManager() File "/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/backends/backend_pgf.py", line 290, in init "or error in preamble:\n%s" % stdout) matplotlib.backends.backend_pgf.LatexError: LaTeX returned an error, probably missing font or error in preamble: b'This is XeTeX, Version 3.14159265-2.6-0.99999 (TeX Live 2018) (preloaded format=xelatex)\n restricted \write18 enabled.\n**entering extended mode\nLaTeX2e <2018-04-01> patch level 2\nBabel <3.18> and hyphenation patterns for 84 language(s) loaded.\n\n*(/usr/local/texlive/2018/texmf-dist/tex/latex/base/minimal.cls\nDocument Class: minimal 2001/05/25 Standard LaTeX minimal class\n)\n*(/usr/local/texlive/2018/texmf-dist/tex/latex/units/units.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/base/ifthen.sty)\n(/usr/local/texlive/2018/texmf-dist/tex/latex/units/nicefrac.sty))\n*(/usr/local/texlive/2018/texmf-dist/tex/latex/metalogo/metalogo.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/graphics/graphicx.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/graphics/keyval.sty)\n(/usr/local/texlive/2018/texmf-dist/tex/latex/graphics/graphics.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/graphics/trig.sty)\n(/usr/local/texlive/2018/texmf-dist/tex/latex/graphics-cfg/graphics.cfg)\n(/usr/local/texlive/2018/texmf-dist/tex/latex/graphics-def/xetex.def)))\n(/usr/local/texlive/2018/texmf-dist/tex/generic/ifxetex/ifxetex.sty)\n(/usr/local/texlive/2018/texmf-dist/tex/latex/fontspec/fontspec.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/l3packages/xparse/xparse.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/l3kernel/expl3.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/l3kernel/expl3-code.tex)\n(/usr/local/texlive/2018/texmf-dist/tex/latex/l3kernel/l3xdvipdfmx.def)))\n(/usr/local/texlive/2018/texmf-dist/tex/latex/fontspec/fontspec-xetex.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/base/fontenc.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/base/tuenc.def))\n(/usr/local/texlive/2018/texmf-dist/tex/latex/fontspec/fontspec.cfg))))\n*(/usr/local/texlive/2018/texmf-dist/tex/latex/unicode-math/unicode-math.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/unicode-math/unicode-math-xetex.s\nty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/l3packages/l3keys2e/l3keys2e.sty)\n (/usr/local/texlive/2018/texmf-dist/tex/latex/filehook/filehook.sty)\n(/usr/local/texlive/2018/texmf-dist/tex/latex/base/fix-cm.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/base/ts1enc.def))\n(/usr/local/texlive/2018/texmf-dist/tex/latex/unicode-math/unicode-math-table.t\nex)))\n*\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n!\n! fontspec error: "font-not-found"\n! \n! The font "DejaVu Serif" cannot be found.\n! \n! See the fontspec documentation for further information.\n! \n! For immediate help type H .\n!............................................... \n \n<*> \setmainfont{DejaVu Serif}\n \nNo pages of output.\nTranscript written on texput.log.\n'

Test Program:

import matplotlib as mpl
mpl.use("pgf")
import matplotlib.pyplot as plt
import os
os.environ["PATH"] += os.pathsep + '/usr/local/texlive/2018/bin/x86_64-darwin'

plt.rcParams.update({
    "font.family": "serif",  # use serif/main font for text elements
    "text.usetex": True,     # use inline math for ticks
    "pgf.rcfonts": False,    # don't setup fonts from rc parameters
    "pgf.texsystem" : "xelatex",
    "pgf.preamble": [
         "\\usepackage{units}",          # load additional packages
         "\\usepackage{metalogo}",
         "\\usepackage{unicode-math}",   # unicode math setup
         r"\setmathfont{xits-math.otf}",
         r"\setmainfont{DejaVu Serif}",  # serif font via preamble
         ]
})

plt.figure(figsize=(4.5, 2.5))
plt.plot(range(5))
plt.xlabel("unicode text: я, ψ, €, ü, \\unitfrac[10]{°}{µm}")
plt.ylabel("\\XeLaTeX")
plt.legend(["unicode math: $λ=∑_i^∞ μ_i^2$"])
plt.tight_layout(.5)

plt.savefig("pgf_preamble.pdf")
plt.savefig("pgf_preamble.png")
1
It's weird that your installation didn't deal with the PATH properly. Anyways, regarding your second error, it's likely because you're using xelatex specific packages. So change the line "pgf.texsystem" : "pdflatex", into "pgf.texsystem" : "xelatex",.gniourf_gniourf
Yeah xelatex is the default value. It is the same error with xelatex so I tried pdflatex, but it is the same. So i can start xelatex with the terminal from any direction if that helps ... :/madden994
I'm pretty sure the second error you obtain comes from you using pdflatex instead of xelatex. Can you please run you program with "pgf.texsystem" : "xelatex", and edit your answer to include the full error message?gniourf_gniourf
So I edited and put the complete error in. I tried also to comment out the dejavu serif and I get different error RuntimeError: No suitable pdf to png renderer found.madden994
The problem is now clear: latex can't find the DejaVu Serif font... so either this font isn't installed on your system, or there's something wrong with your texlive installation (or the way you call this python script). Can you compile, with xelatex, the following? (save it in a file, say test.tex and run xelatex test.tex): \documentclass{minimal} \usepackage{fontspec} \setmainfont{DejaVu Serif} \begin{document} Hello \end{document}gniourf_gniourf

1 Answers

1
votes

So i tried a different test program with plt.rc('text', usetex=True) and plt.rc('font', family='serif') without an extra preamble. And that works when I add the path os.environ["PATH"] += os.pathsep + '/usr/local/texlive/2018/bin/x86_64-darwin'. So problem is solved in a different way. – madden994 just now edit