{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n.. redirect-from:: /tutorials/text/usetex\n\n\n# Text rendering with LaTeX\n\nMatplotlib can use LaTeX to render text. This is activated by setting\n``text.usetex : True`` in your rcParams, or by setting the ``usetex`` property\nto True on individual `.Text` objects. Text handling through LaTeX is slower\nthan Matplotlib's very capable `mathtext `, but\nis more flexible, since different LaTeX packages (font packages, math packages,\netc.) can be used. The results can be striking, especially when you take care\nto use the same fonts in your figures as in the main document.\n\nMatplotlib's LaTeX support requires a working LaTeX_ installation. For\nthe \\*Agg backends, dvipng_ is additionally required; for the PS backend,\nPSfrag_, dvips_ and Ghostscript_ are additionally required. For the PDF\nand SVG backends, if LuaTeX is present, it will be used to speed up some\npost-processing steps, but note that it is not used to parse the TeX string\nitself (only LaTeX is supported). The executables for these external\ndependencies must all be located on your :envvar:`PATH`.\n\nOnly a small number of font families (defined by the PSNFSS_ scheme) are\nsupported. They are listed here, with the corresponding LaTeX font selection\ncommands and LaTeX packages, which are automatically used.\n\n=========================== =================================================\ngeneric family fonts\n=========================== =================================================\nserif (``\\rmfamily``) Computer Modern Roman, Palatino (``mathpazo``),\n Times (``mathptmx``), Bookman (``bookman``),\n New Century Schoolbook (``newcent``),\n Charter (``charter``)\n\nsans-serif (``\\sffamily``) Computer Modern Serif, Helvetica (``helvet``),\n Avant Garde (``avant``)\n\ncursive (``\\rmfamily``) Zapf Chancery (``chancery``)\n\nmonospace (``\\ttfamily``) Computer Modern Typewriter, Courier (``courier``)\n=========================== =================================================\n\nThe default font family (which does not require loading any LaTeX package) is\nComputer Modern. All other families are Adobe fonts. Times and Palatino each\nhave their own accompanying math fonts, while the other Adobe serif fonts make\nuse of the Computer Modern math fonts.\n\nTo enable LaTeX and select a font, use e.g.::\n\n plt.rcParams.update({\n \"text.usetex\": True,\n \"font.family\": \"Helvetica\"\n })\n\nor equivalently, set your `matplotlibrc ` to::\n\n text.usetex : true\n font.family : Helvetica\n\nIt is also possible to instead set ``font.family`` to one of the generic family\nnames and then configure the corresponding generic family; e.g.::\n\n plt.rcParams.update({\n \"text.usetex\": True,\n \"font.family\": \"sans-serif\",\n \"font.sans-serif\": \"Helvetica\",\n })\n\n(this was the required approach until Matplotlib 3.5).\n\nHere is the standard example,\n:doc:`/gallery/text_labels_and_annotations/tex_demo`:\n\n.. figure:: /gallery/text_labels_and_annotations/images/sphx_glr_tex_demo_001.png\n :target: /gallery/text_labels_and_annotations/tex_demo.html\n :align: center\n\nNote that display math mode (``$$ e=mc^2 $$``) is not supported, but adding the\ncommand ``\\displaystyle``, as in the above demo, will produce the same results.\n\nNon-ASCII characters (e.g. the degree sign in the y-label above) are supported\nto the extent that they are supported by inputenc_.\n\n

Note

For consistency with the non-usetex case, Matplotlib special-cases newlines,\n so that single-newlines yield linebreaks (rather than being interpreted as\n whitespace in standard LaTeX).\n\n Matplotlib uses the underscore_ package so that underscores (``_``) are\n printed \"as-is\" in text mode (rather than causing an error as in standard\n LaTeX). Underscores still introduce subscripts in math mode.

\n\n

Note

Certain characters require special escaping in TeX, such as::\n\n # $ % & ~ ^ \\ { } \\( \\) \\[ \\]\n\n Therefore, these characters will behave differently depending on\n :rc:`text.usetex`. As noted above, underscores (``_``) do not require\n escaping outside of math mode.

\n\n

Note

LaTeX always defaults to using a serif font for math (even when\n ``rcParams[\"font.family\"] = \"sans-serif\"``). If desired, adding\n ``\\usepackage{sfmath}`` to ``rcParams[\"text.latex.preamble\"]`` lets LaTeX\n output sans-serif math.

\n\n## PostScript options\n\nIn order to produce encapsulated PostScript (EPS) files that can be embedded\nin a new LaTeX document, the default behavior of Matplotlib is to distill the\noutput, which removes some PostScript operators used by LaTeX that are illegal\nin an EPS file. This step produces results which may be unacceptable to some\nusers, because the text is coarsely rasterized and converted to bitmaps, which\nare not scalable like standard PostScript, and the text is not searchable. One\nworkaround is to set :rc:`ps.distiller.res` to a higher value (perhaps 6000)\nin your rc settings, which will produce larger files but may look better and\nscale reasonably. A better workaround, which requires Poppler_ or Xpdf_, can\nbe activated by changing :rc:`ps.usedistiller` to ``xpdf``. This alternative\nproduces PostScript without rasterizing text, so it scales properly, can be\nedited in Adobe Illustrator, and searched text in pdf documents.\n\n\n## Possible hangups\n\n* On Windows, the :envvar:`PATH` environment variable may need to be modified\n to include the directories containing the latex, dvipng and ghostscript\n executables. See `environment-variables` and\n `setting-windows-environment-variables` for details.\n\n* Using MiKTeX with Computer Modern fonts, if you get odd \\*Agg and PNG\n results, go to MiKTeX/Options and update your format files.\n\n* On Ubuntu and Gentoo, the base texlive install does not ship with\n the type1cm package. You may need to install some of the extra\n packages to get all the goodies that come bundled with other LaTeX\n distributions.\n\n\n## Troubleshooting\n\n* Try deleting your :file:`.matplotlib/tex.cache` directory. If you don't know\n where to find :file:`.matplotlib`, see `locating-matplotlib-config-dir`.\n\n* Make sure LaTeX, dvipng and Ghostscript are each working and on your\n :envvar:`PATH`.\n\n* Make sure what you are trying to do is possible in a LaTeX document,\n that your LaTeX syntax is valid and that you are using raw strings\n if necessary to avoid unintended escape sequences.\n\n* :rc:`text.latex.preamble` is not officially supported. This\n option provides lots of flexibility, and lots of ways to cause\n problems. Please disable this option before reporting problems.\n\n* If you still need help, please see `reporting-problems`.\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }