1

I am trying to plot several column pairs using the \foreach loop command with pgfplotstable in the following way.

\documentclass[tikz]{standalone}
\usepackage{pgfplots,pgfplotstable}
\begin{document}
\begin{tikzpicture}
\def \x {data.txt}
\begin{axis}
    [
        xlabel={x},
        ylabel={y},     
    ]
    \foreach \i/\j in {0/1,2/3,4/5,6/7,8/9,10/11}{
    \edef\temp{\noexpand\addplot[smooth,color=blue,no marks] table[x index=\i,y index=\j] {\x};
    }
    \temp
    } 
\end{axis}
\end{tikzpicture}
\end{document}

Is there a way to automate list creation over two variables as in something like

 \foreach \i/\j in {0/1,2/3,...,10/11}

which does not seem to work?

  • You can pull table elements using \pgfplotstablegetelem and construct a list/macro using \xdef – John Kormylo Apr 09 '18 at 01:52
  • ... and there is of course \pgfplotsinvokeforeach, see the end of section 4.6.3 of the manual... –  Apr 09 '18 at 01:54
  • If your values are consecutive then you can go about it as \foreach\x[count=\xi from 0] in{1,...,11} or use evaluate if you have complicated expression\ – percusse Apr 09 '18 at 08:41
  • Duplicate, I suppose? https://tex.stackexchange.com/questions/37601/possible-to-combine-and-x-y-in-tikzs-foreach – Torbjørn T. Apr 09 '18 at 20:31
  • Unfortunately, evaluate returns a number that is a floating point number and cannot be passed as a row/column index for plotting purposes. – scorpionwars Apr 11 '18 at 01:41
  • int(<calculation>) gives you an integer. – Torbjørn T. Apr 11 '18 at 14:55

1 Answers1

1

Based on the comments and the earlier asked question, I figured out a way to define successive lists based on a parent list definition, that can be passed as indices for plotting with pgfplotstable. The working code is as follows.

\documentclass[tikz]{standalone}
\usepackage{pgfplots,pgfplotstable}
\begin{document}
\begin{tikzpicture}
\def \x {data.txt}
\begin{axis}
   [
    xlabel={x},
    ylabel={y},     
   ]
   \foreach[evaluate=\j using int(\i+1)] \i in {0,2,...,38}
   {
   \edef\temp{\noexpand\addplot[smooth,color=blue,no marks] table[x index=\i,y index=\j] {\x};
   }
   \temp
   } 
\end{axis}
\end{tikzpicture}
\end{document}