I'm trying to adapt the solution given here (this is also relevant) where legend entries can be added via a loop. For instance if we have a collection of N curves depending on more than one parameter it is nice to compare each collection within a groupplot environment. The following adaptation
\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
\begin{document}
\newcommand{\getLab}[1]{plot_#1}
\newcommand{\getLeg}[1]{curve #1}
\begin{tikzpicture}
\begin{groupplot}[cycle list name=color list,
group style={group name=myplot, group size= 2 by 2}]
\nextgroupplot[ylabel=throughput,
every axis y label/.append style={at=(ticklabel cs:0)}] % Common y label
%\addlegendimage{empty legend}
%\addlegendentryexpanded{param 1} % very convenient
\foreach \n in {1,...,3} {
\addplot {x^(\n)};
\label{\getLab{\n}}
}
\nextgroupplot
\foreach \n in {1,...,3} {
\addplot {x^(\n + 1)};
}
\nextgroupplot
\foreach \n in {1,...,3} {
\addplot {x^(\n + 2)};
}
\nextgroupplot
\foreach \n in {1,...,3} {
\addplot {x^(\n + 3)};
}
\end{groupplot}
\path (myplot c1r1.north west|-current bounding box.north)--
coordinate(legendpos)
(myplot c2r1.north east|-current bounding box.north);
\matrix[
matrix of nodes,
anchor=south,
draw,
inner sep=0.2em,
draw
] at ([yshift=1ex] legendpos) {
% Problem starts:
%\foreach \m in {1,...,3} { % ?? this just looks silly
% \ref{\getLab{\m}} & \getLeg{\m} &
%}
%
% This compiles surprisingly
%\foreach \m in {1,...,3} { \ref{\getLab{\m}} } &
%\foreach \m in {1,...,3} { \ref{\getLeg{\m}} } &
% Working
\ref{plot_2} & curve 2 &
\ref{plot_2} & curve 2 &
\ref{plot_3} & curve 3
\\};
\end{tikzpicture}
\end{document}
gives:
which is what I want in the end, but would like to 'functionally' generate the matrix structure used at the end similar to the way the labels were generated. Is that possible?


