[Tex/LaTex] How to center tabularx table

horizontal alignmenttablestabularx

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?

Best Answer

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}