Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
\addplot plot file {somedata.dat};
\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.

share|improve this question
1  
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
add comment

1 Answer 1

up vote 4 down vote accepted

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
      \InputIfFileExists{#1}{}{file not found}%
      \let\input@path\@undefined
    }%
  \else
    \edef\reserved@a{%
      \noexpand\let\noexpand\input@path\noexpand\Ginput@path
      \noexpand\InputIfFileExists{#1}{}{file not found}%
      \noexpand\def\noexpand\input@path\expandafter{\input@path}%
    }%
  \fi
  \reserved@a
}
\makeatother

\begin{document}
\section{Read it}%
\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
\patchcmd{\pgfplots@addplotimpl@file@streamit}{%
  \openin\r@pgfplots@reada=#1
}{%
  \let\input@path\Ginput@path
  \IfFileExists{#1}{\openin\r@pgfplots@reada=\@filef@und}{}%
  \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}
\addplot plot file {somedata.dat};
\end{axis}
\end{tikzpicture}
\end{document}

Maybe a feature request to the author of pgfplots would be a good idea.

share|improve this answer
    
I added a MWE and will test your code –  gerrit Nov 23 '11 at 14:51
    
@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
    
@gerrit I have received your feature request per mail, thank you. I will look into it eventually (actually, it is already on the todo list, if I am not mistaken). –  Christian Feuersänger Nov 26 '11 at 22:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.