我想将文本垂直对齐到每个单元格的顶部,在tabular
某些单元格必须跨越多行。
如果可能的话,尽量不要使用其他软件包。(我无法控制其他软件包的安装。)
\documentclass[12pt, margin = 1mm]{standalone}
\newcommand\TS{\rule{0pt}{2.6ex}} % Top strut
\newcommand\BS{\rule[-0.9ex]{0pt}{0pt}} % Bottom strut
\usepackage{array, multirow}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\TS\BS\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering \let\newline\\\TS\BS\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft \let\newline\\\TS\BS\arraybackslash}p{#1}}
\begin{document}
\begin{tabular} [t] { C{2cm} C{3cm} | C{2cm} C{3cm} }
Column 1 & Column 2 & Column 3 & Column 4 \\ \hline
Foo & Line 1 \newline Line 2 &
\multirow{2}{*}{Alpha} & Line 1 \newline Line 2 \newline Line 3 \\
Bar & Line 1 \newline Line 2 \\
Spam & Line 1 \newline Line 2 &
\multirow{2}{*}{Beta} & Line 1 \newline Line 2 \newline Line 3 \\
Eggs & Line 1 \newline Line 2 \\
\end{tabular}
\end{document}
存在两个问题:
使用
p{#1}
Instead ofm{#1}
有助于将 Foo、Bar、Spam、Eggs 对齐到顶部,但不能将 Alpha、Beta 对齐。该[t]
参数不起作用。Alpha 及其 3 条线不跨越 Foo、Bar 的行。Beta 也是如此。
我们怎样才能解决这两个问题呢?
答案1
像这样?
没有multirow
,但有简化的列定义类型并使用makecell
:
\documentclass[12pt, margin=3mm]{standalone}
\usepackage{array, makecell}
\setcellgapes{3pt}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\begin{document}
{\makegapedcells
\begin{tabular} { C{2cm} C{3cm} | C{2cm} C{3cm} }
Column 1 & Column 2 & Column 3 & Column 4 \\
\hline
Foo & \makecell[t]{Line 1\\ Line 2} & Alpha & \makecell[t]{Line 1\\ Line 2} \\
Bar & \makecell[t]{Line 1\\ Line 2} & & \\
Spam & \makecell[t]{Line 1\\ Line 2} & Beta & \makecell[t]{Line 1\\ Line 2} \\
Eggs & \makecell[t]{Line 1\\ Line 2} & & \\
\end{tabular}
}
\end{document}
编辑(1):或者您正在寻找简单的表格:
{\makegapedcells
\begin{tabular} { C{2cm} C{3cm} | C{2cm} C{3cm} }
Column 1 & Column 2 & Column 3 & Column 4 \\
\hline
Foo & Line 1 & Alpha & Line 1 \\
& Line 2 & & Line 2 \\
Bar & Line 1 & & \\
& Line 2 & & \\
Spam & Line 1 & Beta & Line 1 \\
& Line 2 & & Line 2 \\
Eggs & Line 1 & & \\
& Line 2 & & \\
\end{tabular}
}
垂直间距略有不同:
编辑(2):解决方案multirow
(正如 op 所希望的那样,但不幸的是我并不清楚应该跨越哪一行/多少行)
\documentclass[12pt, margin=3mm]{standalone}
\usepackage{array, makecell, multirow}
\setcellgapes{3pt}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\begin{document}
{\makegapedcells
\renewcommand\multirowsetup{\centering}% <-- for centering contents of multirow center
\begin{tabular} { C{2cm} C{3cm} | C{2cm} C{3cm} }
Column 1 & Column 2 & Column 3 & Column 4 \\
\hline
\multirow[t]{2}{=}{Foo} & Line 1 & \multirow[t]{4}{=}{Alpha} & Line 1 \\
& Line 2 & & Line 2 \\
\multirow[t]{2}{=}{Bar} & Line 1 & & \\
& Line 2 & & \\
\multirow[t]{2}{=}{Spam}& Line 1 & \multirow[t]{4}{=}{Beta} & Line 1 \\
& Line 2 & & Line 2 \\
\multirow[t]{2}{=}{Eggs}& Line 1 & & \\
& Line 2 & & \\
\end{tabular}
}
\end{document}
如果 s 单元格中只有一个单词,则结果看起来与之前相同(在编辑(1)中)multirow
。如果有更多单词,例如:
\begin{tabular} { C{2cm} C{3cm} | C{2cm} C{3cm} }
Column 1 & Column 2 & Column 3 & Column 4 \\
\hline
\multirow[t]{2}{=}{Foo} & Line 1 & \multirow[t]{4}{=}{Alpha} & Line 1 \\
& Line 2 & & Line 2 \\
\multirow[t]{2}{=}{Bar} & Line 1 & & \\
& Line 2 & & \\
\multirow[t]{2}{=}{Spam}& Line 1 & \multirow[t]{4}{=}{Beta Beta Beta Beta Beta
Beta Beta Beta Beta Beta}
& Line 1 \\
& Line 2 & & Line 2 \\
\multirow[t]{2}{=}{Eggs}& Line 1 & & \\
& Line 2 & & \\
\end{tabular}
结果是: