3

I have one file that plots a tikzpicture. I want to be able to use pgfplot tables (\loadedtableI or \loadedtableII in the MWE below) to plot the same figure twice. How can I define an alias to a table, without reloading it?

MWE

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\usepackage{filecontents}
\begin{filecontents}{data1.txt}
 X    Y
 1    1
 2    2
\end{filecontents}

\begin{filecontents}{data2.txt}
 X    Y
 1    1
 2    1
\end{filecontents}

\begin{filecontents}{myfig.tex}
  \begin{tikzpicture}
        \begin{axis}
        \addplot table {\loadedtable};
        \end{axis}
    \end{tikzpicture}
  \end{filecontents}

  \begin{document}

  \pgfplotstableread{data1.txt}\loadedtableI
  \pgfplotstableread{data2.txt}\loadedtableII

  % \def\loadtable{\loadedtableI} Something like this
  \input{myfig}
  % \def\loadtable{\loadedtableII}
  \input{myfig}

\end{document}
1

You can use some expansion. The following seems to work in this case

\edef\Temp{\unexpanded{\addplot} table {\expandonce\CurrentTable}}\Temp

enter image description here

Code:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}

\usepackage{etoolbox}

\usepackage{filecontents}
\begin{filecontents}{data1.txt}
 X    Y
 1    1
 2    2
\end{filecontents}

\begin{filecontents}{data2.txt}
 X    Y
 1    1
 2    1
\end{filecontents}

\begin{filecontents}{myfig.tex}
  \begin{tikzpicture}
        \begin{axis}
        \edef\Temp{\unexpanded{\addplot} table {\expandonce\CurrentTable}}\Temp;
        \end{axis}
    \end{tikzpicture}
  \end{filecontents}

  \begin{document}

  \pgfplotstableread{data1.txt}\loadedtableI
  \pgfplotstableread{data2.txt}\loadedtableII

  \def\CurrentTable{\loadedtableI}
  \input{myfig}
  \def\CurrentTable{\loadedtableII}
  \input{myfig}

\end{document}
  • Please note that I am not in anyway an expert on expansion (just in case any of the regulars here think that I finally get expansion). So if someone who is has a better solution, feel free to post an answer and I am happy to delete this one, – Peter Grill Dec 17 '18 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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