# Error using newcommand “\begin{document} ended by end{center}”

I've noticed that I'm using a particular code snippet for including and centering a figure with its caption all over the place in my Masters thesis.

I decided that for cleanliness and flexibility, I should extract it into a \newcommand.

% This command creates a centered figure.
% The first argument is for the filepath, the second for the label,
% and the third is for the caption.
\newcommand{\cfigure}[3]{
\begin{center}
\end{center}
\begin{figure}[ht]
\centering
\includegraphics[width=1.0\textwidth]{#1}
\caption{#3}
\label{#2}
\end{figure}
\end{center}
}


The \newcommand statement compiles without error, but when I actually try to use it like so:

\cfigure{images/Redpin_10_point2_with_labels.png}{redpin_heuristic}{Graph depicting Redpin heuristic function over a realistic domain.}


I get hit with the following error:

"\begin{document} ended by \end{center}"


I'm not using it in any particular environment, just the document itself.

Any idea what's going on?

• You have an \end{center} just before the final curly brace which does not have a matching (beginning) \begin{center}. So, this is being taken as finishing the \begin{document} command. Perhaps you want to get rid of \end{center} in the third line of your \newcommand definition. – Masroor Dec 11 '13 at 1:38
• Welcome to TeX.SX! – Herr K. Dec 11 '13 at 1:49

You have at least one \end{center} too much:

\newcommand{\cfigure}[3]{
\begin{center}
% \end{center} % here - PS
\begin{figure}[ht]
\centering
\includegraphics[width=1.0\textwidth]{#1}
\caption{#3}
\label{#2}
\end{figure}
\end{center}
}


In fact, both center and \centering seems to be also too much.