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!

\foreach \dt in {1,2} {
           \pgfplotsinvokeforeach {1,2,...,10} { 
            \addplot[]  table[x=x,y=var\dt_roll#1] {../plot/test.txt};
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – user31729
    May 28, 2014 at 11:23

1 Answer 1


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


\usepackage{pgfplots, pgfplotstable}

x var1_roll1 var1_roll2 var2_roll1 var2_roll2
1 10 11 20 21
2 10 11 20 21

\foreach\dt in {1,2}{
    \foreach\roll in {1,2}{
        \addplot table [y=var\dt_roll\roll]{\datatable};
  • My Intention was to have two axis environments but this is also possible with your suggestion by placing the first \foreach before \begin{åxis} . Thanks
    – Jonas
    May 28, 2014 at 12:56

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.