{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n.. redirect-from:: /tutorials/text/pgf\n\n\n# Text rendering with XeLaTeX/LuaLaTeX via the ``pgf`` backend\n\nUsing the ``pgf`` backend, Matplotlib can export figures as pgf drawing\ncommands that can be processed with pdflatex, xelatex or lualatex. XeLaTeX and\nLuaLaTeX have full Unicode support and can use any font that is installed in\nthe operating system, making use of advanced typographic features of OpenType,\nAAT and Graphite. Pgf pictures created by ``plt.savefig('figure.pgf')``\ncan be embedded as raw commands in LaTeX documents. Figures can also be\ndirectly compiled and saved to PDF with ``plt.savefig('figure.pdf')`` by\nswitching the backend ::\n\n matplotlib.use('pgf')\n\nor by explicitly requesting the use of the ``pgf`` backend ::\n\n plt.savefig('figure.pdf', backend='pgf')\n\nor by registering it for handling pdf output ::\n\n from matplotlib.backends.backend_pgf import FigureCanvasPgf\n matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf)\n\nThe last method allows you to keep using regular interactive backends and to\nsave xelatex, lualatex or pdflatex compiled PDF files from the graphical user\ninterface. Note that, in that case, the interactive display will still use the\nstandard interactive backends (e.g., QtAgg), and in particular use latex to\ncompile relevant text snippets.\n\nMatplotlib's pgf support requires a recent LaTeX_ installation that includes\nthe TikZ/PGF packages (such as TeXLive_), preferably with XeLaTeX or LuaLaTeX\ninstalled. If either pdftocairo or ghostscript is present on your system,\nfigures can optionally be saved to PNG images as well. The executables\nfor all applications must be located on your :envvar:`PATH`.\n\n`.rcParams` that control the behavior of the pgf backend:\n\n================= =====================================================\nParameter Documentation\n================= =====================================================\npgf.preamble Lines to be included in the LaTeX preamble\npgf.rcfonts Setup fonts from rc params using the fontspec package\npgf.texsystem Either \"xelatex\" (default), \"lualatex\" or \"pdflatex\"\n================= =====================================================\n\n
TeX defines a set of special characters, such as::\n\n # $ % & ~ _ ^ \\ { }\n\n Generally, these characters must be escaped correctly. For convenience,\n some characters (_, ^, %) are automatically escaped outside of math\n environments. Other characters are not escaped as they are commonly needed\n in actual TeX expressions. However, one can configure TeX to treat them as\n \"normal\" characters (known as \"catcode 12\" to TeX) via a custom preamble,\n such as::\n\n plt.rcParams[\"pgf.preamble\"] = (\n r\"\\AtBeginDocument{\\catcode`\\&=12\\catcode`\\#=12}\")