# pgfplots from file: search path, looking for \graphicspath equivalent

I have recently discovered the wonderful pgfplots package and I've started coding my Python code to output tabular data of whatever is plotted (at least for simple plots). In my LaTeX source code, I use plot file {../datafiles/data.dat}; and similar commands extensively. All my external figures (yes, I still have some) are in a separate directory that I specify in my preamble using \graphicspath{...}. Is there any equivalent path that TikZ and pgfplots use to search for data files?

I have difficulties searching for an answer to this question, because the word path has such a specific meaning in a TikZ context. I'm using PGF 2.10 and pgfplots 1.5. I checked in the PGF manual (section 19.4, page 224) and in the pgfplots manual (section 4.2.2, page 25–26) but did not find anything relevant.

MWE:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
\end{document}


where somedata.dat contains:

1 2
3 4
5 6
7 8
9 10


Sorry for not adding a MWE earlier, I figured it wasn't needed because the question was obvious.

-
Note that "software support" for search paths in TeX is only one possible approach. An alternative is to set the TeX search path BEFORE calling TeX: its search path is applied to any graphics and data files as well. Consequently, you could configure your Python scripts to modify the TEXINPUTS environment variable, and all would work out-of-the-box. For linux, this is extremely simple - I am not so sure if that works for MikTeX under Windows as well (but I suppose the general idea will work). –  Christian Feuersänger Nov 26 '11 at 22:28
To extend Christian's comment until pgfplots can do this natively TEXINPUTS can contain even relative paths and LaTeX searchs recursively. So a simple environment variable setting such as TEXINPUTS=.// will tell LaTeX to search the current directory and all subdirectories. This might slow the compilation down a bit for a large number of files/directories but I did not experience a big effect. –  Alexander Nov 12 '12 at 17:31

Package graphics simply redefines \input@path (part of the LaTeX file name parser \filename@parse) to use \Ginput@path. Here is a minimal example in using the LaTeX file name parser for other files:

\begin{filecontents}{testdir/testsubfile.tex}
This is \texttt{testdir/testsubfile.tex}.
\end{filecontents}
\documentclass{article}
\usepackage{graphics}
\graphicspath{{testdir/}}

\makeatletter
\newcommand*{\inputfromgraphicspath}[1]{%
\ifx\input@path\@undefined
\def\reserved@a{%
\let\input@path\Ginput@path
\let\input@path\@undefined
}%
\else
\edef\reserved@a{%
\noexpand\let\noexpand\input@path\noexpand\Ginput@path
\noexpand\def\noexpand\input@path\expandafter{\input@path}%
}%
\fi
\reserved@a
}
\makeatother

\begin{document}
\inputfromgraphicspath{testsubfile.tex}
\end{document}


Here \InputIfFileExists has been used, that uses the LaTeX file name parser internally.

Note: To test the example you first have to make sub-directory testdir (otherwise writing the test file testdir/testsubfile.tex would fail).

Unfortunately pgfplots does not use the LaTeX file name parser. But you may patch it into the definition of \pgfplots@addplotimpl@file@streamit:

\begin{filecontents*}{testdir/somedata.dat}
1 2
3 4
5 6
7 8
9 10
\end{filecontents*}
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\usepackage{etoolbox}
\makeatletter
}{%
\let\input@path\Ginput@path
\let\input@path\undefined
}{}{\typeout{patch failed}\@undefined}
\makeatother

\usepackage{graphics}
\graphicspath{{testdir/}}
\begin{document}
\begin{tikzpicture}\makeatletter\let\input@path\Ginput@path\makeatother
\begin{axis}

Maybe a feature request to the author of pgfplots would be a good idea.
@gerrit: With your MWE I was able to patch pgfplots to use \Ginput@path for plot file (see the revised answer). Similar patches may be done for other macros of pgfplots but a better solution would be, if the package author would add such a feature (including \pgfplotspath command) to the package. –  Schweinebacke Nov 23 '11 at 15:56