如何使 tabularx 表格居中?

如何使 tabularx 表格居中?

我想将使用 tabularx 生成的表格水平居中。但是 tabularx 表格右侧有一个偏移,我无法删除:

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

输出如下所示: 比较 tabularx 和 tabular 的呈现表格的屏幕截图。

如何在 tabularx 中将表格水平居中?

答案1

tabularx需要您指定结果表的宽度,以便计算X列的宽度。因此,请像这样使用它:

在此处输入图片描述

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

相关内容