35

I create a table that I would like centered on the page horizontally. I scale the table size using \resizebox. However, doing so, left aligns the table. How do I center it?

Here's a MWE:

\documentclass[11pt,english,titlepage]{article}
\usepackage{graphicx}


\begin{document}

\begin{table}
    \caption{mytable}
    \resizebox{.5\textwidth}{!}{

    \begin{centering}
        \begin{tabular}{rrr}
            A                     & B                  & C \\
            \hline
            A1                   & B1             &C1\\
            A2                  & B2                  & C2 \\
            A3                    & B3             & C3 \\
        \end{tabular}
    \end{centering}
    }
\end{table}

\end{document}
Martin Scharrer
  • 262,582
Alex
  • 2,111

2 Answers2

57

There is no centering environment. And issuing \centering inside \resizebox doesn't make sense anyway: it should be outside, because you want to center the resized box.

\documentclass[11pt,english,titlepage]{article}
\usepackage{graphicx}

\begin{document}

\begin{table}
\centering
\caption{mytable}
\resizebox{.5\textwidth}{!}{% <------ Don't forget this %
  \begin{tabular}{rrr}
  A              & B            & C \\
  \hline
  A1             & B1           & C1 \\
  A2             & B2           & C2 \\
  A3             & B3           & C3 \\
  \end{tabular}% <------ Don't forget this %
}
\end{table}

\end{document}
egreg
  • 1,121,712
  • 4
    @egreg: why do you need the % there? – Alex Jul 28 '16 at 04:21
  • 10
    @Alex Without them you'd get spaces on either side of the table – egreg Jul 28 '16 at 07:35
  • @egreg, how do you do if not within a table environment? Using just {\centering .... } as discussed in https://tex.stackexchange.com/questions/162462/how-to-center-the-table-in-latex does not seem to work? Thanks! – Matifou Feb 09 '19 at 02:17
  • @Matifou while there is no centering environment, there is a center environment. That can definitely center a resized box. – corwin.amber Oct 15 '21 at 11:44
5

Here an alternative answer using adjustbox. It covers the functionality of \resizebox and is also able to center its content. It also removes the need of escaping the line breaks with % as seen in egregs answer, as the adjustbox environment removes the spaces added by them.

It is even possible to produce the table environment using adjustbox keys, or even to get the whole tabular environment replaced. This saves you a lot of typing in the long run. Note that the order of most adjustbox keys matter.

\documentclass[11pt,english,titlepage]{article}
\usepackage{adjustbox}

\begin{document}

\begin{table}
\caption{mytable}% will not give you proper skip as it is configured for placement below the content
\begin{adjustbox}{width=.5\textwidth,center}
  \begin{tabular}{rrr}
  A              & B            & C \\
  \hline
  A1             & B1           & C1 \\
  A2             & B2           & C2 \\
  A3             & B3           & C3 \\
  \end{tabular}
\end{adjustbox}
\end{table}

% shorter, also covering the `table` environment
\begin{adjustbox}{width=.5\textwidth,center,caption=mytable,float=table}
  \begin{tabular}{rrr}
  A              & B            & C \\
  \hline
  A1             & B1           & C1 \\
  A2             & B2           & C2 \\
  A3             & B3           & C3 \\
  \end{tabular}
\end{adjustbox}

% even shorter, now also covering the `tabular` environment
\begin{adjustbox}{tabular=rrr,width=.5\textwidth,center,caption=mytable,float=table}
  A              & B            & C \\
  \hline
  A1             & B1           & C1 \\
  A2             & B2           & C2 \\
  A3             & B3           & C3 \\
\end{adjustbox}

\end{document}
Martin Scharrer
  • 262,582