<divid="unreleased-message"> You are reading an old version of the documentation (v2.2.5). For the latest version see <ahref="https://matplotlib.org/stable/tutorials/text/pgf.html">https://matplotlib.org/stable/tutorials/text/pgf.html</a></div>
<pclass="last">Click <aclass="reference internal" href="#sphx-glr-download-tutorials-text-pgf-py"><spanclass="std std-ref">here</span></a> to download the full example code</p>
<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 either
<p>The second 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="../../faq/environment_variables_faq.html#envvar-PATH"><codeclass="xref std std-envvar docutils literal notranslate"><spanclass="pre">PATH</span></code></a>.</p>
<p>Rc parameters that control the behavior of the pgf backend:</p>
<blockquote>
<div><tableborder="1" class="docutils">
<colgroup>
<colwidth="24%" />
<colwidth="76%" />
</colgroup>
<theadvalign="bottom">
<trclass="row-odd"><thclass="head">Parameter</th>
<thclass="head">Documentation</th>
</tr>
</thead>
<tbodyvalign="top">
<trclass="row-even"><td>pgf.preamble</td>
<td>Lines to be included in the LaTeX preamble</td>
</tr>
<trclass="row-odd"><td>pgf.rcfonts</td>
<td>Setup fonts from rc params using the fontspec package</td>
</tr>
<trclass="row-even"><td>pgf.texsystem</td>
<td>Either "xelatex" (default), "lualatex" or "pdflatex"</td>
</tr>
</tbody>
</table>
</div></blockquote>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<p>TeX defines a set of special characters, such as:</p>
<spanclass="s2">"font.serif"</span><spanclass="p">:</span><spanclass="p">[],</span><spanclass="c1"># use latex default serif font</span>
<spanclass="s2">"font.sans-serif"</span><spanclass="p">:</span><spanclass="p">[</span><spanclass="s2">"DejaVu Sans"</span><spanclass="p">],</span><spanclass="c1"># use a specific sans-serif font</span>
<spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">text</span><spanclass="p">(</span><spanclass="mf">2.5</span><spanclass="p">,</span><spanclass="mf">1.</span><spanclass="p">,</span><spanclass="s2">"comic sans"</span><spanclass="p">,</span><spanclass="n">family</span><spanclass="o">=</span><spanclass="s2">"Comic Sans MS"</span><spanclass="p">)</span>
<spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">xlabel</span><spanclass="p">(</span><spanclass="sa">u</span><spanclass="s2">"µ is not $</span><spanclass="se">\\</span><spanclass="s2">mu$"</span><spanclass="p">)</span>
<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="s2">"</span><spanclass="se">\\</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 the <codeclass="docutils literal notranslate"><spanclass="pre">pgf.texsystem</span></code>
parameter. 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 be configured in the preamble.</p>
<spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">xlabel</span><spanclass="p">(</span><spanclass="sa">u</span><spanclass="s2">"µ is not $</span><spanclass="se">\\</span><spanclass="s2">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>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.</li>
<li>On Windows, the <spanclass="target" id="index-1"></span><aclass="reference internal" href="../../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="../../faq/environment_variables_faq.html#environment-variables"><spanclass="std std-ref">Environment Variables</span></a> and
<aclass="reference internal" href="../../faq/environment_variables_faq.html#setting-windows-environment-variables"><spanclass="std std-ref">Setting environment variables in windows</span></a> for details.</li>
<li>A limitation on Windows causes the backend to keep file handles that have
been opened by your application open. As a result, it may not be possible
to delete the corresponding files until the application closes (see
<li>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.</li>
<li>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.</li>
<li>The <codeclass="docutils literal notranslate"><spanclass="pre">pgf.preamble</span></code> rc setting provides lots of flexibility, and lots of
ways to cause problems. When experiencing problems, try to minimalize or
disable the custom preamble.</li>
<li>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
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
<li>If you still need help, please see <aclass="reference internal" href="../../faq/troubleshooting_faq.html#reporting-problems"><spanclass="std std-ref">Getting help</span></a></li>
</ul>
<divclass="sphx-glr-footer class sphx-glr-footer-example docutils container" id="sphx-glr-download-tutorials-text-pgf-py">