Matplotlib Latex 工作目录/搜索路径 [英] Matplotlib latex working directory / search path
问题描述
运行 Latex 时,Matplotlib 似乎没有在当前工作目录中找到文件.有谁知道它在哪里查找文件?
Matplotlib doesn't seem to find files in the current working directory when running latex. Does anyone know where it looks for files?
背景是:我有一个巨大的序言,我在处理之前将 input
放入 Latex 中(大量宏、各种使用包等).在独立论文中,我使用 input{BigFatHeader.tex}
.所以当我使用 matplotlib 时,我尝试在序言中输入这个文件.执行此操作的 python 代码是
The background is: I have a huge preamble that I input
into latex before processing (lots of macros, various usepackages, etc.). In a stand-alone paper, I do input{BigFatHeader.tex}
. So when I use matplotlib, I try to just input this file in the preamble. The python code to do this is
matplotlib.rcParams['text.latex.preamble'].append(r'input{BigFatHeader.tex}')
我可以验证该文件是否在 cwd 中——我在 ls
时看到它,或者我可以执行 os.path.isfile("BigFatHeader.tex")
并获得 True.但是当我尝试使用乳胶绘制某些东西时,python 从乳胶过程中吐出一条大错误消息,最终导致!LaTeX 错误:找不到文件 BigFatHeader.tex
.所以大概它会更改到其他目录(不是 /tmp/
;我检查过)来完成它的工作.知道这可能在哪里吗?
And I can verify that that file is in the cwd -- I see it when I ls
, or I can do os.path.isfile("BigFatHeader.tex")
and get True. But when I try to plot something using latex, python spits out a big error message from the latex process, that culminates in
! LaTeX Error: File BigFatHeader.tex
not found.
So presumably it changes to some other directory (not /tmp/
; I checked) to do its work. Any idea where this might be?
我的最小工作示例是:
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['text.latex.preamble'] = r'input{BigFatHeader.tex}'
matplotlib.rcParams['text.usetex'] = True
plt.plot([1,2])
plt.savefig('MWE.pdf')
BigFatHeader.tex
可能就这么简单
usepackage{bm}
推荐答案
我在我的 Ubuntu Lucid,matplotlib 1.1.0 上遇到了同样的错误.有两种选择:
I'm having the same error on my Ubuntu Lucid, matplotlib 1.1.0. There are two options:
给它一个完整的路径:
matplotlib.rcParams['text.latex.preamble'] = r'input{/home/br/sweethome/temp/BigFatHeader}'
为我工作.请注意,您没有将文件的 .tex
扩展名设置为 input
.如果您不想硬编码路径,可以使用 os.getcwd()
获取它:
works for me. Notice that you don't put .tex
extension for the files to be input
. If you don't want to hardcode the path, you can get it using os.getcwd()
:
import matplotlib
import matplotlib.pyplot as plt
import os
filename=r'input{'+os.getcwd()+r'/BigFatHeader}'
matplotlib.rcParams['text.latex.preamble'] = filename
matplotlib.rcParams['text.usetex'] = True
plt.plot([1,2])
plt.savefig('MWE.pdf')
或者只是将你的文件读入一个文本字符串并用它设置 rcParams
.
Or just read in your your file into a text string and set the rcParams
with it.
import matplotlib
import matplotlib.pyplot as plt
paramstring=r'usepackage{bm}'
matplotlib.rcParams['text.latex.preamble'] = paramstring
matplotlib.rcParams['text.usetex'] = True
plt.plot([1,2])
plt.savefig('MWE.pdf')
这篇关于Matplotlib Latex 工作目录/搜索路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!