|
| 1 | +.. _pgf-tutorial: |
| 2 | + |
| 3 | +********************************* |
| 4 | +Typesetting With XeLaTeX/LuaLaTeX |
| 5 | +********************************* |
| 6 | + |
| 7 | +Using the ``pgf`` backend, matplotlib can export figures as pgf drawing commands |
| 8 | +that can be processed with pdflatex, xelatex or lualatex. XeLaTeX and LuaLaTeX |
| 9 | +have full unicode support and can use any fonts installed in the operating |
| 10 | +system, making use of advanced typographic features of OpenType, AAT and |
| 11 | +Graphite. Pgf pictures created by ``plt.savefig('figure.pgf')`` can be |
| 12 | +embedded as raw commands in LaTeX documents. Figures can also be directly |
| 13 | +compiled and saved to PDF with ``plt.savefig('figure.pdf')``. |
| 14 | + |
| 15 | +Matplotlib's pgf support requires a working LaTeX_ installation (such as |
| 16 | +TeXLive_), preferably including XeLaTeX or LuaLaTeX. If pdftocairo or |
| 17 | +ghostscript is installed, figures can optionally be saved to PNG images. |
| 18 | +The executables for all applications must be located on your :envvar:`PATH`. |
| 19 | + |
| 20 | +Rc parameters that control the behavior of the pgf backend: |
| 21 | + |
| 22 | + ================= ===================================================== |
| 23 | + Parameter Documentation |
| 24 | + ================= ===================================================== |
| 25 | + pgf.preamble Lines to be included in the LaTeX preamble |
| 26 | + pgf.rcfonts Setup fonts from rc params using the fontspec package |
| 27 | + pgf.texsystem Either "xelatex", "lualatex" or "pdflatex" |
| 28 | + ================= ===================================================== |
| 29 | + |
| 30 | +.. note:: |
| 31 | + |
| 32 | + TeX defines a set of secial characters, such as:: |
| 33 | + |
| 34 | + # $ % & ~ _ ^ \ { } |
| 35 | + |
| 36 | + Generally, these characters must be escaped correctly. For convenience, |
| 37 | + some characters (_,^,%) are automatically escaped outside of math |
| 38 | + environments. |
| 39 | + |
| 40 | +.. _pgf-rcfonts: |
| 41 | + |
| 42 | +Font specification |
| 43 | +================== |
| 44 | + |
| 45 | +The fonts used for obtaining the size of text elements or when compiling |
| 46 | +figures to PDF are usually defined in the matplotlib rc parameters. You can |
| 47 | +also use the LaTeX default Computer Modern fonts by clearing the lists for |
| 48 | +``font.serif``, ``font.sans-serif`` or ``font.monospace``. Please note that |
| 49 | +the glyph coverage of these fonts is very limited. For extended unicode support |
| 50 | +the `Computer Modern Unicode <http://sourceforge.net/projects/cm-unicode/>`_ |
| 51 | +fonts "CMU Serif", "CMU Sans Serif" are recommended. |
| 52 | + |
| 53 | +.. literalinclude:: plotting/examples/pgf_fonts.py |
| 54 | + :end-before: plt.savefig |
| 55 | + |
| 56 | +.. image:: /_static/pgf_fonts.* |
| 57 | + |
| 58 | + |
| 59 | +.. _pgf-preamble: |
| 60 | + |
| 61 | +Custom preamble |
| 62 | +=============== |
| 63 | + |
| 64 | +Full customization is possible by adding your own commands to the preamble. |
| 65 | +Use the ``pgf.preamble`` parameter if you want to configure the math fonts or |
| 66 | +for loading additional packages. Also, if you want to do the font configuration |
| 67 | +yourself instead of using the fonts specified in the rc parameters, make sure |
| 68 | +to disable ``pgf.rcfonts``. |
| 69 | + |
| 70 | +.. htmlonly:: |
| 71 | + |
| 72 | + .. literalinclude:: plotting/examples/pgf_preamble.py |
| 73 | + :end-before: plt.savefig |
| 74 | + |
| 75 | +.. latexonly:: |
| 76 | + |
| 77 | + .. literalinclude:: plotting/examples/pgf_preamble.py |
| 78 | + :end-before: import matplotlib.pyplot as plt |
| 79 | + |
| 80 | +.. image:: /_static/pgf_preamble.* |
| 81 | + |
| 82 | + |
| 83 | +.. _pgf-texsystem: |
| 84 | + |
| 85 | +Choosing the TeX system |
| 86 | +======================= |
| 87 | + |
| 88 | +The TeX system to be used by matplotlib is chosen by the ``pgf.texsystem`` |
| 89 | +parameter. Possible values are ``'xelatex'`` (default), ``'lualatex'`` and |
| 90 | +``'pdflatex'``. Please note that when selecting pdflatex the fonts and |
| 91 | +unicode handling must be configured in the preamble. |
| 92 | + |
| 93 | +.. literalinclude:: plotting/examples/pgf_texsystem.py |
| 94 | + :end-before: plt.savefig |
| 95 | + |
| 96 | +.. image:: /_static/pgf_texsystem.* |
| 97 | + |
| 98 | +.. _pgf-hangups: |
| 99 | + |
| 100 | +Possible hangups |
| 101 | +================ |
| 102 | + |
| 103 | +* On Windows, the :envvar:`PATH` environment variable may need to be modified |
| 104 | + to include the directories containing the latex, dvipng and ghostscript |
| 105 | + executables. See :ref:`environment-variables` and |
| 106 | + :ref:`setting-windows-environment-variables` for details. |
| 107 | + |
| 108 | +* Sometimes the font rendering in figures that are saved to png images is |
| 109 | + very bad. This happens when the pdftocairo tool is not available and |
| 110 | + ghostscript is used for the pdf to png conversion. |
| 111 | + |
| 112 | +.. _pgf-troubleshooting: |
| 113 | + |
| 114 | +Troubleshooting |
| 115 | +=============== |
| 116 | + |
| 117 | +* Make sure what you are trying to do is possible in a LaTeX document, |
| 118 | + that your LaTeX syntax is valid and that you are using raw strings |
| 119 | + if necessary to avoid unintended escape sequences. |
| 120 | + |
| 121 | +* The ``pgf.preamble`` rc setting provides lots of flexibility, and lots of |
| 122 | + ways to cause problems. When experiencing problems, try to minimalize or |
| 123 | + disable the custom preamble before reporting problems. |
| 124 | + |
| 125 | +* If you still need help, please see :ref:`reporting-problems` |
| 126 | + |
| 127 | +.. _LaTeX: http://www.tug.org |
| 128 | +.. _TeXLive: http://www.tug.org/texlive/ |
0 commit comments