<divid="unreleased-message"> You are reading an old version of the documentation (v2.1.2). For the latest version see <ahref="https://matplotlib.org/stable/tutorials/text/usetex.html">https://matplotlib.org/stable/tutorials/text/usetex.html</a></div>
<spanid="sphx-glr-tutorials-text-usetex-py"></span><h1>Text rendering With LaTeX<aclass="headerlink" href="#text-rendering-with-latex" title="Permalink to this headline">¶</a></h1>
<p>Rendering text with LaTeX in Matplotlib.</p>
<p>Matplotlib has the option to use LaTeX to manage all text layout. This
option is available with the following backends:</p>
<ulclass="simple">
<li>Agg</li>
<li>PS</li>
<li>PDF</li>
</ul>
<p>The LaTeX option is activated by setting <codeclass="docutils literal"><spanclass="pre">text.usetex</span><spanclass="pre">:</span><spanclass="pre">True</span></code> in
your rc settings. Text handling with matplotlib’s LaTeX support is
slower than matplotlib’s very capable <aclass="reference internal" href="mathtext.html#sphx-glr-tutorials-text-mathtext-py"><spanclass="std std-ref">mathtext</span></a>, but is more flexible, since different LaTeX
packages (font packages, math packages, etc.) can be used. The
results can be striking, especially when you take care to use the same
fonts in your figures as in the main document.</p>
<p>Matplotlib’s LaTeX support requires a working <aclass="reference external" href="http://www.tug.org">LaTeX</a> installation, <aclass="reference external" href="http://www.nongnu.org/dvipng/">dvipng</a>
(which may be included with your LaTeX installation), and <aclass="reference external" href="https://ghostscript.com/">Ghostscript</a>
(GPL Ghostscript 8.60 or later is recommended). The executables for these
external dependencies must all 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"><spanclass="pre">PATH</span></code></a>.</p>
<p>There are a couple of options to mention, which can be changed using <aclass="reference internal" href="../introductory/customizing.html#sphx-glr-tutorials-introductory-customizing-py"><spanclass="std std-ref">rc
settings</span></a>. Here is an example matplotlibrc file:</p>
<p>The first valid font in each family is the one that will be loaded. If the
fonts are not specified, the Computer Modern fonts are used by default. All of
the other fonts are Adobe fonts. Times and Palatino each have their own
accompanying math fonts, while the other Adobe serif fonts make use of the
Computer Modern math fonts. See the <aclass="reference external" href="http://www.ctan.org/tex-archive/macros/latex/required/psnfss/psnfss2e.pdf">PSNFSS</a> documentation for more details.</p>
<p>To use LaTeX and select Helvetica as the default font, without editing
<p>Note that display math mode (<codeclass="docutils literal"><spanclass="pre">$$</span><spanclass="pre">e=mc^2</span><spanclass="pre">$$</span></code>) is not supported, but adding the
command <codeclass="docutils literal"><spanclass="pre">\displaystyle</span></code>, as in <codeclass="xref py py-obj docutils literal"><spanclass="pre">tex_demo.py</span></code>, will produce the same
results.</p>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<p>Certain characters require special escaping in TeX, such as:</p>
<pclass="last">Therefore, these characters will behave differently depending on
the rcParam <codeclass="docutils literal"><spanclass="pre">text.usetex</span></code> flag.</p>
</div>
<divclass="section" id="usetex-with-unicode">
<spanid="usetex-unicode"></span><h2>usetex with unicode<aclass="headerlink" href="#usetex-with-unicode" title="Permalink to this headline">¶</a></h2>
<p>It is also possible to use unicode strings with the LaTeX text manager, here is
an example taken from <codeclass="xref py py-obj docutils literal"><spanclass="pre">tex_demo.py</span></code>. The axis labels include Unicode text:</p>
<spanid="usetex-postscript"></span><h2>Postscript options<aclass="headerlink" href="#postscript-options" title="Permalink to this headline">¶</a></h2>
<p>In order to produce encapsulated postscript files that can be embedded in a new
LaTeX document, the default behavior of matplotlib is to distill the output,
which removes some postscript operators used by LaTeX that are illegal in an
eps file. This step produces results which may be unacceptable to some users,
because the text is coarsely rasterized and converted to bitmaps, which are not
scalable like standard postscript, and the text is not searchable. One
workaround is to set <codeclass="docutils literal"><spanclass="pre">ps.distiller.res</span></code> to a higher value (perhaps 6000)
in your rc settings, which will produce larger files but may look better and
scale reasonably. A better workaround, which requires <aclass="reference external" href="https://poppler.freedesktop.org/">Poppler</a> or <aclass="reference external" href="http://www.xpdfreader.com/">Xpdf</a>, can be
activated by changing the <codeclass="docutils literal"><spanclass="pre">ps.usedistiller</span></code> rc setting to <codeclass="docutils literal"><spanclass="pre">xpdf</span></code>. This
alternative produces postscript without rasterizing text, so it scales
properly, can be edited in Adobe Illustrator, and searched text in pdf
documents.</p>
</div>
<divclass="section" id="possible-hangups">
<spanid="usetex-hangups"></span><h2>Possible hangups<aclass="headerlink" href="#possible-hangups" title="Permalink to this headline">¶</a></h2>
<ulclass="simple">
<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"><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>Using MiKTeX with Computer Modern fonts, if you get odd *Agg and PNG
results, go to MiKTeX/Options and update your format files</li>
<li>On Ubuntu and Gentoo, the base texlive install does not ship with
the type1cm package. You may need to install some of the extra
packages to get all the goodies that come bundled with other latex
distributions.</li>
<li>Some progress has been made so matplotlib uses the dvi files
directly for text layout. This allows latex to be used for text
layout with the pdf and svg backends, as well as the *Agg and PS
backends. In the future, a latex installation may be the only
external dependency.</li>
</ul>
</div>
<divclass="section" id="troubleshooting">
<spanid="usetex-troubleshooting"></span><h2>Troubleshooting<aclass="headerlink" href="#troubleshooting" title="Permalink to this headline">¶</a></h2>
<ulclass="simple">
<li>Try deleting your <codeclass="file docutils literal"><spanclass="pre">.matplotlib/tex.cache</span></code> directory. If you don’t know
where to find <codeclass="file docutils literal"><spanclass="pre">.matplotlib</span></code>, see <aclass="reference internal" href="../../faq/troubleshooting_faq.html#locating-matplotlib-config-dir"><spanclass="std std-ref">matplotlib configuration and cache directory locations</span></a>.</li>
<li>Make sure LaTeX, dvipng and ghostscript are each working and on your
<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>Most problems reported on the mailing list have been cleared up by
upgrading <aclass="reference external" href="https://ghostscript.com/">Ghostscript</a>. If possible, please try upgrading to the
latest release before reporting problems to the list.</li>
<li>The <codeclass="docutils literal"><spanclass="pre">text.latex.preamble</span></code> rc setting is not officially supported. This
option provides lots of flexibility, and lots of ways to cause
problems. Please disable this option before reporting problems to
the mailing list.</li>
<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>
<p><strong>Total running time of the script:</strong> ( 0 minutes 0.000 seconds)</p>