tabularx 环境中的对齐

tabularx 环境中的对齐

我想要生成一个大致如下所示的表格:

我想使用 tabularx 使位于第二行下方和第一列右侧的单元格具有等距单元格。

所需表

这是我的乳胶代码:

\documentclass{article}

\usepackage{tabularx}
\usepackage{booktabs}

\begin{document}

\begin{tabularx}{.5\textwidth}{l *{6}{X} }
\toprule
 & \multicolumn{2}{c}{first} & \multicolumn{2}{c}{second} & \multicolumn{2}{c}{third} \\
\multicolumn{1}{c}{data set} & foo & bar & foo & bar & foo & bar \\
\midrule
d1 & 28.4 & 4.4 & 25.9 & 8.9 & -3.7 & 33.3 \\
d2 & 2.8 & 3.7 & 5.1 & 16.3 & 47.0 & 0.1 \\
d3 & 32.1 & 4.2 & 31.5 & 4.0 & 23.0 & 3.3 \\
\bottomrule
\end{tabularx}

\end{document}

这是由 pdflatex 生成的:

由 latex 生成的表格

我需要 tabularx,因为我想给出表格的总宽度,并让 tabularx(或类似的包)进行长度计算。所有数字都应右对齐,并且数字不应在减号和字母之间换行。

答案1

不要tabularx在数字数据上使用:-)

在此处输入图片描述

\documentclass{article}

\usepackage{dcolumn}
\usepackage{booktabs}

\begin{document}

\begin{center}
\begin{tabular}{l *{6}{D..{3.1}}}
\toprule
 & \multicolumn{2}{c}{first} & \multicolumn{2}{c}{second} & \multicolumn{2}{c}{third} \\
\multicolumn{1}{c}{data set} & 
\multicolumn{1}{c}{foo} &
\multicolumn{1}{c}{bar} &
\multicolumn{1}{c}{foo} &
\multicolumn{1}{c}{bar} &
\multicolumn{1}{c}{foo} &
\multicolumn{1}{c}{bar} \\
\midrule
d1 & 28.4 & 4.4 & 25.9 & 8.9 & -3.7 & 33.3 \\
d2 & 2.8 & 3.7 & 5.1 & 16.3 & 47.0 & 0.1 \\
d3 & 32.1 & 4.2 & 31.5 & 4.0 & 23.0 & 3.3 \\
\bottomrule
\end{tabular}
\end{center}

\end{document}

答案2

你想要siunitx;如果你真的想要展开表格(不要,请尊重你的读者;-)),使用tabular*

\documentclass{article}

\usepackage{siunitx}
\usepackage{booktabs}

\begin{document}

\begin{center}
\begin{tabular}{
  l
  S[table-format=2.1]
  S[table-format=1.1]
  S[table-format=2.1]
  S[table-format=2.1]
  S[table-format=-1.1]% the minus covers the second digit
  S[table-format=2.1]
}
\toprule
 & \multicolumn{2}{c}{first} & \multicolumn{2}{c}{second} & \multicolumn{2}{c}{third} \\
data set & {foo} & {bar} & {foo} & {bar} & {foo} & {bar} \\
\midrule
d1 & 28.4 & 4.4 & 25.9 & 8.9 & -3.7 & 33.3 \\
d2 & 2.8 & 3.7 & 5.1 & 16.3 & 47.0 & 0.1 \\
d3 & 32.1 & 4.2 & 31.5 & 4.0 & 23.0 & 3.3 \\
\bottomrule
\end{tabular}
\end{center}

\begin{center}
\begin{tabular*}{\textwidth}{
  l@{\extracolsep{\fill}}
  S[table-format=2.1]@{\extracolsep{2\tabcolsep}}
  S[table-format=1.1]@{\extracolsep{\fill}}
  S[table-format=2.1]@{\extracolsep{2\tabcolsep}}
  S[table-format=2.1]@{\extracolsep{\fill}}
  S[table-format=-1.1]@{\extracolsep{2\tabcolsep}}
  S[table-format=2.1]
}
\toprule
 & \multicolumn{2}{c}{first} & \multicolumn{2}{c}{second} & \multicolumn{2}{c}{third} \\
