|
3 | 3 | Text rendering With LaTeX
|
4 | 4 | *************************
|
5 | 5 |
|
6 |
| -Rendering text with LaTeX in Matplotlib. |
7 |
| -
|
8 |
| -Matplotlib has the option to use LaTeX to manage all text layout. This |
9 |
| -option is available with the following backends: |
10 |
| -
|
11 |
| -* Agg |
12 |
| -* PS |
13 |
| -* PDF |
14 |
| -
|
15 |
| -The LaTeX option is activated by setting ``text.usetex : True`` in your rc |
16 |
| -settings. Text handling with matplotlib's LaTeX support is slower than |
17 |
| -matplotlib's very capable :doc:`mathtext </tutorials/text/mathtext>`, but is |
18 |
| -more flexible, since different LaTeX packages (font packages, math packages, |
| 6 | +Matplotlib can use LaTeX to render text. This is activated by setting |
| 7 | +``text.usetex : True`` in your rcParams, or by setting the ``usetex`` property |
| 8 | +to True on individual `.Text` objects. Text handling through LaTeX is slower |
| 9 | +than Matplotlib's very capable :doc:`mathtext </tutorials/text/mathtext>`, but |
| 10 | +is more flexible, since different LaTeX packages (font packages, math packages, |
19 | 11 | etc.) can be used. The results can be striking, especially when you take care
|
20 | 12 | to use the same fonts in your figures as in the main document.
|
21 | 13 |
|
22 |
| -Matplotlib's LaTeX support requires a working LaTeX_ installation, dvipng_ |
23 |
| -(which may be included with your LaTeX installation), and Ghostscript_ |
24 |
| -(GPL Ghostscript 9.0 or later is required). The executables for these |
| 14 | +Matplotlib's LaTeX support requires a working LaTeX_ installation. For the |
| 15 | +\*Agg backends, dvipng_ is additionally required; for the PS backend, psfrag_, |
| 16 | +dvips_ and Ghostscript_ are additionally required. The executables for these |
25 | 17 | external dependencies must all be located on your :envvar:`PATH`.
|
26 | 18 |
|
27 | 19 | There are a couple of options to mention, which can be changed using
|
|
30 | 22 |
|
31 | 23 | font.family : serif
|
32 | 24 | font.serif : Times, Palatino, New Century Schoolbook, Bookman, Computer Modern Roman
|
33 |
| - font.sans-serif : Helvetica, Avant Garde, Computer Modern Sans serif |
| 25 | + font.sans-serif : Helvetica, Avant Garde, Computer Modern Sans Serif |
34 | 26 | font.cursive : Zapf Chancery
|
35 | 27 | font.monospace : Courier, Computer Modern Typewriter
|
36 | 28 |
|
|
50 | 42 | "text.usetex": True,
|
51 | 43 | "font.family": "sans-serif",
|
52 | 44 | "font.sans-serif": ["Helvetica"]})
|
53 |
| - ## for Palatino and other serif fonts use: |
| 45 | + # for Palatino and other serif fonts use: |
54 | 46 | plt.rcParams.update({
|
55 | 47 | "text.usetex": True,
|
56 | 48 | "font.family": "serif",
|
|
65 | 57 | :align: center
|
66 | 58 | :scale: 50
|
67 | 59 |
|
68 |
| - TeX Demo |
69 |
| -
|
70 | 60 | Note that display math mode (``$$ e=mc^2 $$``) is not supported, but adding the
|
71 | 61 | command ``\displaystyle``, as in the above demo, will produce the same results.
|
72 | 62 |
|
| 63 | +Non-ASCII characters (e.g. the degree sign in the y-label above) are supported |
| 64 | +to the extent that they are supported by inputenc_. |
| 65 | +
|
73 | 66 | .. note::
|
74 | 67 | Certain characters require special escaping in TeX, such as::
|
75 | 68 |
|
|
78 | 71 | Therefore, these characters will behave differently depending on
|
79 | 72 | :rc:`text.usetex`.
|
80 | 73 |
|
81 |
| -.. _usetex-unicode: |
82 |
| -
|
83 |
| -usetex with unicode |
84 |
| -=================== |
85 |
| -
|
86 |
| -It is also possible to use unicode strings with the LaTeX text manager, here is |
87 |
| -an example taken from :file:`/gallery/text_labels_and_annotations/tex_demo`. |
88 |
| -The axis labels include Unicode text: |
89 |
| -
|
90 |
| -.. figure:: ../../gallery/text_labels_and_annotations/images/sphx_glr_tex_demo_001.png |
91 |
| - :target: ../../gallery/text_labels_and_annotations/tex_demo.html |
92 |
| - :align: center |
93 |
| - :scale: 50 |
94 |
| -
|
95 |
| - TeX Unicode Demo |
96 |
| -
|
97 |
| -.. _usetex-postscript: |
98 |
| -
|
99 |
| -Postscript options |
| 74 | +PostScript options |
100 | 75 | ==================
|
101 | 76 |
|
102 |
| -In order to produce encapsulated postscript files that can be embedded in a new |
103 |
| -LaTeX document, the default behavior of matplotlib is to distill the output, |
104 |
| -which removes some postscript operators used by LaTeX that are illegal in an |
105 |
| -eps file. This step produces results which may be unacceptable to some users, |
106 |
| -because the text is coarsely rasterized and converted to bitmaps, which are not |
107 |
| -scalable like standard postscript, and the text is not searchable. One |
108 |
| -workaround is to set ``ps.distiller.res`` to a higher value (perhaps 6000) |
| 77 | +In order to produce encapsulated PostScript (EPS) files that can be embedded |
| 78 | +in a new LaTeX document, the default behavior of Matplotlib is to distill the |
| 79 | +output, which removes some PostScript operators used by LaTeX that are illegal |
| 80 | +in an EPS file. This step produces results which may be unacceptable to some |
| 81 | +users, because the text is coarsely rasterized and converted to bitmaps, which |
| 82 | +are not scalable like standard PostScript, and the text is not searchable. One |
| 83 | +workaround is to set :rc:`ps.distiller.res` to a higher value (perhaps 6000) |
109 | 84 | in your rc settings, which will produce larger files but may look better and
|
110 |
| -scale reasonably. A better workaround, which requires Poppler_ or Xpdf_, can be |
111 |
| -activated by changing the ``ps.usedistiller`` rc setting to ``xpdf``. This |
112 |
| -alternative produces postscript without rasterizing text, so it scales |
113 |
| -properly, can be edited in Adobe Illustrator, and searched text in pdf |
114 |
| -documents. |
| 85 | +scale reasonably. A better workaround, which requires Poppler_ or Xpdf_, can |
| 86 | +be activated by changing :rc:`ps.usedistiller` to ``xpdf``. This alternative |
| 87 | +produces PostScript without rasterizing text, so it scales properly, can be |
| 88 | +edited in Adobe Illustrator, and searched text in pdf documents. |
115 | 89 |
|
116 | 90 | .. _usetex-hangups:
|
117 | 91 |
|
|
152 | 126 | that your LaTeX syntax is valid and that you are using raw strings
|
153 | 127 | if necessary to avoid unintended escape sequences.
|
154 | 128 |
|
155 |
| -* Most problems reported on the mailing list have been cleared up by |
156 |
| - upgrading Ghostscript_. If possible, please try upgrading to the |
157 |
| - latest release before reporting problems to the list. |
158 |
| -
|
159 |
| -* The ``text.latex.preamble`` rc setting is not officially supported. This |
| 129 | +* :rc:`text.latex.preamble` is not officially supported. This |
160 | 130 | option provides lots of flexibility, and lots of ways to cause
|
161 | 131 | problems. Please disable this option before reporting problems to
|
162 | 132 | the mailing list.
|
163 | 133 |
|
164 | 134 | * If you still need help, please see :ref:`reporting-problems`
|
165 | 135 |
|
166 |
| -.. _LaTeX: http://www.tug.org |
167 | 136 | .. _dvipng: http://www.nongnu.org/dvipng/
|
| 137 | +.. _dvips: https://tug.org/texinfohtml/dvips.html |
168 | 138 | .. _Ghostscript: https://ghostscript.com/
|
169 |
| -.. _PSNFSS: http://www.ctan.org/tex-archive/macros/latex/required/psnfss/psnfss2e.pdf |
| 139 | +.. _inputenc: https://ctan.org/pkg/inputenc |
| 140 | +.. _LaTeX: http://www.tug.org |
170 | 141 | .. _Poppler: https://poppler.freedesktop.org/
|
| 142 | +.. _PSNFSS: http://www.ctan.org/tex-archive/macros/latex/required/psnfss/psnfss2e.pdf |
| 143 | +.. _psfrag: https://ctan.org/pkg/psfrag |
171 | 144 | .. _Xpdf: http://www.xpdfreader.com/
|
172 | 145 | """
|
0 commit comments