我有一张表格,其中单元格可以包含一行或多行文本。我想让所有单元格内容垂直居中,同时水平使文本左对齐。到目前为止,我已经想出了以下代码:
\documentclass[final,10pt]{book}
\usepackage{longtable,booktabs}
\usepackage{array} % needed for "m" in longtable
\usepackage[paperwidth=9in,paperheight=3in,right=0.5in,bottom=0in,left=0.2in]{geometry}
\begin{document}
\thispagestyle{empty}
\noindent\begin{longtable}{@{} m{.30\paperwidth} m{.30\paperwidth} m{.30\paperwidth}@{}}
\toprule\addlinespace
\textbf{Header 1} & \textbf{Header 2} & \textbf{Header 3}
\\\addlinespace
\toprule\endhead
1. Lorem ipsum dolor sit amet (c1, r1) & This is second column (c2, r1), line one\newline This is second column (c2, r1), line two & This is third column (c3, r1), line one\newline This is third column (c3, r1), line two
\\\addlinespace\hline
2. Lorem ipsum dolor sit amet (c1, r2) & This is second column (c2, r2), line one\newline This is second column (c2, r2), line two & This is third column (c3, r2), line one\newline This is third column (c3, r2), line two
\\\addlinespace\hline
3. Lorem ipsum dolor sit amet (c1, r3) & c2, r3 & c3, r3
\\\addlinespace\hline
4. Lorem ipsum dolor sit amet (c1, r4) & c2, r4, line one \newline c2, r4, line two & c3, r4, line one\newline c3, r4, line two
\\\addlinespace
\bottomrule
\end{longtable}
All text in the cells should be centered vertically and left aligned horizontally.
\end{document}
结果是:
只有标题行的内容垂直居中。所有其他单元格均未垂直居中,并且它们在单元格顶部的填充量方面有所不同。
为什么这些单元格没有按预期居中?这难道不是longtable 定义中m
(来自array
包)应该做的事情吗?如何确保每个单元格中的文本垂直居中,无论其中有多少行文本?
答案1
您可能不想\addlinespace
在很多地方手动插入指令,而是希望通过指令增加\aboverulesep
和的值。另外,请考虑使用而不是。\belowrulesep
\addtolength
\midrule
\hline
\documentclass[final,10pt]{book}
\usepackage{booktabs}
\addtolength\aboverulesep{0.15ex} % extra spacing above and below rules
\addtolength\belowrulesep{0.35ex}
\usepackage{longtable, array} % "array" is needed for "m" in longtable
\usepackage[paperwidth=9in,paperheight=3in,right=0.5in,bottom=0in,left=0.2in]{geometry}
\begin{document}
\thispagestyle{empty}
\begin{longtable}{@{} *{3}{m{.30\paperwidth}} @{}}
\toprule
\textbf{Header 1} & \textbf{Header 2} & \textbf{Header 3} \\
\midrule[\heavyrulewidth] % make this rule as thick as \toprule
\endhead
\bottomrule
\endfoot
1. Lorem ipsum dolor sit amet (c1, r1) & This is second column (c2, r1), line one\newline This is second column (c2, r1), line two & This is third column (c3, r1), line one\newline This is third column (c3, r1), line two \\
\midrule
2. Lorem ipsum dolor sit amet (c1, r2) & This is second column (c2, r2), line one\newline This is second column (c2, r2), line two & This is third column (c3, r2), line one\newline This is third column (c3, r2), line two \\
\midrule
3. Lorem ipsum dolor sit amet (c1, r3) & c2, r3 & c3, r3 \\
\midrule
4. Lorem ipsum dolor sit amet (c1, r4) & c2, r4, line one \newline c2, r4, line two & c3, r4, line one\newline c3, r4, line two \\
\end{longtable}
\end{document}