How should I run the following example code using
/pgfplots/legend to name={<name>}to put a shared legend below a group of horizontally aligned figures?
The example code using /pgfplots/legend to name={<name>} in the pgfplots manual under "Legends outside of an axis" is as follows (the first two lines are added by myself).
\documentclass{standalone}
\usepackage{pgfplots}
% Preamble: \pgfplotsset{width=7cm,compat=1.3}
\begin{document}
\pgfplotsset{footnotesize,samples=10}
\begin{center}% note that \centering uses less vspace...
\begin{tikzpicture}
\begin{axis}[
legend columns=-1,
legend entries={$(x+0)^k$;,$(x+1)^k$;,$(x+2)^k$;,$(x+3)^k$},
legend to name=named,
title={$k=1$}]
\addplot {x};
\addplot {x+1};
\addplot {x+2};
\addplot {x+3};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[title={$k=2$}]
\addplot {x^2};
\addplot {(x+1)^2};
\addplot {(x+2)^2};
\addplot {(x+3)^2};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[title={$k=3$}]
\addplot {x^3};
\addplot {(x+1)^3};
\addplot {(x+2)^3};
\addplot {(x+3)^3};
\end{axis}
\end{tikzpicture}
\\
\ref{named}
\end{center}
\end{document}
It should generate a shared legend below a group of three horizontally aligned figures:

However, it throws the error
Something is wrong -- perhaps a missing \item, and generates a shared legend to the right.

The compiler blames the begin{center} end{center} environment. However, commenting them out does not help.
standalone? – LaRiFaRi Jul 02 '15 at 07:41pgfplotsmanual. I have only added the first two lines\documentclass{standalone} \usepackage{pgfplots}. – hengxin Jul 02 '15 at 07:45\begin{document}\end{document}. I would do this with groupplots like I did here: http://tex.stackexchange.com/a/248151 When you are instandalone, thecentermakes no sense. – LaRiFaRi Jul 02 '15 at 07:48\begin{document}\end{document}added. Thanks. I am usingstandalonebecause I want to generate a .pdf file and then import it into my main .tex file. I changedstandalonetoarticle, and it generated too much space around the figure. – hengxin Jul 02 '15 at 07:56article, and runpdfcropon the PDF afterwards. – Torbjørn T. Jul 02 '15 at 08:11groupplots. Thanks. – hengxin Jul 02 '15 at 09:49groupplots. – hengxin Jul 02 '15 at 09:50