[Tex/LaTex] Using \pgfplotsinvokeforeach in foreach loop

foreachloops

I am trying to run a \pgfplotsinvokeforeach loop in a \foreach loop to plot the variables
var1_roll1 – var1_roll10 in one plot and var2_roll1 – var2_roll10 in another.
Both loops work fine individually but a combination of both gives the following error:

! Illegal parameter number in definition
of \pgffor@body.

Can anyone help? Here is the Code. Thanks!

\begin{figure}    
\foreach \dt in {1,2} {
    \begin{tikzpicture}
        \begin{axis}[]
           \pgfplotsinvokeforeach {1,2,...,10} { 
            \addplot[]  table[x=x,y=var\dt_roll#1] {../plot/test.txt};
            }
        \end{axis}
    \end{tikzpicture}
    }
\end{figure}

Best Answer

You don't need to use \pgfplotstableinvokeforeach in this case, simply nesting two ordinary \foreach loops works fine:

\documentclass{article}

\usepackage{pgfplots, pgfplotstable}

\pgfplotstableread{
x var1_roll1 var1_roll2 var2_roll1 var2_roll2
1 10 11 20 21
2 10 11 20 21
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}
\foreach\dt in {1,2}{
    \foreach\roll in {1,2}{
        \addplot table [y=var\dt_roll\roll]{\datatable};
    }
}
\end{axis}
\end{tikzpicture}
\end{document}