<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>Matplotlib can use LaTeX to render text. This is activated by setting
<codeclass="docutils literal notranslate"><spanclass="pre">text.usetex</span><spanclass="pre">:</span><spanclass="pre">True</span></code> in your rcParams, or by setting the <codeclass="docutils literal notranslate"><spanclass="pre">usetex</span></code> property
to True on individual <aclass="reference internal" href="../../api/text_api.html#matplotlib.text.Text" title="matplotlib.text.Text"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Text</span></code></a> objects. Text handling through LaTeX is slower
than Matplotlib's very capable <aclass="reference internal" href="mathtext.html"><spanclass="doc">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. For
the *Agg backends, <aclass="reference external" href="http://www.nongnu.org/dvipng/">dvipng</a> is additionally required; for the PS backend,
<aclass="reference external" href="https://ctan.org/pkg/psfrag">PSfrag</a>, <aclass="reference external" href="https://tug.org/texinfohtml/dvips.html">dvips</a> and <aclass="reference external" href="https://ghostscript.com/">Ghostscript</a> are additionally required. For the PDF
and SVG backends, if LuaTeX is present, it will be used to speed up some
post-processing steps, but note that it is not used to parse the TeX string
itself (only LaTeX is supported). The executables for these external
dependencies must all 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>There are a couple of options to mention, which can be changed using
<aclass="reference internal" href="../introductory/customizing.html"><spanclass="doc">rc settings</span></a>. Here is an example
<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
<aclass="reference internal" href="../../gallery/text_labels_and_annotations/tex_demo.html"><spanclass="doc">Rendering math equations using TeX</span></a>:</p>
<p>Note that display math mode (<codeclass="docutils literal notranslate"><spanclass="pre">$$</span><spanclass="pre">e=mc^2</span><spanclass="pre">$$</span></code>) is not supported, but adding the
command <codeclass="docutils literal notranslate"><spanclass="pre">\displaystyle</span></code>, as in the above demo, will produce the same results.</p>
<p>Non-ASCII characters (e.g. the degree sign in the y-label above) are supported
to the extent that they are supported by <aclass="reference external" href="https://ctan.org/pkg/inputenc">inputenc</a>.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>Certain characters require special escaping in TeX, such as:</p>
<h2>PostScript options<aclass="headerlink" href="#postscript-options" title="Permalink to this headline">¶</a></h2>
<p>In order to produce encapsulated PostScript (EPS) 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 notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=ps.distiller.res#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["ps.distiller.res"]</span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">6000</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 <codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=ps.usedistiller#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["ps.usedistiller"]</span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>) to <codeclass="docutils literal notranslate"><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>
</section>
<sectionid="possible-hangups">
<spanid="usetex-hangups"></span><h2>Possible hangups<aclass="headerlink" href="#possible-hangups" title="Permalink to this headline">¶</a></h2>
<ulclass="simple">
<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>Using MiKTeX with Computer Modern fonts, if you get odd *Agg and PNG
results, go to MiKTeX/Options and update your format files</p></li>
<li><p>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.</p></li>
<li><p>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.</p></li>
</ul>
</section>
<sectionid="troubleshooting">
<spanid="usetex-troubleshooting"></span><h2>Troubleshooting<aclass="headerlink" href="#troubleshooting" title="Permalink to this headline">¶</a></h2>
<ulclass="simple">
<li><p>Try deleting your <codeclass="file docutils literal notranslate"><spanclass="pre">.matplotlib/tex.cache</span></code> directory. If you don't know
where to find <codeclass="file docutils literal notranslate"><spanclass="pre">.matplotlib</span></code>, see <aclass="reference internal" href="../../users/faq/troubleshooting_faq.html#locating-matplotlib-config-dir"><spanclass="std std-ref">matplotlib configuration and cache directory locations</span></a>.</p></li>
<li><p>Make sure LaTeX, dvipng and ghostscript are each working and on your
<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=text.latex.preamble#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["text.latex.preamble"]</span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">''</span></code>) 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.</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-usetex-py">
<pclass="sphx-glr-signature">Keywords: matplotlib code example, codex, python plot, pyplot
<aclass="reference external" href="https://sphinx-gallery.readthedocs.io">Gallery generated by Sphinx-Gallery</a></p>
</section>
</section>
</div>
</main>
</div>
</div>
<!-- Scripts loaded after <body> so the DOM is not blocked -->
<divid="unreleased-message"> You are reading an old version of the documentation (v3.5.1). For the latest version see <ahref="https://matplotlib.org/stable/tutorials/text/usetex.html">https://matplotlib.org/stable/tutorials/text/usetex.html</a></div>