![无法将多行单元格与单行单元格对齐](https://linux22.com/image/452209/%E6%97%A0%E6%B3%95%E5%B0%86%E5%A4%9A%E8%A1%8C%E5%8D%95%E5%85%83%E6%A0%BC%E4%B8%8E%E5%8D%95%E8%A1%8C%E5%8D%95%E5%85%83%E6%A0%BC%E5%AF%B9%E9%BD%90.png)
我正在尝试对齐一行单元格,其中一些单元格只有 1 行,而其他单元格有 2 行。MWE:
\usepackage{dcolumn}
\usepackage{makecell}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\begin{table}[]
% let LaTeX figure out intercolumn whitespace amount
\setlength\tabcolsep{0pt}
\centering
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{4}{d{1.2}} }
& \multicolumn{4}{c}{Dependent Variable ($+$ Additional Independent Variables)} \\
\cmidrule{2-5}
& \mc{Word 1 speed} & \begin{tabular}[c]{@{}c@{}}Word 1 speed \\ (+ intx)\end{tabular} & \mc{Word 2 speed} & \begin{tabular}[c]{@{}c@{}}Word 2 speed \\ (+ intx)\end{tabular} \\
\begin{tabular}[c]{@{}l@{}}Dialogue act \\ F-score\end{tabular} & 2.53^{*} & 2.52^{*} & 4.45^{****} & 4.45^{****} \\
\end{tabular*}
\end{table}
我也尝试过使用\makecell{}
多行单元格:
& Word 1 speed & \makecell{Word 1 speed \\ (+ intx)} & Word 2 speed & \makecell{Word 2 speed \\ (+ intx)} \\
但我的结果看起来完全不对。我做错了什么?
答案1
既然您加载了makecell
包,那么您应该使用它而不是嵌套表,例如像这样:
\documentclass{article}
\usepackage{dcolumn, makecell, booktabs}
\begin{document}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\newcommand{\mc}[2]{\multicolumn{1}{#1}{\makecell[#1]{#2}}} % handy shortcut macro
\begin{table}[]
% let LaTeX figure out intercolumn whitespace amount
\setlength\tabcolsep{0pt}
\centering
\begin{tabular*}{\textwidth}{ @{\extracolsep{\fill}} l *{4}{d{1.2}} }
& \multicolumn{4}{c}{Dependent Variable ($+$ Additional Independent Variables)} \\
\cmidrule{2-5}
& \mc{c}{Word 1 speed} & \mc{c}{Word 1 speed \\ (+ intx)} & \mc{c}{Word 2 speed} & \mc{c}{Word 2 speed \\ (+ intx)} \\
\mc{l}{Dialogue act \\ F-score} & 2.53^{*} & 2.52^{*} & 4.45^{****} & 4.45^{****} \\
\end{tabular*}
\end{table}
\end{document}
新的命令本质上是在宏内\mc
放置一个框,这显然是这里需要的,因为该列具有规范,您需要覆盖它以实现正确的对齐。我稍微改变了宏的原始代码,现在它需要两个参数,其中第一个参数降低内容的对齐方式(表示居中,表示左对齐)。对齐规范是通过宏可以采用的可选参数完成的。因此,如果宏的第一个参数是,例如,您将在左对齐宏内有一个左对齐,因为该参数被输入到两者中。我在这里只考虑了水平对齐。有关垂直对齐,请参阅 Zarko 的精彩答案。\makecell
\multicolumn
d
\mc
c
l
\makecell
\mc
l
\makecell
\multicolumn
答案2
像这样?
- 不清楚文本应该如何对齐。我认为,文本应该垂直居中。如果不是这种情况,那么你可以对齐:
- 多行文本顶部
\makecell[t]{...}
,或在 - 多行文本的底部
\makecell[b]{...}
。
- 多行文本顶部
- 所有列标题都必须位于
multicolumn
单元格中。为了简洁起见,我建议使用\NewExpandableDocumentCommand
命令:
\documentclass{article}
\usepackage{dcolumn}
\usepackage{booktabs, makecell}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\NewExpandableDocumentCommand\mcc{O{1}m}{\multicolumn{#1}{c}{#2}}
\begin{document}
\begin{table}[ht]
% let LaTeX figure out intercolumn whitespace amount
\setlength\tabcolsep{0pt}
\centering
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{4}{d{1.2}} }
& \mcc[4]{Dependent Variable ($+$ Additional Independent Variables)} \\
\cmidrule{2-5}
& \mcc{Word 1 speed}
& \mcc{\makecell{Word 1 speed \\ (+ intx) }}
& \mcc{Word 2 speed}
& \mcc{\makecell{Word 2 speed \\ (+ intx) }} \\
\midrule
\makecell{Dialogue act \\ F-score}
& 2.53^{*} & 2.52^{*} & 4.45^{****} & 4.45^{****} \\
\end{tabular*}
\end{table}
\end{document}
附录:
- 在多行列标题的顶部对齐:
...
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{4}{d{1.2}} }
& \mcc[4]{Dependent Variable ($+$ Additional Independent Variables)} \\
\cmidrule{2-5}
& \mcc{Word 1 speed}
& \mcc{\makecell[t]{Word 1 speed \\ (+ intx) }}
& \mcc{Word 2 speed}
& \mcc{\makecell[t]{Word 2 speed \\ (+ intx) }} \\
\midrule
...
- 与多行列标题底部对齐
...
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{4}{d{1.2}} }
& \mcc[4]{Dependent Variable ($+$ Additional Independent Variables)} \\
\cmidrule{2-5}
& \mcc{Word 1 speed}
& \mcc{\makecell[b]{Word 1 speed \\ (+ intx) }}
& \mcc{Word 2 speed}
& \mcc{\makecell[b]{Word 2 speed \\ (+ intx) }} \\
\midrule