3

matplotlib用来生成pgf文件。基于这些,我使用tex仅包含必要设置和预先构建pgf的文件的独立文件。在这种情况下,在我的绘图文件中使用自定义 tex 宏进行描述时出现错误。

这是一个示例pgf生成器:

import matplotlib as mpl
mpl.use("pgf")
mpl.rcParams.update({
    "pgf.texsystem": "pdflatex",
    "pgf.preamble": [
        #r"\newcommand{\foo}{foo}",
        r"\usepackage{import}",
        r'\subimport{./}{foo.tex}'
    ]
})

import matplotlib.pyplot as plt
plt.figure(figsize=(4.5,2.5))
plt.plot(range(5))
plt.xlabel(r'\foo{}')
plt.savefig('foo.pgf')

可以在具有以下foo.tex文件的目录中使用:

\newcommand{\foo}{foo}

运行此程序会导致以下错误:

ValueError: Error processing '\foo{}'
LaTeX Output:
! Undefined control sequence.
<argument> ....000000}{12.000000}\selectfont \foo
                                                  {}
<*> ...ze{10.000000}{12.000000}\selectfont \foo{}}

!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on texput.log.

请注意,这是由编译我的独立文件产生的错误,matplotlib不是由编译我的独立文件产生的错误。另请注意,当\foo宏作为pgf.preamble (注释掉的行)的一部分提供时,错误就会消失。我检查了pgf这个变体产生的,确实它使用了\foo{}.

我无法进一步缩小问题范围。这是我的具体问题:

  1. 为什么matplotlib调用pdflatex呢?我正在生成pgf输出,因此pdflatex没有必要。(供参考:我straced 上面的脚本并且确实知道pdflatex正在调用它。)
  2. 有没有办法保留matplotlib试图编译的临时文件?之后(当然)该文件引用的错误texput.log不存在。
  3. 为什么我不能在另一个 tex 文件中提供的标签中使用宏?
4

1 回答 1

2

tl;dr在of中包含tex-files需要绝对路径。pgf.preamblematplotlib


对于将来,我建议使用以下pdflatex“替换脚本”进行调试:

#!/usr/bin/env bash
MAIN=/usr/bin/pdflatex
cat /dev/stdin | tee /some/abs/path/to/dbg.tex | "${MAIN}" "${@}"

确保将其另存为pdflatex,确保它是可执行的,确保/usr/bin/pdflatex是您的实际pdflatex并确保首先在 yout 中找到此包装器PATH(参见which pdflatex)。运行python生成器时,我们将最终tex输入保存在dbg.tex. 答案是(2)。

考虑到输出,我们看到:

\documentclass{minimal}
\usepackage{import}
\subimport{./}{foo.tex}

\begin{document}
text $math \mu$
\typeout{pgf_backend_query_start}
\sbox0{\sffamily\fontsize{10.000000}{12.000000}\selectfont lp}
\typeout{\the\wd0,\the\ht0,\the\dp0}
\sbox0{\sffamily\fontsize{10.000000}{12.000000}\selectfont 0}
\typeout{\the\wd0,\the\ht0,\the\dp0}
\sbox0{\sffamily\fontsize{10.000000}{12.000000}\selectfont 1}
\typeout{\the\wd0,\the\ht0,\the\dp0}
\sbox0{\sffamily\fontsize{10.000000}{12.000000}\selectfont 2}
\typeout{\the\wd0,\the\ht0,\the\dp0}
\sbox0{\sffamily\fontsize{10.000000}{12.000000}\selectfont 3}
\typeout{\the\wd0,\the\ht0,\the\dp0}
\sbox0{\sffamily\fontsize{10.000000}{12.000000}\selectfont 4}
\typeout{\the\wd0,\the\ht0,\the\dp0}
\sbox0{\sffamily\fontsize{10.000000}{12.000000}\selectfont \foo{}}
\typeout{\the\wd0,\the\ht0,\the\dp0}

我不知道那应该有什么用。但我猜matplotlib是试图调整它尝试编译这个“测试”文档的字体设置。那(某种)答案(1)。

现在得出结论(事后看来很明显): matplotlib在一个临时目录中编译这个示例文档。显然这个目录中没有foo.tex可用的,所以subimport失败了。从那时起,很明显\foo将无法使用。

虽然不是最干净的解决方案,但这可以通过包含foo.tex绝对路径来解决。工作python生成器最终回答(3):

import matplotlib as mpl
import pathlib

mpl.use("pgf")
mpl.rcParams.update({
    "pgf.texsystem": "pdflatex",
    "pgf.preamble": [
        r"\usepackage{import}",
        f'\subimport{{{pathlib.Path.cwd().resolve()}/}}{{foo.tex}}']
})

import matplotlib.pyplot as plt
plt.figure(figsize=(4.5,2.5))
plt.plot(range(5))
plt.xlabel(r'\foo{}')
plt.savefig('foo.pgf')

(我使用python3and pathlib。因为python2我们宁愿回退到os.getcwd。)

于 2018-10-22T18:58:07.273 回答