335

What is the correct way to center figures and tables (figure, table)?

\begin{center}
...
\end{center}

or

\begin{centering}
...
\end{centering}
1

3 Answers 3

412

The correct way is

\begin{figure}
\centering
... (Code for pictures, captions) ...
\end{figure}

\begin{center} ... \end{center} inside a figure environment will result in (generally unwanted) additional vertical space.

Note that while \centering produces proper spacing, manually adding this command to every figure environment (and to every table and custom-made float) is tedious and goes against the idea of separating the content of a document from the format. Even better ways are to add the following to your document preamble (thanks to egreg for the tip):

\makeatletter
\g@addto@macro\@floatboxreset\centering
\makeatother

or to load the floatrow package which allows to control the justification of float contents from the preamble (with objectset=centering as default).

8
  • 78
    Also note that \centering is a declaration, not an environment. That \begin{centering} works is an unfortunate consequence of the way environments work internally in LaTeX: The environment foo essentially consist of two macros \foo and \endfoo which are called by \begin{foo} and \end{foo}, respectively (along with some error checking, such as proper nesting). But since \endfoo is called via \csname endfoo\endcsname, that macro doesn't actually have to exist. This means that even stuff such as $\begin{alpha}\end{alpha}$ works (but should of course not be used).
    – Villemoes
    Commented Dec 1, 2010 at 10:56
  • 27
    There's also the more esoteric \g@addto@macro\@floatboxreset\centering which, of course, should be preceded by \makeatletter and followed by \makeatother.
    – egreg
    Commented Apr 10, 2011 at 14:25
  • 2
    @Villemoes So why so many LaTeX editors use snippets for insert figures that use yet the center environment instead the centering declaration?
    – Aradnix
    Commented Sep 30, 2014 at 3:09
  • 3
    For use with IEEEtran, \g@addto@macro\@floatboxreset\centering is ineffective since \figure redefines \@floatboxreset. I use etoolbox and \patchcmd{\figure}{\normalsize}{\normalsize\centering}{}{}.
    – bers
    Commented Jan 15, 2016 at 14:42
  • 1
    @bers This command is not working anymore. How to fix it?
    – stackname
    Commented Nov 9, 2021 at 22:16
63

This is a (very late!) supplement to lockstep's answer which just offers a visual demonstration of the difference between the use of \centering and the center environment within figure environments.

Each page shows 2 figures, one using \centering and one using center. The differences in spacing are the result of ordering the two figure environments differently. On the first page, \centering is used first and the center environment second, while on the second page, this order is reversed.

The results clearly show inappropriate spacing for the lower figure (first page) and the upper figure (second page) i.e. for whichever figure uses center rather than \centering.

showframe is used to show the overall page layout.

illustration of differences between <code>\centering</code> and <code>center</code> environment inside <code>figure</code> environments

\documentclass{article}
\usepackage{graphicx,showframe,kantlipsum}
\begin{document}
  \kant[1]
  \begin{figure}
    \centering
    \includegraphics[scale=.25]{example-image-a}
    \caption{Figure with centering}
  \end{figure}
  \kant[2]
  \begin{figure}
    \begin{center}
      \includegraphics[scale=.25]{example-image-a}
    \end{center}
    \caption{Figure in center environment}
  \end{figure}
  \kant[3]
  \begin{figure}
    \begin{center}
      \includegraphics[scale=.25]{example-image-a}
    \end{center}
    \caption{Figure in center environment}
  \end{figure}
  \kant[4]
  \begin{figure}
    \centering
    \includegraphics[scale=.25]{example-image-a}
    \caption{Figure with centering}
  \end{figure}
\end{document}

Finally, compare two pages with two figures each. The first page includes figures which use \centering, while the second includes figures which use the center environment.

Further comparisons

\documentclass{article}
\usepackage{graphicx,showframe,kantlipsum}
\begin{document}
  \kant[1]
  \begin{figure}
    \centering
    \includegraphics[scale=.25]{example-image-a}
    \caption{Figure with centering}
  \end{figure}
  \kant[2]
  \begin{figure}
    \centering
    \includegraphics[scale=.25]{example-image-a}
    \caption{Figure with centering}
  \end{figure}
  \kant[3]
  \begin{figure}
    \begin{center}
      \includegraphics[scale=.25]{example-image-a}
    \end{center}
    \caption{Figure in center environment}
  \end{figure}
  \kant[4]
   \begin{figure}
    \begin{center}
      \includegraphics[scale=.25]{example-image-a}
    \end{center}
    \caption{Figure in center environment}
  \end{figure}
\end{document}
16

Since this thread gave birth to a little misunderstanding, I would like to add a note.

As the other answers say, center environment should never be used within a figure or table environment, you should use \centering instead:

enter image description here

But if your table or image are not floating, that is you would like to have them exactly where you put them, i.e. they are not within a figure or a table environment, you can use a center environment without problems. It is equivalent to a table or a figure environment with the H option of float package.

If you would like to add a caption, you can use \captionof from \caption package.

\documentclass{article}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{caption}
\captionsetup[table]{position=above}
\usepackage{float}

\begin{document}
You can use \texttt{center} environments here, because they are not within a floating one:
\begin{center}
    \captionof{table}{A non-floating table within a \texttt{center} environment}
    \begin{tabular}{cc}
        \toprule
         Ducks & Lions \\
         \midrule
         1 & 2 \\
         \bottomrule
    \end{tabular}
\end{center}
\begin{center}
    \includegraphics[width=.5\linewidth]{example-image-a}
    \captionof{figure}{A non-floating figure within a \texttt{center} environment}
\end{center}

They are equivalent to a \texttt{table} or \texttt{figure} environment with the \texttt{H} option of \texttt{float} package:
\begin{table}[H]
    \centering
    \caption{A non-floating table with \texttt{H} option}
    \begin{tabular}{cc}
        \toprule
         Ducks & Lions \\
         \midrule
         1 & 2 \\
         \bottomrule
    \end{tabular}
\end{table}
\begin{figure}[H]
    \centering
    \includegraphics[width=.5\linewidth]{example-image-a}    \caption{A non-floating figure with \texttt{H} option}
\end{figure}

Just to show the also the lists works:
\listoftables
\listoffigures

\end{document}

enter image description here enter image description here

2
  • How about flushleft and flushright a table or a figure?
    – Black Mild
    Commented Sep 14, 2019 at 12:43
  • 1
    @BlackMild There are flushleft and flushright environments, as well as \raggedright and \raggedleft commands
    – CarLaTeX
    Commented Sep 14, 2019 at 20:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .