forked from apachecn/matplotlib-doc-zh
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92449d0
commit 1471a24
Showing
1 changed file
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
# XeLaTeX/LuaLaTeX 设置 | ||
|
||
> 原文:[Typesetting With XeLaTeX/LuaLaTeX](http://matplotlib.org/users/pgf.html) | ||
> 译者:[飞龙](https://github.com/) | ||
> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/) | ||
使用 pgf 后端,matplotlib 可以将图形导出为可以使用 pdflatex,xelatex 或 lualatex 处理的 pgf 绘图命令。 XeLaTeX 和 LuaLaTeX 具有完整的 unicode 支持,可以使用安装在操作系统中的任何字体,利用 OpenType,AAT 和 Graphite 的高级排版功能。 由`plt.savefig('figure.pgf')`创建的 Pgf 图片可以作为原始命令嵌入到 LaTeX 文档中。 图形也可以通过切换到该后端,直接编译并使用`plt.savefig('figure.pdf')`保存到 PDF。 | ||
|
||
```py | ||
matplotlib.use('pgf') | ||
``` | ||
|
||
或者为处理 PDF 输出而注册它: | ||
|
||
```py | ||
from matplotlib.backends.backend_pgf import FigureCanvasPgf | ||
matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf) | ||
``` | ||
|
||
第二种方法允许你继续使用常规的交互式后端,并从图形用户界面保存 xelatex,lualatex 或 pdflatex 编译的 PDF 文件。 | ||
|
||
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)中。 | ||
|
||
控制 pgf 后端行为的 Rc 参数: | ||
|
||
| 参数 | 文档 | | ||
| --- | --- | | ||
| `pgf.preamble `| 包含在 LaTeX 序言中的行 | | ||
| `pgf.rcfonts` | 使用 fontspec 软件包从 rc 参数设置字体 | | ||
| `pgf.texsystem` | `xelatex`(默认),`lualatex`或者`pdflatex` | | ||
|
||
> 注意 | ||
> TeX 定义了一系列特殊字符,例如: | ||
> ``` | ||
> # $ % & ~ _ ^ \ { } | ||
> ``` | ||
> 通常,这些字符必须正确转义。一些字符(`_`,`^`,`%`)会自动在数学环境之外转义。 | ||
## 字体规定 | ||
用于获取文本元素大小,或将图形编译为 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 等。 | ||
保存到`.pgf`时,matplotlib 用于图形布局的字体配置包含在文本文件的标题中。 | ||
```py | ||
# -*- coding: utf-8 -*- | ||
import matplotlib as mpl | ||
mpl.use("pgf") | ||
pgf_with_rc_fonts = { | ||
"font.family": "serif", | ||
"font.serif": [], # use latex default serif font | ||
"font.sans-serif": ["DejaVu Sans"], # use a specific sans-serif font | ||
} | ||
mpl.rcParams.update(pgf_with_rc_fonts) | ||
import matplotlib.pyplot as plt | ||
plt.figure(figsize=(4.5,2.5)) | ||
plt.plot(range(5)) | ||
plt.text(0.5, 3., "serif") | ||
plt.text(0.5, 2., "monospace", family="monospace") | ||
plt.text(2.5, 2., "sans-serif", family="sans-serif") | ||
plt.text(2.5, 1., "comic sans", family="Comic Sans MS") | ||
plt.xlabel(u"μ is not $\\mu$") | ||
plt.tight_layout(.5) | ||
``` | ||
![](http://matplotlib.org/_images/pgf_fonts.png) | ||
|
||
## 自定义序言 | ||
|
||
通过将你的命令添加到序言中,你可以完全自定义它。 如果要配置数学字体(例如使用 unicode-math)或加载其他软件包,请使用`pgf.preamble`参数。 此外,如果你想自己做字体配置,而不是使用 rc 参数中指定的字体,请确保禁用`pgf.rcfonts`。 | ||
|
||
```py | ||
# -*- coding: utf-8 -*- | ||
from __future__ import (absolute_import, division, print_function, | ||
unicode_literals) | ||
|
||
import six | ||
|
||
import matplotlib as mpl | ||
mpl.use("pgf") | ||
pgf_with_custom_preamble = { | ||
"font.family": "serif", # use serif/main font for text elements | ||
"text.usetex": True, # use inline math for ticks | ||
"pgf.rcfonts": False, # don't setup fonts from rc parameters | ||
"pgf.preamble": [ | ||
"\\usepackage{units}", # load additional packages | ||
"\\usepackage{metalogo}", | ||
"\\usepackage{unicode-math}", # unicode math setup | ||
r"\setmathfont{xits-math.otf}", | ||
r"\setmainfont{DejaVu Serif}", # serif font via preamble | ||
] | ||
} | ||
mpl.rcParams.update(pgf_with_custom_preamble) | ||
|
||
import matplotlib.pyplot as plt | ||
plt.figure(figsize=(4.5,2.5)) | ||
plt.plot(range(5)) | ||
plt.xlabel("unicode text: я, ψ, €, ü, \\unitfrac[10]{°}{μm}") | ||
plt.ylabel("\\XeLaTeX") | ||
plt.legend(["unicode math: $λ=∑_i^∞ μ_i^2$"]) | ||
plt.tight_layout(.5) | ||
``` | ||
|
||
![](http://matplotlib.org/_images/pgf_preamble.png) | ||
|
||
## 选择 TeX 系统 | ||
|
||
matplotlib 使用的 TeX 系统由`pgf.texsystem`参数选择。 可能的值为`xelatex`(默认值),`lualatex`和`pdflatex`。 请注意,当选择`pdflatex`时,必须在序言中配置字体和 unicode 处理。 | ||
|
||
```py | ||
# -*- coding: utf-8 -*- | ||
|
||
import matplotlib as mpl | ||
mpl.use("pgf") | ||
pgf_with_pdflatex = { | ||
"pgf.texsystem": "pdflatex", | ||
"pgf.preamble": [ | ||
r"\usepackage[utf8x]{inputenc}", | ||
r"\usepackage[T1]{fontenc}", | ||
r"\usepackage{cmbright}", | ||
] | ||
} | ||
mpl.rcParams.update(pgf_with_pdflatex) | ||
|
||
import matplotlib.pyplot as plt | ||
plt.figure(figsize=(4.5,2.5)) | ||
plt.plot(range(5)) | ||
plt.text(0.5, 3., "serif", family="serif") | ||
plt.text(0.5, 2., "monospace", family="monospace") | ||
plt.text(2.5, 2., "sans-serif", family="sans-serif") | ||
plt.xlabel(u"μ is not $\\mu$") | ||
plt.tight_layout(.5) | ||
``` | ||
|
||
![](http://matplotlib.org/_images/pgf_texsystem.png) | ||
|
||
## 故障排除 | ||
|
||
请注意,在一些 Linux 发行版和 MiKTeX 安装中发现的 TeX 包已经过时了。确保更新你的软件包目录并升级或安装最新的 TeX 发行版。 | ||
在 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)。 | ||
Windows 上的限制会导致后端保留由应用程序打开的文件句柄。因此,可能无法删除相应的文件,直到应用程序关闭(参见[`#1324`](https://github.com/matplotlib/matplotlib/issues/1324))。 | ||
有时保存到 png 图像的图形中的字体非常糟糕。这在 pdftocairo 工具不可用,并且 ghostscript 用于 pdf 到 png 的转换时发生。 | ||
确保你想要做的事情在 LaTeX 文档中可实现,你的 LaTeX 语法是有效的,并且你正在使用原始字符串,如果必要的话,避免意外的转义序列。 | ||
`pgf.preamble rc`设置提供了大量的灵活性,以及导致问题的许多方法。遇到问题时,尝试最小化或禁用自定义序言。 | ||
配置 unicode-math 环境可能有点棘手。例如 TeXLive 分发版提供了一组通常不在系统范围内安装的数学字体。与 LuaLatex 不同的是,XeTeX 不能根据名字找到这些字体,这就是你可能必须指定`\setmathfont{xits-math.otf}`,而不是`\setmathfont{XITS Math}`的原因,或者使字体可用于你的操作系统。更多详细信息请参阅这个[`tex.stackexchange.com`的问题](http://tex.stackexchange.com/questions/43642)。 | ||
如果 matplotlib 使用的字体配置不同于你的 LaTeX 文档中的字体设置,则导入图形中的文本元素对齐可能会关闭。如果你不确定 matplotlib 用于布局的字体,请检查`.pgf`文件的标题。 | ||
如果图中有很多对象,矢量图像和`.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)`『栅格化』图形的某些导致问题部分。 | ||
如果你仍需要帮助,请参阅[获取帮助](http://matplotlib.org/faq/troubleshooting_faq.html#reporting-problems)。 |