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.
