Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib latex working directory / search path

Matplotlib doesn't seem to find files in the current working directory when running latex. Does anyone know where it looks for files?

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}')

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?

My minimal working example is:

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')

Where BigFatHeader.tex might be as simple as

\usepackage{bm}
like image 519
Mike Avatar asked Feb 11 '12 17:02

Mike


1 Answers

I'm having the same error on my Ubuntu Lucid, matplotlib 1.1.0. There are two options:

Giving it a full path:

matplotlib.rcParams['text.latex.preamble'] = r'\input{/home/br/sweethome/temp/BigFatHeader}'

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')

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')
like image 153
ev-br Avatar answered Oct 25 '22 01:10

ev-br