-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathusetex.html
More file actions
275 lines (252 loc) · 21.4 KB
/
usetex.html
File metadata and controls
275 lines (252 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Text rendering With LaTeX — Matplotlib 3.4.0 documentation</title>
<link rel="stylesheet" href="../../_static/mpl.css?v3.4.0-2-gcb03754703" type="text/css" />
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
<link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
<link rel="stylesheet" type="text/css" href="../../_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="../../_static/gallery.css" />
<link rel="stylesheet" type="text/css" href="../../_static/gallery-binder.css" />
<link rel="stylesheet" type="text/css" href="../../_static/gallery-dataframe.css" />
<script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
<script src="../../_static/jquery.js"></script>
<script src="../../_static/underscore.js"></script>
<script src="../../_static/doctools.js"></script>
<script src="../../_static/language_data.js"></script>
<script src="../../_static/clipboard.min.js"></script>
<script src="../../_static/copybutton.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Matplotlib 3.4.0 documentation"
href="../../_static/opensearch.xml"/>
<link rel="shortcut icon" href="../../_static/favicon.ico"/>
<link rel="index" title="Index" href="../../genindex.html" />
<link rel="search" title="Search" href="../../search.html" />
<link rel="next" title="Overview of axes_grid1 toolkit" href="../toolkits/axes_grid.html" />
<link rel="prev" title="Typesetting With XeLaTeX/LuaLaTeX" href="pgf.html" />
<link rel="top" title="Matplotlib 3.4.0 documentation" href="#" />
<link rel="canonical" href="https://matplotlib.org/stable/tutorials/text/usetex.html" />
<link rel="stylesheet" href="../../_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
<script data-domain="matplotlib.org" defer="defer" src="https://views.scientific-python.org/js/script.js"></script>
</head><body>
<div id="unreleased-message"> You are reading an old version of the documentation (v3.4.0). For the latest version see <a href="https://matplotlib.org/stable/tutorials/text/usetex.html">https://matplotlib.org/stable/tutorials/text/usetex.html</a></div>
<!--
<div id="annc-banner">
</div>
-->
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px; position: relative;">
<a href="../../index.html">
<div style="float: left; position: absolute; width: 496px; bottom: 0; padding-bottom: 24px"><span style="float: right; color: #789; background: white">Version 3.4.0</span></div>
<img src="../../_static/logo2_compressed.svg" height="125px" border="0" alt="matplotlib"/></a>
<!-- The "Fork me on github" ribbon -->
<div id="forkongithub"><a href="https://github.com/matplotlib/matplotlib">Fork me on GitHub</a></div>
</div>
<nav class="main-nav">
<ul>
<li><a href="../../users/installing.html">Installation</a></li>
<li><a href="../../contents.html">Documentation</a></li>
<li><a href="../../gallery/index.html">Examples</a></li>
<li><a href="../index.html">Tutorials</a></li>
<li><a href="../../devel/index.html">Contributing</a></li>
<li class="nav-right">
<form class="search" action="../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" placeholder="Search"/>
</form>
</li>
</ul>
</nav>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="../toolkits/axes_grid.html" title="Overview of axes_grid1 toolkit"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="pgf.html" title="Typesetting With XeLaTeX/LuaLaTeX"
accesskey="P">previous</a> |</li>
<li><a href="../../index.html">home</a>| </li>
<li><a href="../../contents.html">contents</a> »</li>
<li class="nav-item nav-item-1"><a href="../../users/index.html" >User's Guide</a> »</li>
<li class="nav-item nav-item-2"><a href="../index.html" accesskey="U">Tutorials</a> »</li>
<li class="nav-item nav-item-this"><a href="">Text rendering With LaTeX</a></li>
</ul>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../../contents.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Text rendering With LaTeX</a><ul>
<li><a class="reference internal" href="#postscript-options">PostScript options</a></li>
<li><a class="reference internal" href="#possible-hangups">Possible hangups</a></li>
<li><a class="reference internal" href="#troubleshooting">Troubleshooting</a></li>
</ul>
</li>
</ul>
<div id="sidebar-pagesource" role="note" aria-label="source link"
style="margin-top: 1.5em; padding-top: 0.1em; border-top: 1px solid #86989b">
<a href="../../_sources/tutorials/text/usetex.rst.txt"
style="color: #c0c0c0" rel="nofollow">Show Page Source</a>
</div>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="sphx-glr-download-link-note admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Click <a class="reference internal" href="#sphx-glr-download-tutorials-text-usetex-py"><span class="std std-ref">here</span></a> to download the full example code</p>
</div>
<div class="sphx-glr-example-title section" id="text-rendering-with-latex">
<span id="sphx-glr-tutorials-text-usetex-py"></span><h1>Text rendering With LaTeX<a class="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
<code class="docutils literal notranslate"><span class="pre">text.usetex</span> <span class="pre">:</span> <span class="pre">True</span></code> in your rcParams, or by setting the <code class="docutils literal notranslate"><span class="pre">usetex</span></code> property
to True on individual <a class="reference internal" href="../../api/text_api.html#matplotlib.text.Text" title="matplotlib.text.Text"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Text</span></code></a> objects. Text handling through LaTeX is slower
than Matplotlib's very capable <a class="reference internal" href="mathtext.html"><span class="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 <a class="reference external" href="http://www.tug.org">LaTeX</a> installation. For the
*Agg backends, <a class="reference external" href="http://www.nongnu.org/dvipng/">dvipng</a> is additionally required; for the PS backend, <a class="reference external" href="https://ctan.org/pkg/psfrag">psfrag</a>,
<a class="reference external" href="https://tug.org/texinfohtml/dvips.html">dvips</a> and <a class="reference external" href="https://ghostscript.com/">Ghostscript</a> are additionally required. The executables for these
external dependencies must all be located on your <span class="target" id="index-0"></span><a class="reference internal" href="../../faq/environment_variables_faq.html#envvar-PATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code></a>.</p>
<p>There are a couple of options to mention, which can be changed using
<a class="reference internal" href="../introductory/customizing.html"><span class="doc">rc settings</span></a>. Here is an example
matplotlibrc file:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">font</span><span class="o">.</span><span class="n">family</span> <span class="p">:</span> <span class="n">serif</span>
<span class="n">font</span><span class="o">.</span><span class="n">serif</span> <span class="p">:</span> <span class="n">Times</span><span class="p">,</span> <span class="n">Palatino</span><span class="p">,</span> <span class="n">New</span> <span class="n">Century</span> <span class="n">Schoolbook</span><span class="p">,</span> <span class="n">Bookman</span><span class="p">,</span> <span class="n">Computer</span> <span class="n">Modern</span> <span class="n">Roman</span>
<span class="n">font</span><span class="o">.</span><span class="n">sans</span><span class="o">-</span><span class="n">serif</span> <span class="p">:</span> <span class="n">Helvetica</span><span class="p">,</span> <span class="n">Avant</span> <span class="n">Garde</span><span class="p">,</span> <span class="n">Computer</span> <span class="n">Modern</span> <span class="n">Sans</span> <span class="n">Serif</span>
<span class="n">font</span><span class="o">.</span><span class="n">cursive</span> <span class="p">:</span> <span class="n">Zapf</span> <span class="n">Chancery</span>
<span class="n">font</span><span class="o">.</span><span class="n">monospace</span> <span class="p">:</span> <span class="n">Courier</span><span class="p">,</span> <span class="n">Computer</span> <span class="n">Modern</span> <span class="n">Typewriter</span>
<span class="n">text</span><span class="o">.</span><span class="n">usetex</span> <span class="p">:</span> <span class="n">true</span>
</pre></div>
</div>
<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 <a class="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
matplotlibrc use:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="n">plt</span><span class="o">.</span><span class="n">rcParams</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
<span class="s2">"text.usetex"</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
<span class="s2">"font.family"</span><span class="p">:</span> <span class="s2">"sans-serif"</span><span class="p">,</span>
<span class="s2">"font.sans-serif"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Helvetica"</span><span class="p">]})</span>
<span class="c1"># for Palatino and other serif fonts use:</span>
<span class="n">plt</span><span class="o">.</span><span class="n">rcParams</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
<span class="s2">"text.usetex"</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
<span class="s2">"font.family"</span><span class="p">:</span> <span class="s2">"serif"</span><span class="p">,</span>
<span class="s2">"font.serif"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Palatino"</span><span class="p">],</span>
<span class="p">})</span>
</pre></div>
</div>
<p>Here is the standard example,
<code class="file docutils literal notranslate"><span class="pre">/gallery/text_labels_and_annotations/tex_demo</span></code>:</p>
<div class="figure align-center">
<a class="reference external image-reference" href="../../gallery/text_labels_and_annotations/tex_demo.html"><img alt="../../_images/sphx_glr_tex_demo_0012.png" src="../../_images/sphx_glr_tex_demo_0012.png" style="width: 300.0px; height: 200.0px;" /></a>
</div>
<p>Note that display math mode (<code class="docutils literal notranslate"><span class="pre">$$</span> <span class="pre">e=mc^2</span> <span class="pre">$$</span></code>) is not supported, but adding the
command <code class="docutils literal notranslate"><span class="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 <a class="reference external" href="https://ctan.org/pkg/inputenc">inputenc</a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Certain characters require special escaping in TeX, such as:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># $ % & ~ _ ^ \ { } \( \) \[ \]</span>
</pre></div>
</div>
<p class="last">Therefore, these characters will behave differently depending on
<code class="docutils literal notranslate"><a class="reference external" href="../../tutorials/introductory/customizing.html?highlight=text.usetex#a-sample-matplotlibrc-file"><span class="pre">rcParams["text.usetex"]</span></a></code> (default: <code class="docutils literal notranslate"><span class="pre">False</span></code>).</p>
</div>
<div class="section" id="postscript-options">
<h2>PostScript options<a class="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 <code class="docutils literal notranslate"><a class="reference external" href="../../tutorials/introductory/customizing.html?highlight=ps.distiller.res#a-sample-matplotlibrc-file"><span class="pre">rcParams["ps.distiller.res"]</span></a></code> (default: <code class="docutils literal notranslate"><span class="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 <a class="reference external" href="https://poppler.freedesktop.org/">Poppler</a> or <a class="reference external" href="http://www.xpdfreader.com/">Xpdf</a>, can
be activated by changing <code class="docutils literal notranslate"><a class="reference external" href="../../tutorials/introductory/customizing.html?highlight=ps.usedistiller#a-sample-matplotlibrc-file"><span class="pre">rcParams["ps.usedistiller"]</span></a></code> (default: <code class="docutils literal notranslate"><span class="pre">None</span></code>) to <code class="docutils literal notranslate"><span class="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>
<div class="section" id="possible-hangups">
<span id="usetex-hangups"></span><h2>Possible hangups<a class="headerlink" href="#possible-hangups" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li>On Windows, the <span class="target" id="index-1"></span><a class="reference internal" href="../../faq/environment_variables_faq.html#envvar-PATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code></a> environment variable may need to be modified
to include the directories containing the latex, dvipng and ghostscript
executables. See <a class="reference internal" href="../../faq/environment_variables_faq.html#environment-variables"><span class="std std-ref">Environment Variables</span></a> and
<a class="reference internal" href="../../faq/environment_variables_faq.html#setting-windows-environment-variables"><span class="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>
<div class="section" id="troubleshooting">
<span id="usetex-troubleshooting"></span><h2>Troubleshooting<a class="headerlink" href="#troubleshooting" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li>Try deleting your <code class="file docutils literal notranslate"><span class="pre">.matplotlib/tex.cache</span></code> directory. If you don't know
where to find <code class="file docutils literal notranslate"><span class="pre">.matplotlib</span></code>, see <a class="reference internal" href="../../faq/troubleshooting_faq.html#locating-matplotlib-config-dir"><span class="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
<span class="target" id="index-2"></span><a class="reference internal" href="../../faq/environment_variables_faq.html#envvar-PATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code></a>.</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><code class="docutils literal notranslate"><a class="reference external" href="../../tutorials/introductory/customizing.html?highlight=text.latex.preamble#a-sample-matplotlibrc-file"><span class="pre">rcParams["text.latex.preamble"]</span></a></code> (default: <code class="docutils literal notranslate"><span class="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.</li>
<li>If you still need help, please see <a class="reference internal" href="../../faq/troubleshooting_faq.html#reporting-problems"><span class="std std-ref">Getting help</span></a></li>
</ul>
<div class="sphx-glr-footer class sphx-glr-footer-example docutils container" id="sphx-glr-download-tutorials-text-usetex-py">
<div class="sphx-glr-download sphx-glr-download-python docutils container">
<a class="reference download internal" download="" href="../../_downloads/0d107edc8c3e240a5bc4f7486b23afc1/usetex.py"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Python</span> <span class="pre">source</span> <span class="pre">code:</span> <span class="pre">usetex.py</span></code></a></div>
<div class="sphx-glr-download sphx-glr-download-jupyter docutils container">
<a class="reference download internal" download="" href="../../_downloads/67444aa8e727dcf1b494799f62393f05/usetex.ipynb"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Jupyter</span> <span class="pre">notebook:</span> <span class="pre">usetex.ipynb</span></code></a></div>
</div>
<p class="sphx-glr-signature">Keywords: matplotlib code example, codex, python plot, pyplot
<a class="reference external" href="https://sphinx-gallery.readthedocs.io">Gallery generated by Sphinx-Gallery</a></p>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<footer>
<div class="footer">
© Copyright 2002 - 2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the Matplotlib development team; 2012 - 2021 The Matplotlib development team.
<br />
Last updated on Mar 26, 2021.
Created using
<a href="http://sphinx-doc.org/">Sphinx</a> 3.2.1.
Doc version v3.4.0-2-gcb03754703.
</div>
</footer>
</body>
</html>