<spanid="sphx-glr-tutorials-text-pgf-py"></span><h1>Typesetting with XeLaTeX/LuaLaTeX<aclass="headerlink" href="#typesetting-with-xelatex-lualatex" title="Permalink to this headline">¶</a></h1>
<p>How to typeset text with the <codeclass="docutils literal notranslate"><spanclass="pre">pgf</span></code> backend in Matplotlib.</p>
<p>Using the <codeclass="docutils literal notranslate"><spanclass="pre">pgf</span></code> backend, Matplotlib can export figures as pgf drawing
commands that can be processed with pdflatex, xelatex or lualatex. XeLaTeX and
LuaLaTeX have full Unicode support and can use any font that is installed in
the operating system, making use of advanced typographic features of OpenType,
AAT and Graphite. Pgf pictures created by <codeclass="docutils literal notranslate"><spanclass="pre">plt.savefig('figure.pgf')</span></code>
can be embedded as raw commands in LaTeX documents. Figures can also be
directly compiled and saved to PDF with <codeclass="docutils literal notranslate"><spanclass="pre">plt.savefig('figure.pdf')</span></code> by
<p>The last method allows you to keep using regular interactive backends and to
save xelatex, lualatex or pdflatex compiled PDF files from the graphical user
interface.</p>
<p>Matplotlib's pgf support requires a recent <aclass="reference external" href="http://www.tug.org">LaTeX</a> installation that includes
the TikZ/PGF packages (such as <aclass="reference external" href="http://www.tug.org/texlive/">TeXLive</a>), preferably with XeLaTeX or LuaLaTeX
installed. If either pdftocairo or ghostscript is present on your system,
figures can optionally be saved to PNG images as well. The executables
for all applications must be located on your <spanclass="target" id="index-0"></span><aclass="reference internal" href="../../users/faq/environment_variables_faq.html#envvar-PATH"><codeclass="xref std std-envvar docutils literal notranslate"><spanclass="pre">PATH</span></code></a>.</p>
<p><aclass="reference internal" href="../../api/matplotlib_configuration_api.html#matplotlib.rcParams" title="matplotlib.rcParams"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">rcParams</span></code></a> that control the behavior of the pgf backend:</p>
<h2>Font specification<aclass="headerlink" href="#font-specification" title="Permalink to this headline">¶</a></h2>
<p>The fonts used for obtaining the size of text elements or when compiling
figures to PDF are usually defined in the <aclass="reference internal" href="../../api/matplotlib_configuration_api.html#matplotlib.rcParams" title="matplotlib.rcParams"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">rcParams</span></code></a>. You can also use the
LaTeX default Computer Modern fonts by clearing the lists for <codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=font.serif#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["font.serif"]</span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">['DejaVu</span><spanclass="pre">Serif',</span><spanclass="pre">'Bitstream</span><spanclass="pre">Vera</span><spanclass="pre">Serif',</span><spanclass="pre">'Computer</span><spanclass="pre">Modern</span><spanclass="pre">Roman',</span><spanclass="pre">'New</span><spanclass="pre">Century</span><spanclass="pre">Schoolbook',</span><spanclass="pre">'Century</span><spanclass="pre">Schoolbook</span><spanclass="pre">L',</span><spanclass="pre">'Utopia',</span><spanclass="pre">'ITC</span><spanclass="pre">Bookman',</span><spanclass="pre">'Bookman',</span><spanclass="pre">'Nimbus</span><spanclass="pre">Roman</span><spanclass="pre">No9</span><spanclass="pre">L',</span><spanclass="pre">'Times</span><spanclass="pre">New</span><spanclass="pre">Roman',</span><spanclass="pre">'Times',</span><spanclass="pre">'Palatino',</span><spanclass="pre">'Charter',</span><spanclass="pre">'serif']</span></code>),
<codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=font.sans-serif#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["font.sans-serif"]</span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">['DejaVu</span><spanclass="pre">Sans',</span><spanclass="pre">'Bitstream</span><spanclass="pre">Vera</span><spanclass="pre">Sans',</span><spanclass="pre">'Computer</span><spanclass="pre">Modern</span><spanclass="pre">Sans</span><spanclass="pre">Serif',</span><spanclass="pre">'Lucida</span><spanclass="pre">Grande',</span><spanclass="pre">'Verdana',</span><spanclass="pre">'Geneva',</span><spanclass="pre">'Lucid',</span><spanclass="pre">'Arial',</span><spanclass="pre">'Helvetica',</span><spanclass="pre">'Avant</span><spanclass="pre">Garde',</span><spanclass="pre">'sans-serif']</span></code>) or <codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=font.monospace#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["font.monospace"]</span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">['DejaVu</span><spanclass="pre">Sans</span><spanclass="pre">Mono',</span><spanclass="pre">'Bitstream</span><spanclass="pre">Vera</span><spanclass="pre">Sans</span><spanclass="pre">Mono',</span><spanclass="pre">'Computer</span><spanclass="pre">Modern</span><spanclass="pre">Typewriter',</span><spanclass="pre">'Andale</span><spanclass="pre">Mono',</span><spanclass="pre">'Nimbus</span><spanclass="pre">Mono</span><spanclass="pre">L',</span><spanclass="pre">'Courier</span><spanclass="pre">New',</span><spanclass="pre">'Courier',</span><spanclass="pre">'Fixed',</span><spanclass="pre">'Terminal',</span><spanclass="pre">'monospace']</span></code>). Please note that the glyph
coverage of these fonts is very limited. If you want to keep the Computer
Modern font face but require extended Unicode support, consider installing the
<aclass="reference external" href="https://sourceforge.net/projects/cm-unicode/">Computer Modern Unicode</a> fonts <em>CMU Serif</em>, <em>CMU Sans Serif</em>, etc.</p>
<p>When saving to <codeclass="docutils literal notranslate"><spanclass="pre">.pgf</span></code>, the font configuration Matplotlib used for the
layout of the figure is included in the header of the text file.</p>
<spanclass="c1"># Use specific cursive fonts.</span>
<spanclass="s2">"font.cursive"</span><spanclass="p">:</span><spanclass="p">[</span><spanclass="s2">"Comic Neue"</span><spanclass="p">,</span><spanclass="s2">"Comic Sans MS"</span><spanclass="p">],</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">text</span><spanclass="p">(</span><spanclass="mf">2.5</span><spanclass="p">,</span><spanclass="mf">2.</span><spanclass="p">,</span><spanclass="s2">"sans-serif"</span><spanclass="p">,</span><spanclass="n">family</span><spanclass="o">=</span><spanclass="s2">"DejaVu Sans"</span><spanclass="p">)</span><spanclass="c1"># Use specific sans font.</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">set_xlabel</span><spanclass="p">(</span><spanclass="s2">"µ is not $</span><spanclass="se">\\</span><spanclass="s2">mu$"</span><spanclass="p">)</span>
<spanid="pgf-preamble"></span><h2>Custom preamble<aclass="headerlink" href="#custom-preamble" title="Permalink to this headline">¶</a></h2>
<p>Full customization is possible by adding your own commands to the preamble.
Use <codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=pgf.preamble#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["pgf.preamble"]</span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">''</span></code>) if you want to configure the math fonts,
using <codeclass="docutils literal notranslate"><spanclass="pre">unicode-math</span></code> for example, or for loading additional packages. Also,
if you want to do the font configuration yourself instead of using the fonts
specified in the rc parameters, make sure to disable <codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=pgf.rcfonts#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["pgf.rcfonts"]</span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>).</p>
<spanclass="s2">"font.family"</span><spanclass="p">:</span><spanclass="s2">"serif"</span><spanclass="p">,</span><spanclass="c1"># use serif/main font for text elements</span>
<spanclass="s2">"text.usetex"</span><spanclass="p">:</span><spanclass="kc">True</span><spanclass="p">,</span><spanclass="c1"># use inline math for ticks</span>
<spanclass="s2">"pgf.rcfonts"</span><spanclass="p">:</span><spanclass="kc">False</span><spanclass="p">,</span><spanclass="c1"># don't setup fonts from rc parameters</span>
<spanclass="sa">r</span><spanclass="s2">"\usepackage{unicode-math}"</span><spanclass="p">,</span><spanclass="c1"># unicode math setup</span>
<spanclass="sa">r</span><spanclass="s2">"\setmainfont{DejaVu Serif}"</span><spanclass="p">,</span><spanclass="c1"># serif font via preamble</span>
<spanid="pgf-texsystem"></span><h2>Choosing the TeX system<aclass="headerlink" href="#choosing-the-tex-system" title="Permalink to this headline">¶</a></h2>
<p>The TeX system to be used by Matplotlib is chosen by <codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=pgf.texsystem#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["pgf.texsystem"]</span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">'xelatex'</span></code>).
Possible values are <codeclass="docutils literal notranslate"><spanclass="pre">'xelatex'</span></code> (default), <codeclass="docutils literal notranslate"><spanclass="pre">'lualatex'</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">'pdflatex'</span></code>.
Please note that when selecting pdflatex, the fonts and Unicode handling must
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">set_xlabel</span><spanclass="p">(</span><spanclass="sa">r</span><spanclass="s2">"µ is not $\mu$"</span><spanclass="p">)</span>
<spanid="pgf-troubleshooting"></span><h2>Troubleshooting<aclass="headerlink" href="#troubleshooting" title="Permalink to this headline">¶</a></h2>
<ulclass="simple">
<li><p>Please note that the TeX packages found in some Linux distributions and
MiKTeX installations are dramatically outdated. Make sure to update your
package catalog and upgrade or install a recent TeX distribution.</p></li>
<li><p>On Windows, the <spanclass="target" id="index-1"></span><aclass="reference internal" href="../../users/faq/environment_variables_faq.html#envvar-PATH"><codeclass="xref std std-envvar docutils literal notranslate"><spanclass="pre">PATH</span></code></a> environment variable may need to be modified
to include the directories containing the latex, dvipng and ghostscript
executables. See <aclass="reference internal" href="../../users/faq/environment_variables_faq.html#environment-variables"><spanclass="std std-ref">Environment variables</span></a> and
<aclass="reference internal" href="../../users/faq/environment_variables_faq.html#setting-windows-environment-variables"><spanclass="std std-ref">Setting environment variables in Windows</span></a> for details.</p></li>
<li><p>Sometimes the font rendering in figures that are saved to png images is
very bad. This happens when the pdftocairo tool is not available and
ghostscript is used for the pdf to png conversion.</p></li>
<li><p>Make sure what you are trying to do is possible in a LaTeX document,
that your LaTeX syntax is valid and that you are using raw strings
if necessary to avoid unintended escape sequences.</p></li>
<li><p><codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=pgf.preamble#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["pgf.preamble"]</span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">''</span></code>) provides lots of flexibility, and lots of
ways to cause problems. When experiencing problems, try to minimalize or
disable the custom preamble.</p></li>
<li><p>Configuring an <codeclass="docutils literal notranslate"><spanclass="pre">unicode-math</span></code> environment can be a bit tricky. The
TeXLive distribution for example provides a set of math fonts which are
usually not installed system-wide. XeTeX, unlike LuaLatex, cannot find
these fonts by their name, which is why you might have to specify
<codeclass="docutils literal notranslate"><spanclass="pre">\setmathfont{xits-math.otf}</span></code> instead of <codeclass="docutils literal notranslate"><spanclass="pre">\setmathfont{XITS</span><spanclass="pre">Math}</span></code> or
alternatively make the fonts available to your OS. See this
<aclass="reference external" href="https://tex.stackexchange.com/q/43642/">tex.stackexchange.com question</a> for more details.</p>
</li>
<li><p>If the font configuration used by Matplotlib differs from the font setting
in yout LaTeX document, the alignment of text elements in imported figures
may be off. Check the header of your <codeclass="docutils literal notranslate"><spanclass="pre">.pgf</span></code> file if you are unsure about
the fonts Matplotlib used for the layout.</p></li>
<li><p>Vector images and hence <codeclass="docutils literal notranslate"><spanclass="pre">.pgf</span></code> files can become bloated if there are a lot
of objects in the graph. This can be the case for image processing or very
big scatter graphs. In an extreme case this can cause TeX to run out of
memory: "TeX capacity exceeded, sorry" You can configure latex to increase
the amount of memory available to generate the <codeclass="docutils literal notranslate"><spanclass="pre">.pdf</span></code> image as discussed on
Another way would be to "rasterize" parts of the graph causing problems
using either the <codeclass="docutils literal notranslate"><spanclass="pre">rasterized=True</span></code> keyword, or <codeclass="docutils literal notranslate"><spanclass="pre">.set_rasterized(True)</span></code> as
per <aclass="reference internal" href="../../gallery/misc/rasterization_demo.html"><spanclass="doc">this example</span></a>.</p></li>
<li><p>Various math fonts are compiled and rendered only if corresponding font
packages are loaded. Specifically, when using <codeclass="docutils literal notranslate"><spanclass="pre">\mathbf{}</span></code> on Greek letters,
the default computer modern font may not contain them, in which case the
letter is not rendered. In such scenarios, the <codeclass="docutils literal notranslate"><spanclass="pre">lmodern</span></code> package should be
loaded.</p></li>
<li><p>If you still need help, please see <aclass="reference internal" href="../../users/faq/troubleshooting_faq.html#reporting-problems"><spanclass="std std-ref">Getting help</span></a></p></li>
</ul>
<divclass="sphx-glr-footer class sphx-glr-footer-example docutils container" id="sphx-glr-download-tutorials-text-pgf-py">