0

I want to horizontally center a table that is generated with tabularx. But the tabularx table has an offset to the right that I am not able to remove:

\begin{table}
\small
\centering
\noindent
\begin{tabularx}{}{@{}X|X|X@{}}\hline
a & b & c \\ \hline
d & e & f
\\\hline
\end{tabularx}
\caption{This table is generated with tabularx. This table is generated with tabularx. This table is generated with tabularx. This table is generated with tabularx. This table is generated with tabularx. This table is generated with tabularx. }
\end{table}

\begin{table}
\small
\centering
\noindent
\begin{tabular}{ccc}\hline
a & b & c \\ \hline
d & e & f
\\\hline
\end{tabular}
\caption{This table is generated with tabular. This table is generated with tabular. This table is generated with tabular. This table is generated with tabular. This table is generated with tabular. This table is generated with tabular.}
\end{table}

The output looks like this: Screenshot of rendered tables comparing tabularx and tabular.

How can I horizontally center the table in tabularx?

8
  • 3
    Unfortunately you're using tabularx incorrectly. You need to specify a width as the second argument - something you've left blank. Even so, in its current form, you don't need a tabularx as it suffices to use all "natural width" columns like r, c and l like in your use with tabular.
    – Werner
    Mar 1, 2019 at 17:41
  • Do you know how to fix this if I had a table that required tabularx? Mar 1, 2019 at 18:02
  • 1
    Specify a width in the second argument of tabularx, as in \begin{tabularx}{.5\linewidth}{<col spec>}, where <col spec> has to have at least one X-column.
    – Werner
    Mar 1, 2019 at 18:03
  • If a code results in an error (! Missing number, treated as zero. in your case) don't even look at the 'output' but instead try to fix the error.
    – leandriis
    Mar 1, 2019 at 18:38
  • 1
    never ignore tex error messages, tex does not attempt to make usable pdf output after an error. Mar 1, 2019 at 19:15

1 Answer 1

2

tabularx requires you to specify a width of the resulting table in order for it to calculate the widths of X-columns. So, use it like this:

enter image description here

\documentclass{article}

\usepackage{tabularx}

\begin{document}

\begin{table}[t]
  \small
  \centering
  \begin{tabularx}{10em}{ @{} X | X | X @{} }
    \hline
    a & b & c \\
    \hline
    d & e & f \\
    \hline
  \end{tabularx}
  \caption{This table is generated with \texttt{tabularx}.}
\end{table}

\begin{table}[t]
  \small
  \centering
  \begin{tabular}{ *{3}{c} }
    \hline
    a & b & c \\
    \hline
    d & e & f \\
    \hline
  \end{tabular}
  \caption{This table is generated with \texttt{tabular}.}
\end{table}

Some regular text.

\end{document}

You must log in to answer this question.

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