Matplotlib乳胶工作目录/搜索路径 [英] Matplotlib latex working directory / search path
问题描述
运行乳胶时,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
放入乳胶中(很多宏,各种使用包等).在独立论文中,我执行\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}'
为我工作.请注意,对于文件为\input
的文件,您无需添加.tex
扩展名.如果您不想对路径进行硬编码,则可以使用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乳胶工作目录/搜索路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!