4

Is there a way to add a file path prefix (ideally defined by a key) to the "argument" of \addplot table. Something like

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{%
  table file path/.initial = {./}}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    \pgfplotsset{%
      table file path = {./cvsfile/}}
    % Plot ./cvsfile/myfile
    \addplot table {myfile};
  \end{axis}
\end{tikzpicture}
\end{document}
cjorssen
  • 10,032
  • 4
  • 36
  • 126

1 Answers1

5

This seems to work...

\documentclass{standalone}
\usepackage{pgfplots}


\makeatletter

\def\pgfplotstableread@openfile{%
    \def\pgfplotstable@loc@TMPa{\pgfutil@in@{ }}%
    \expandafter\pgfplotstable@loc@TMPa\expandafter{\pgfplotstableread@filename}%
    \ifpgfutil@in@
        \t@pgfplots@toka=\expandafter{\pgfplotstableread@filename}%
        \edef\pgfplotstableread@filename{\pgfplots@dquote\the\t@pgfplots@toka\pgfplots@dquote}%
    \fi
    \let\pgfplotstableread@old@crcr=\\%
    \def\\{\string\\}% just to make sure we don't try to open inline table data...
    \openin\r@pgfplots@reada=\csname pgfk@/pgfplots/table file path\endcsname\pgfplotstableread@filename.tex
    \ifeof\r@pgfplots@reada
        \openin\r@pgfplots@reada=\csname pgfk@/pgfplots/table file path\endcsname\pgfplotstableread@filename\relax
    \else
        \pgfplots@warning{%
            You requested to open table '\pgfplotstableread@filename', but there is also a '\pgfplotstableread@filename.tex'. 
            TeX will automatically append the suffix '.tex', so I will now open '\pgfplotstableread@filename.tex'.
            Please make sure you don't accidentally load TeX files - this may produce unrecoverable errors.}%
        \closein\r@pgfplots@reada
        \openin\r@pgfplots@reada=\pgfplotstableread@filename\relax
    \fi
    %
    \ifeof\r@pgfplots@reada
        \pgfplotsthrow{no such table file}{\pgfplots@loc@TMPa}{\pgfplotstableread@filename}{Could not read table file '\csname pgfk@/pgfplots/table file path\endcsname\pgfplotstableread@filename'. In case you intended to provide inline data: maybe TeX screwed up your end-of-lines? Try `row sep=crcr' and terminate your lines with `\string\\' (refer to the pgfplotstable manual for details)}\pgfeov%
        \global\let\pgfplotstable@colnames@glob=\pgfplots@loc@TMPa
        \def\pgfplotstableread@ready{0}%
    \fi
    \pgfplots@logfileopen{\pgfplotstableread@filename}%
    \let\\=\pgfplotstableread@old@crcr
}

\makeatother

\pgfplotsset{%
  table file path/.initial = {./}}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
  \pgfplotsset{%
      table file path = {./cvsfile/}
  }
    % Plot ./cvsfile/myfile
    \addplot table {myfile2.txt};
  \end{axis}
\end{tikzpicture}
\end{document}
David Carlisle
  • 757,742
  • Thanks: that a really good start (of course +1). Maybe a more robust solution would be to hack pgfplotstableshared.code.tex where \pgfplotstableread@filename is defined. I'll try later today. – cjorssen May 13 '13 at 12:04
  • @cjorssen That's just a few lines up, I didn't do it there as it was a bigger macro to copy or patch:-) – David Carlisle May 13 '13 at 12:08
  • Maybe a bit more robust (in case code change): \let\pgfplotstableread@openfile@i\pgfplotstableread@openfile \def\pgfplotstableread@openfile{\edef\pgfplotstableread@filename{\pgfkeysvalueof{/pgfplots/table file path}\pgfplotstableread@filename}\pgfplotstableread@openfile@i}. Thanks again for digging the code to the right place. – cjorssen May 13 '13 at 19:28