gpt4 book ai didi

python - 描述中的 Matplotlib tex-macro

转载 作者:行者123 更新时间:2023-12-04 15:53:22 24 4
gpt4 key购买 nike

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

这里有一个例子 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 文件中提供的标签中使用宏?

最佳答案

tl;dr 包括 tex -pgf.preamble 中的文件的 matplotlib需要绝对路径。


为了以后,我推荐以下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并确保首先在你的 PATH 中找到这个包装器(参见 which pdflatex)。运行 python 时生成器,我们保留最终的 texdbg.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')

(我使用 python3pathlib 。对于 python2我们宁愿回到os.getcwd .)

关于python - 描述中的 Matplotlib tex-macro,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52918660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com