|
| 1 | +# XeLaTeX/LuaLaTeX 设置 |
| 2 | + |
| 3 | +> 原文:[Typesetting With XeLaTeX/LuaLaTeX](http://matplotlib.org/users/pgf.html) |
| 4 | +
|
| 5 | +> 译者:[飞龙](https://github.com/) |
| 6 | +
|
| 7 | +> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/) |
| 8 | +
|
| 9 | +使用 pgf 后端,matplotlib 可以将图形导出为可以使用 pdflatex,xelatex 或 lualatex 处理的 pgf 绘图命令。 XeLaTeX 和 LuaLaTeX 具有完整的 unicode 支持,可以使用安装在操作系统中的任何字体,利用 OpenType,AAT 和 Graphite 的高级排版功能。 由`plt.savefig('figure.pgf')`创建的 Pgf 图片可以作为原始命令嵌入到 LaTeX 文档中。 图形也可以通过切换到该后端,直接编译并使用`plt.savefig('figure.pdf')`保存到 PDF。 |
| 10 | + |
| 11 | +```py |
| 12 | +matplotlib.use('pgf') |
| 13 | +``` |
| 14 | + |
| 15 | +或者为处理 PDF 输出而注册它: |
| 16 | + |
| 17 | +```py |
| 18 | +from matplotlib.backends.backend_pgf import FigureCanvasPgf |
| 19 | +matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf) |
| 20 | +``` |
| 21 | + |
| 22 | +第二种方法允许你继续使用常规的交互式后端,并从图形用户界面保存 xelatex,lualatex 或 pdflatex 编译的 PDF 文件。 |
| 23 | + |
| 24 | +Matplotlib 的 pgf 支持需要最新的 [LaTeX](http://www.tug.org/) 安装,包括 TikZ/PGF 软件包(如 [TeXLive](http://www.tug.org/texlive/)),最好安装 XeLaTeX 或 LuaLaTeX。 如果你的系统上存在 pdftocairo 或 ghostscript,也可以选择将图形保存为 PNG 图像。 所有应用程序的可执行文件必须位于[`PATH`](http://matplotlib.org/faq/environment_variables_faq.html#envvar-PATH)中。 |
| 25 | + |
| 26 | +控制 pgf 后端行为的 Rc 参数: |
| 27 | + |
| 28 | +| 参数 | 文档 | |
| 29 | +| --- | --- | |
| 30 | +| `pgf.preamble `| 包含在 LaTeX 序言中的行 | |
| 31 | +| `pgf.rcfonts` | 使用 fontspec 软件包从 rc 参数设置字体 | |
| 32 | +| `pgf.texsystem` | `xelatex`(默认),`lualatex`或者`pdflatex` | |
| 33 | + |
| 34 | +> 注意 |
| 35 | +
|
| 36 | +> TeX 定义了一系列特殊字符,例如: |
| 37 | +
|
| 38 | +> ``` |
| 39 | +> # $ % & ~ _ ^ \ { } |
| 40 | +> ``` |
| 41 | +
|
| 42 | +> 通常,这些字符必须正确转义。一些字符(`_`,`^`,`%`)会自动在数学环境之外转义。 |
| 43 | +
|
| 44 | +## 字体规定 |
| 45 | +
|
| 46 | +用于获取文本元素大小,或将图形编译为 PDF 的字体通常在 matplotlib rc 参数中定义。 你还可以通过清除`font.serif`,`font.sans-serif`或`font.monospace`的列表来使用 LaTeX 默认的 Computer Modern 字体。 请注意,这些字体的字形覆盖范围非常有限。 如果要保留 Computer Modern 字体,但需要扩展 Unicode 编码支持,请考虑安装 [Computer Modern Unicode](https://sourceforge.net/projects/cm-unicode/) 字体 CMU Serif,CMU Sans Serif 等。 |
| 47 | +
|
| 48 | +保存到`.pgf`时,matplotlib 用于图形布局的字体配置包含在文本文件的标题中。 |
| 49 | +
|
| 50 | +```py |
| 51 | +# -*- coding: utf-8 -*- |
| 52 | +
|
| 53 | +import matplotlib as mpl |
| 54 | +mpl.use("pgf") |
| 55 | +pgf_with_rc_fonts = { |
| 56 | + "font.family": "serif", |
| 57 | + "font.serif": [], # use latex default serif font |
| 58 | + "font.sans-serif": ["DejaVu Sans"], # use a specific sans-serif font |
| 59 | +} |
| 60 | +mpl.rcParams.update(pgf_with_rc_fonts) |
| 61 | +
|
| 62 | +import matplotlib.pyplot as plt |
| 63 | +plt.figure(figsize=(4.5,2.5)) |
| 64 | +plt.plot(range(5)) |
| 65 | +plt.text(0.5, 3., "serif") |
| 66 | +plt.text(0.5, 2., "monospace", family="monospace") |
| 67 | +plt.text(2.5, 2., "sans-serif", family="sans-serif") |
| 68 | +plt.text(2.5, 1., "comic sans", family="Comic Sans MS") |
| 69 | +plt.xlabel(u"μ is not $\\mu$") |
| 70 | +plt.tight_layout(.5) |
| 71 | +``` |
| 72 | +
|
| 73 | + |
| 74 | + |
| 75 | +## 自定义序言 |
| 76 | + |
| 77 | +通过将你的命令添加到序言中,你可以完全自定义它。 如果要配置数学字体(例如使用 unicode-math)或加载其他软件包,请使用`pgf.preamble`参数。 此外,如果你想自己做字体配置,而不是使用 rc 参数中指定的字体,请确保禁用`pgf.rcfonts`。 |
| 78 | + |
| 79 | +```py |
| 80 | +# -*- coding: utf-8 -*- |
| 81 | +from __future__ import (absolute_import, division, print_function, |
| 82 | + unicode_literals) |
| 83 | + |
| 84 | +import six |
| 85 | + |
| 86 | +import matplotlib as mpl |
| 87 | +mpl.use("pgf") |
| 88 | +pgf_with_custom_preamble = { |
| 89 | + "font.family": "serif", # use serif/main font for text elements |
| 90 | + "text.usetex": True, # use inline math for ticks |
| 91 | + "pgf.rcfonts": False, # don't setup fonts from rc parameters |
| 92 | + "pgf.preamble": [ |
| 93 | + "\\usepackage{units}", # load additional packages |
| 94 | + "\\usepackage{metalogo}", |
| 95 | + "\\usepackage{unicode-math}", # unicode math setup |
| 96 | + r"\setmathfont{xits-math.otf}", |
| 97 | + r"\setmainfont{DejaVu Serif}", # serif font via preamble |
| 98 | + ] |
| 99 | +} |
| 100 | +mpl.rcParams.update(pgf_with_custom_preamble) |
| 101 | + |
| 102 | +import matplotlib.pyplot as plt |
| 103 | +plt.figure(figsize=(4.5,2.5)) |
| 104 | +plt.plot(range(5)) |
| 105 | +plt.xlabel("unicode text: я, ψ, €, ü, \\unitfrac[10]{°}{μm}") |
| 106 | +plt.ylabel("\\XeLaTeX") |
| 107 | +plt.legend(["unicode math: $λ=∑_i^∞ μ_i^2$"]) |
| 108 | +plt.tight_layout(.5) |
| 109 | +``` |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +## 选择 TeX 系统 |
| 114 | + |
| 115 | +matplotlib 使用的 TeX 系统由`pgf.texsystem`参数选择。 可能的值为`xelatex`(默认值),`lualatex`和`pdflatex`。 请注意,当选择`pdflatex`时,必须在序言中配置字体和 unicode 处理。 |
| 116 | + |
| 117 | +```py |
| 118 | +# -*- coding: utf-8 -*- |
| 119 | + |
| 120 | +import matplotlib as mpl |
| 121 | +mpl.use("pgf") |
| 122 | +pgf_with_pdflatex = { |
| 123 | + "pgf.texsystem": "pdflatex", |
| 124 | + "pgf.preamble": [ |
| 125 | + r"\usepackage[utf8x]{inputenc}", |
| 126 | + r"\usepackage[T1]{fontenc}", |
| 127 | + r"\usepackage{cmbright}", |
| 128 | + ] |
| 129 | +} |
| 130 | +mpl.rcParams.update(pgf_with_pdflatex) |
| 131 | + |
| 132 | +import matplotlib.pyplot as plt |
| 133 | +plt.figure(figsize=(4.5,2.5)) |
| 134 | +plt.plot(range(5)) |
| 135 | +plt.text(0.5, 3., "serif", family="serif") |
| 136 | +plt.text(0.5, 2., "monospace", family="monospace") |
| 137 | +plt.text(2.5, 2., "sans-serif", family="sans-serif") |
| 138 | +plt.xlabel(u"μ is not $\\mu$") |
| 139 | +plt.tight_layout(.5) |
| 140 | +``` |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +## 故障排除 |
| 145 | + |
| 146 | +请注意,在一些 Linux 发行版和 MiKTeX 安装中发现的 TeX 包已经过时了。确保更新你的软件包目录并升级或安装最新的 TeX 发行版。 |
| 147 | +在 Windows 上,可能需要修改[`PATH`](http://matplotlib.org/faq/environment_variables_faq.html#envvar-PATH)环境变量来包含 latex,dvipng 和 ghostscript 可执行文件的目录。详细信息请参阅[环境变量](http://matplotlib.org/faq/environment_variables_faq.html#environment-variables)和[在窗口中设置环境变量](http://matplotlib.org/faq/environment_variables_faq.html#setting-windows-environment-variables)。 |
| 148 | +Windows 上的限制会导致后端保留由应用程序打开的文件句柄。因此,可能无法删除相应的文件,直到应用程序关闭(参见[`#1324`](https://github.com/matplotlib/matplotlib/issues/1324))。 |
| 149 | +有时保存到 png 图像的图形中的字体非常糟糕。这在 pdftocairo 工具不可用,并且 ghostscript 用于 pdf 到 png 的转换时发生。 |
| 150 | +确保你想要做的事情在 LaTeX 文档中可实现,你的 LaTeX 语法是有效的,并且你正在使用原始字符串,如果必要的话,避免意外的转义序列。 |
| 151 | +`pgf.preamble rc`设置提供了大量的灵活性,以及导致问题的许多方法。遇到问题时,尝试最小化或禁用自定义序言。 |
| 152 | +配置 unicode-math 环境可能有点棘手。例如 TeXLive 分发版提供了一组通常不在系统范围内安装的数学字体。与 LuaLatex 不同的是,XeTeX 不能根据名字找到这些字体,这就是你可能必须指定`\setmathfont{xits-math.otf}`,而不是`\setmathfont{XITS Math}`的原因,或者使字体可用于你的操作系统。更多详细信息请参阅这个[`tex.stackexchange.com`的问题](http://tex.stackexchange.com/questions/43642)。 |
| 153 | +如果 matplotlib 使用的字体配置不同于你的 LaTeX 文档中的字体设置,则导入图形中的文本元素对齐可能会关闭。如果你不确定 matplotlib 用于布局的字体,请检查`.pgf`文件的标题。 |
| 154 | +如果图中有很多对象,矢量图像和`.pgf`文件可能变得臃肿。这可能是图像处理或非常大的散点图的情况。在极端情况下,这可能导致 TeX 内存不足:`TeX capacity exceeded, sorry`(TeX 容量过大,对不起)。你可以配置 LaTeX 来增加可用于生成`.pdf`图像的内存量,请见[`tex.stackexchange.com`](http://tex.stackexchange.com/questions/7953)上讨论的问题。另一种方法是使用`rasterized = True`关键字,或者根据[本示例](http://matplotlib.org/examples/misc/rasterization_demo.html)的`.set_rasterized(True)`『栅格化』图形的某些导致问题部分。 |
| 155 | +如果你仍需要帮助,请参阅[获取帮助](http://matplotlib.org/faq/troubleshooting_faq.html#reporting-problems)。 |
0 commit comments