How to center a pgfplot? [duplicate]

I am trying to center a graph made with pgfplots with \centering. If I put \centering within a group to limit its scope, the graph does not get centered. If I don't limit the scope of \centering, then everything gets centered.

\documentclass{minimal}
\usepackage{pgfplots}

\begin{document}
This text should not be centered, but the graph should be.

% \begingroup
\centering
\begin{tikzpicture}
\begin{axis}
\addplot coordinates { (0, 0) (1, 1)};
\end{axis}
\end{tikzpicture}
% \endgroup

This text should not be centered.
\end{document}


marked as duplicate by LaRiFaRi, David Carlisle horizontal-alignment StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Nov 27 '14 at 12:39

• If you do not want to use a float (figure) here, which I would recommend, you should just wrap it in \begin{center}\end{center} – LaRiFaRi Nov 27 '14 at 12:19
• Note that \begingroup is plain TeX. You could use {} as well. The \centering-command needs a paragraph in order to see the end of its use. If you add a blank line in front of \endgroup, you will succeed as well. – LaRiFaRi Nov 27 '14 at 12:25
• @LaRiFaRi \begingroup is not necessarily Plain TeX. It's a primitive, but it's used everywhere. – Manuel Nov 27 '14 at 12:41

There is nothing special about tikzpicture here you would see the same with

{\centering X}


\centering works by setting the paragraph parameters, so if you close the group before the paragraph ends then nothing happens. You can use

{\centering
X

}


To centre an X (or a tikzpicture). Or more naturally use

\begin{center}
X
\end{center}


which also adds some vertical space.