Skip to content

Update/cleanup pgf tutorial. #16281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions tutorials/text/pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@

How to typeset text with the ``pgf`` backend in Matplotlib.

Using the ``pgf`` backend, matplotlib can export figures as pgf drawing
Using the ``pgf`` backend, Matplotlib can export figures as pgf drawing
commands that can be processed with pdflatex, xelatex or lualatex. XeLaTeX and
LuaLaTeX have full unicode support and can use any font that is installed in
LuaLaTeX have full Unicode support and can use any font that is installed in
the operating system, making use of advanced typographic features of OpenType,
AAT and Graphite. Pgf pictures created by ``plt.savefig('figure.pgf')``
can be embedded as raw commands in LaTeX documents. Figures can also be
directly compiled and saved to PDF with ``plt.savefig('figure.pdf')`` by either
switching to the backend

.. code-block:: python
directly compiled and saved to PDF with ``plt.savefig('figure.pdf')`` by
switching the backend ::

matplotlib.use('pgf')

or registering it for handling pdf output
or by explicitly requesting the use of the ``pgf`` backend ::

.. code-block:: python
plt.savefig('figure.pdf', backend='pgf')

or by registering it for handling pdf output ::

from matplotlib.backends.backend_pgf import FigureCanvasPgf
matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf)

The second method allows you to keep using regular interactive backends and to
The last method allows you to keep using regular interactive backends and to
save xelatex, lualatex or pdflatex compiled PDF files from the graphical user
interface.

Expand All @@ -35,7 +35,7 @@
figures can optionally be saved to PNG images as well. The executables
for all applications must be located on your :envvar:`PATH`.

Rc parameters that control the behavior of the pgf backend:
`.rcParams` that control the behavior of the pgf backend:

================= =====================================================
Parameter Documentation
Expand All @@ -61,7 +61,8 @@
Multi-Page PDF Files
====================

The pgf backend also supports multipage pdf files using ``PdfPages``
The pgf backend also supports multipage pdf files using
`~.backend_pgf.PdfPages`

.. code-block:: python

Expand All @@ -83,16 +84,16 @@
==================

The fonts used for obtaining the size of text elements or when compiling
figures to PDF are usually defined in the matplotlib rc parameters. You can
also use the LaTeX default Computer Modern fonts by clearing the lists for
``font.serif``, ``font.sans-serif`` or ``font.monospace``. Please note that the
glyph coverage of these fonts is very limited. If you want to keep the Computer
Modern font face but require extended unicode support, consider installing the
figures to PDF are usually defined in the `.rcParams`. You can also use the
LaTeX default Computer Modern fonts by clearing the lists for :rc:`font.serif`,
:rc:`font.sans-serif` or :rc:`font.monospace`. Please note that the glyph
coverage of these fonts is very limited. If you want to keep the Computer
Modern font face but require extended Unicode support, consider installing the
`Computer Modern Unicode`__ fonts *CMU Serif*, *CMU Sans Serif*, etc.

__ https://sourceforge.net/projects/cm-unicode/

When saving to ``.pgf``, the font configuration matplotlib used for the
When saving to ``.pgf``, the font configuration Matplotlib used for the
layout of the figure is included in the header of the text file.

.. literalinclude:: ../../gallery/userdemo/pgf_fonts.py
Expand All @@ -105,10 +106,10 @@
===============

Full customization is possible by adding your own commands to the preamble.
Use the ``pgf.preamble`` parameter if you want to configure the math fonts,
Use :rc:`pgf.preamble` if you want to configure the math fonts,
using ``unicode-math`` for example, or for loading additional packages. Also,
if you want to do the font configuration yourself instead of using the fonts
specified in the rc parameters, make sure to disable ``pgf.rcfonts``.
specified in the rc parameters, make sure to disable :rc:`pgf.rcfonts`.

.. only:: html

Expand All @@ -126,10 +127,10 @@
Choosing the TeX system
=======================

The TeX system to be used by matplotlib is chosen by the ``pgf.texsystem``
parameter. Possible values are ``'xelatex'`` (default), ``'lualatex'`` and
``'pdflatex'``. Please note that when selecting pdflatex the fonts and
unicode handling must be configured in the preamble.
The TeX system to be used by Matplotlib is chosen by :rc:`pgf.texsystem`.
Possible values are ``'xelatex'`` (default), ``'lualatex'`` and ``'pdflatex'``.
Please note that when selecting pdflatex, the fonts and Unicode handling must
be configured in the preamble.

.. literalinclude:: ../../gallery/userdemo/pgf_texsystem.py
:end-before: plt.savefig
Expand Down Expand Up @@ -162,7 +163,7 @@
that your LaTeX syntax is valid and that you are using raw strings
if necessary to avoid unintended escape sequences.

* The ``pgf.preamble`` rc setting provides lots of flexibility, and lots of
* :rc:`pgf.preamble` provides lots of flexibility, and lots of
ways to cause problems. When experiencing problems, try to minimalize or
disable the custom preamble.

Expand All @@ -176,10 +177,10 @@

__ http://tex.stackexchange.com/questions/43642

* If the font configuration used by matplotlib differs from the font setting
* If the font configuration used by Matplotlib differs from the font setting
in yout LaTeX document, the alignment of text elements in imported figures
may be off. Check the header of your ``.pgf`` file if you are unsure about
the fonts matplotlib used for the layout.
the fonts Matplotlib used for the layout.

* Vector images and hence ``.pgf`` files can become bloated if there are a lot
of objects in the graph. This can be the case for image processing or very
Expand Down