# Error when there are plotting macros before \pgfplotsinvokeforeach

Why does \pgfplotsinvokeforeach produce

ERROR: Undefined control sequence.

when there are plotting macros before it inside a groupplot?

\documentclass{article}

\usepackage{pgfplots}

\usepgfplotslibrary{groupplots}

\begin{document}

\begin{figure}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=3 by 1}]
\nextgroupplot        % When this and...
\addplot{x^1};        % this line is commented there is no error
\pgfplotsinvokeforeach{2,3}{%
\nextgroupplot
}
\end{groupplot}
\end{tikzpicture}
\end{figure}

\end{document}

-
The arguments are expanded so \nextgroupplot needs a \noexpand just before that. –  percusse Aug 6 '12 at 15:03
@percusse But then the output is several plots in one axis and not several plots in their own axis. Maybe I am using the wrong approach. What I want to do is to be able to do is to have different options for the first \nextgroupplot than the rest (in the loop). –  N.N. Aug 6 '12 at 15:08

I'm not 100% sure but this is due to the grouping inside the foreach loop such that pgfplots thinks there is a nesting going on which is not allowed. So a little bit manual ungrouped work, gives a result.

\documentclass{article}

\usepackage{pgfplots}

\usepgfplotslibrary{groupplots}

\begin{document}

\begin{figure}
\begin{tikzpicture}[scale=0.7]
\begin{groupplot}[group style={group size=3 by 1}]
\pgfplotsforeachungrouped \x in {1,2,3}{%
\edef\temp{\noexpand\nextgroupplot%