# Am I properly inserting legends in groupplots? Labels “multiply defined”

I have a few places in my document where I use groupplot to insert several figures, but I only want one legend as it's common to them all. I've done this using legend to name=foo and then, after the figure, \ref{foo}

The whole thing looks like this (which is not an MWE)

\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
legend columns=-1,
legend entries={{\tiny ++Cost},{\tiny ++FTE},{\tiny ++Resources},{\tiny Hold All},{\tiny Random},{\tiny Come and Go}},
legend to name=CombinedLegendAlpha2,
group style={
group size=3 by 1,
xlabels at=edge bottom,
ylabels at=edge left
},
legend style={draw=none},
legend style={at={(0.98,0.825)}},
xlabel = {\footnotesize $\alpha_{++}$},
ylabel = {\footnotesize Avg Portfolio Value},
]
\nextgroupplot[title={\scriptsize Empirical CDF},
y tick label style={
font=\tiny,
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=0,
/tikz/.cd
},
footnotesize,
x tick label style={
font=\tiny,
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=1,
/tikz/.cd
}]
\addplot+[black, mark=o,line join=round, mark repeat=10] table[col sep=comma, y=PlusPlusCost, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=x,line join=round, mark repeat=10] table[col sep=comma, y=PlusPlusFTE, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=|,line join=round, mark repeat=10] table[col sep=comma, y=PlusPlusResources, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=square,line join=round, mark repeat=10] table[col sep=comma, y=HoldAll, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=star,line join=round, mark repeat=10] table[col sep=comma, y=Random, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=otimes,line join=round, mark repeat=10] table[col sep=comma, y=ComeAndGo, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\nextgroupplot[title={\scriptsize Triangular CDF},
y tick label style={
font=\tiny,
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=0,
/tikz/.cd
},
footnotesize,
x tick label style={
font=\tiny,
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=0,
/tikz/.cd
}]
\addplot+[black, mark=o,line join=round, mark repeat=10] table[col sep=comma, y=PlusPlusCostTri, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=x,line join=round, mark repeat=10] table[col sep=comma, y=PlusPlusFTETri, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=|,line join=round, mark repeat=10] table[col sep=comma, y=PlusPlusResourcesTri, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=square,line join=round, mark repeat=10] table[col sep=comma, y=HoldAll, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=star,line join=round, mark repeat=10] table[col sep=comma, y=Random, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=otimes,line join=round, mark repeat=10] table[col sep=comma, y=ComeAndGo, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\nextgroupplot[title={\scriptsize LN/Exponential CDF},
y tick label style={
font=\tiny,
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=0,
/tikz/.cd
},
footnotesize,
x tick label style={
font=\tiny,
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=0,
/tikz/.cd
}]
\addplot+[black, mark=o,line join=round, mark repeat=10] table[col sep=comma, y=PlusPlusCostLN, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=x,line join=round, mark repeat=10] table[col sep=comma, y=PlusPlusFTEExpo, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=|,line join=round, mark repeat=10] table[col sep=comma, y=PlusPlusResourcesExpo, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=square,line join=round, mark repeat=10] table[col sep=comma, y=HoldAll, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=star,line join=round, mark repeat=10] table[col sep=comma, y=Random, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\addplot+[black, mark=otimes,line join=round, mark repeat=10] table[col sep=comma, y=ComeAndGo, x=CostAlpha]{PlusPlusMethodsAlpha.csv};
\end{groupplot}
\end{tikzpicture}
\ref{CombinedLegendAlpha2}
\caption{Triage++ Performance}
\label{PlusPlusAlpha}
\end{figure}


This works fine, and produces the output I want. For instance, the above code produces

When I build the file though, I get warnings that my labels (in this case, CombinedLegendAlpha2) are multiply defined. I know these are warnings, not errors, but they still bug me. Is there a more appropriate way to accomplish this? I'm very much a Latex novice, so it would not surprise me at all if my solution is hackish....

• May be your are using the name CombinedLegendAlpha2 at more than one place. Just search. – user11232 May 14 '15 at 6:32
• Nope, CombinedLegendAlpha2 appears two times in the whole document: once in the legend to name and then in the \ref. I get warnings on every legend where I use legend to name – jerH May 14 '15 at 11:57

Indeed your legend to name is defined multiple times.

Whatever you put in the \begin{groupplot}[#1] as #1 will be put on all axis plots.

Doing:

\begin{groupplot}[/tikz/font=\small,...]
\nextgroupplot
...
\nextgroupplot
...
\end{groupplot}


is thus equivalent to:

\begin{axis}[/tikz/font=\small,...]
...
\end{axis}
\begin{axis}[/tikz/font=\small,at=<below>,...]
...
\end{axis}
\begin{axis}[/tikz/font=\small,at=<below>,...]
...
\end{axis}


more or less.

Whatever you put in groupplot is global. Instead move the once used keys into their respective \nextgroupplot[#1] to limit their extend. The top keys passed to the groupplot environment is intended much similarly as the scope environment.

• Your answer makes sense, but I'm surprised that this is the case for legends, where it makes sense to define the total legend in the total groupplot options instead of the nextgroupplot options – darthbith May 18 '15 at 19:28
• To be frank, I am quite surprised you do not have 3 legend boxes. Furthermore I just do not get how you get the legend box below the plots with legend style={at={(0.98,0.825)}}. Nevertheless it might not always make sense in grouped plots to have a common legend. It can be used for that, but it is general to allow different legends in each \nextgroupplot. So I would not say that common legends should be a general feature of the grouped plots. I always encourage users to supply legends in the plot they wish the legend box shown, this is a simple reflection of the abstraction allowed. – zeroth May 18 '15 at 20:22
• However, your example is far from a MWE... So I will not try to decipher that ;) – zeroth May 18 '15 at 20:27
• It is not my example :-) But I have seen the same warning when using the \label & \ref facilities to plot a common legend for a groupplot... I agree it is useful to have the ability to specify each legend individually, but it would be nice not to give a warning when having a common legend is desired. That said, I don't know how everything is put together, and maybe its not possible – darthbith May 18 '15 at 20:30
• There are a bunch of corner cases to take into account. Again I would advice you to move it down to the respective ≠xtgroupplot\nextgroupplot or do, A.sty≤={≤≥ndstuff...}A/.style={legend stuff...} and put AA in the ≠xtgroupplot\nextgroupplot. In that way it seems common. (I know, just an idea if you want the stuff collected). – zeroth May 18 '15 at 20:41