6

I know that there are some expansion problem if the \foreach command is used in pgfplots' axis environment. I have read the answer concerning this issue and I know that the loop variable should be expanded by edef. But after using such a technique, my code still failed.

I'm going to draw some subplot with the help of groupplots library. The data file is d1.dat, d2.dat and so on. So I try to use foreach to plot them. The following is my attempt.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{filecontents*}{d1.dat}
1   2
2   3
\end{filecontents*}

\begin{filecontents*}{d2.dat}
1   3
1.5   0
2   4
\end{filecontents*}
\usepgfplotslibrary{groupplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
    \foreach \x in {1,2}{
        \edef\tmp{\noexpand\addplot table {d\x.dat};}
        \tmp
    }
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{groupplot}[
    group style={
        group size=1 by 2,
        vertical sep=2cm
    }]
    \nextgroupplot[title=data1]
    \addplot table {d1.dat};
    \nextgroupplot[title=data2]
    \addplot table {d2.dat};
\end{groupplot}
\end{tikzpicture}

% \begin{tikzpicture}
% \begin{groupplot}[
%     group style={
%         group size=1 by 2,
%         vertical sep=2cm
%     }
%     ]
%     \foreach \x/\y in {1/data1,2/data2}{
%     \edef\tmp{
%         \noexpand\nextgroupplot[title=\y]
%         \noexpand\addplot table {d\x.dat};
%     }
%     \tmp
%     }
% \end{groupplot}
% \end{tikzpicture}

\end{document}

The above code contain three tikzpicture environment. In the first tikzpicture environment, the edef technique is used and it works as expected. In the last tikzpicture environment, I still use this method but it failed. I wonder the reason why it failed.

Stefan Pinnow
  • 29,535
Ice0cean
  • 1,646

1 Answers1

10

\foreach of these subtleties one can write a separate question and answer. ;-)

Here the issue is that \foreach puts the stuff it is iterating over in groups. Ironically, despite their name, \groupplots do not want to be put in groups. So \pgfplotsforeachungrouped saves the day here.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{filecontents*}{d1.dat}
1   2
2   3
\end{filecontents*}

\begin{filecontents*}{d2.dat}
1   3
1.5   0
2   4
\end{filecontents*}
\usepgfplotslibrary{groupplots}
\begin{document}

% this fails because the group plots are in group
% 
% \begin{tikzpicture}
% \begin{groupplot}[
%     group style={
%         group size=1 by 2,
%         vertical sep=2cm
%     }]
%     \begingroup\nextgroupplot[title=data1]
%     \addplot table {d1.dat};\endgroup
%     \begingroup\nextgroupplot[title=data2]
%     \addplot table {d2.dat};\endgroup
% \end{groupplot}
% \end{tikzpicture}

\begin{tikzpicture}
\begin{groupplot}[
    group style={
        group size=1 by 2,
        vertical sep=2cm
    }
    ]
    \pgfplotsforeachungrouped \x in {1,2}{
    \edef\tmp{
        \noexpand\nextgroupplot[title=data\x]
        \noexpand\addplot table {d\x.dat};
    }
    \tmp
    }
\end{groupplot}
\end{tikzpicture}

\end{document}

enter image description here

If you uncomment the commented part, you can verify that putting the \groupplots into groups leads to errors, regardless of \foreach.