data set & {foo} & {bar} & {foo} & {bar} & {foo} & {bar} \\
\midrule
d1 & 28.4 & 4.4 & 25.9 & 8.9 & -3.7 & 33.3 \\
d2 & 2.8 & 3.7 & 5.1 & 16.3 & 47.0 & 0.1 \\
d3 & 32.1 & 4.2 & 31.5 & 4.0 & 23.0 & 3.3 \\
\bottomrule
\end{tabular*}
\end{center}

\end{document}

在此处输入图片描述

答案3

这只是对@egreg 使用他的tabular表格的回答的较长评论,重写为tabularx

当空间允许时,我倾向于使用 重写tabularx。我使用X列来添加列之间的空间,不是对于数据列(我也建议siunitx这样做)。我将列数加倍,然后相应地调整空间列。请注意,类型a是组内,后面应该跟@{\hspace{some space}}或类似,这里我用来!{\quad}指定组中列之间的真实距离。在组外X使用(以不同的名称),请注意,a删除任何填充的位置不会。这给了我们我认为看起来更好的A填充。\cmidrule

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs,tabularx,array}
\newcolumntype{A}{X}
\newcolumntype{a}{@{}l@{}}
\begin{document}

\begin{center}
\begin{tabularx}{0.9\textwidth}{
  l
  A
  S[table-format=2.1]
  a!\quad
  S[table-format=1.1]
  A
  S[table-format=2.1]
  a!\quad
  S[table-format=2.1]
  A
  S[table-format=-1.1]% the minus covers the second digit
  a!\quad
  S[table-format=2.1]
}
\toprule
&& \multicolumn{3}{c}{first} && \multicolumn{3}{c}{second} &&
 \multicolumn{3}{c}{third} \\
 \cmidrule{3-5}
 \cmidrule{7-9}
 \cmidrule{11-13}
 data set && {foo} && {bar} && {foo} && {bar} && {foo} && {bar} \\
\midrule
d1 && 28.4 && 4.4 && 25.9 && 8.9 && -3.7 && 33.3 \\
d2 && 2.8 && 3.7 && 5.1 && 16.3 && 47.0 && 0.1 \\
d3 && 32.1 && 4.2 && 31.5 && 4.0 && 23.0 && 3.3 \\
\bottomrule
\end{tabularx}
\end{center}

\end{document}

在此处输入图片描述

答案4

如果由于某种原因,你只是必须使表格占据文本块的整个宽度,我建议您使用环境tabular*而不是环境tabularx。使用下面的代码,六个列间空间将全部等宽。

像其他答案一样,我建议您将数字数据与相应的小数点对齐。并且,请考虑清晰地组织表格标题。

在此处输入图片描述

\documentclass{article}
\usepackage{dcolumn,booktabs}
\newcolumntype{d}[1]{D..{#1}}
% Two handy shortcut macros:
\newcommand\mC[1]{\multicolumn{2}{c}{#1}} 
\newcommand\mc[1]{\multicolumn{1}{c}{#1}}

\begin{document}
\begin{table}[h!]
\setlength\tabcolsep{0pt} % let LaTeX figure out intercolumn space
\begin{tabular*}{\textwidth}{l @{\extracolsep{\fill}} *{6}{d{2.1}}}
\toprule
data set & \mC{first} & \mC{second} & \mC{third} \\
\cmidrule{2-3} \cmidrule{4-5} \cmidrule{6-7} & 
\mc{foo} & \mc{bar} & \mc{foo} & \mc{bar} & \mc{foo} & \mc{bar} \\
\midrule
d1 & 28.4 & 4.4 & 25.9 & 8.9 & -3.7 & 33.3 \\
d2 & 2.8 & 3.7 & 5.1 & 16.3 & 47.0 & 0.1 \\
d3 & 32.1 & 4.2 & 31.5 & 4.0 & 23.0 & 3.3 \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}

相关内容