18

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}
30

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}
  • 2
    @egreg: why do you need the % there? – Alex Jul 28 '16 at 4:21
  • 6
    @Alex Without them you'd get spaces on either side of the table – egreg Jul 28 '16 at 7:35
  • @egreg, how do you do if not within a table environment? Using just {\centering .... } as discussed in tex.stackexchange.com/questions/162462/… does not seem to work? Thanks! – Matifou Feb 9 at 2:17
1

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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