5

This question already has an answer here:

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 Nov 27 '14 at 12:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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
8

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.