![垂直对齐(居中)不起作用(多行、长表、书页标签)](https://linux22.com/image/473819/%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90%EF%BC%88%E5%B1%85%E4%B8%AD%EF%BC%89%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8%EF%BC%88%E5%A4%9A%E8%A1%8C%E3%80%81%E9%95%BF%E8%A1%A8%E3%80%81%E4%B9%A6%E9%A1%B5%E6%A0%87%E7%AD%BE%EF%BC%89.png)
我遇到了一个无法解决的问题,即使在网上搜索之后也是如此,我很抱歉,因为我对 Latex 还很陌生,而且这基本上是我的第一个表(需要跨越多页)。
如您所见,组 1 和所有区域均未垂直居中。此外,“表格末尾”注释也未正确水平居中。我尝试应用我找到的解决方案,但均无效 - 我相信我在这里做错了什么...
代码:
\documentclass{article}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pdflscape}
\begin{document}
\begin{landscape}
\begin{longtable}{p{5cm} @{}l p{10cm}}
\toprule
Risk Category & Zone & Description \\
\midrule
\endfirsthead
% Longtable rules for subsequent pages
\multicolumn{3}{c}%
{{\tablename\ \thetable{} -- Continued from previous page}} \\
\toprule
Risk Category & Zone & Description \\
\midrule
\endhead
% Footer for all pages except last page.
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\endfoot
% Footer forthe last page
\bottomrule
\multicolumn{3}{r}{{End of table.}} \\
\endlastfoot
\multirow{2}{*}{Group 1} & Zone A of Group 1 & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\cmidrule{2-3}
& Zone B of Group 1 & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column.\\
\midrule
& Zone C & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\midrule
& Zone D & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\end{longtable}
\end{landscape}
\end{document}
答案1
作为对 @cabohah 的精彩回答 (+1) 的补充,但使用了tabularray
包。使用其longtblr
表格,代码更清晰、更简洁(更短)。表格(在我看来)具有更吸引人的外观:
包的语法tabularray
与其他(经典)表略有不同。例如,它定义和而不是\multirow
和环境。有关更多详细信息,您需要阅读包文档。它包含许多示例。multicolumn
\SetCell[r=<num of rows>]{<style} <content>
\SetCell[c=<num of columns>]{<style} <content>
\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\usepackage{pdflscape}
\begin{document}
\begin{landscape}
\begin{longtblr}[
caption = {does your table has a caption?},
label = {tab:long}
]{colspec = {*{2}{Q[l]} X[l,m]},
cell{2}{1} = {r=2}{},
rowhead = 1,
}
\toprule
Risk Category & Zone & Description \\
\midrule
\SetCell[r=2]{} Group 1
& Zone A of Group 1
& This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\midrule
& Zone B of Group 1
& This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column.\\
\midrule
& Zone C & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\midrule
& Zone D & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\midrule[\heavyrulewidth]
\SetCell[c=3]{r} End of table. \\
\end{longtblr}
\end{landscape}
\end{document}
答案2
的第一个强制参数\multirow
是行数。您的表格有多行表格行,并且还使用booktab
行。因此该值2
太低。增加数字可以帮助改善垂直居中。
要垂直居中“区域”单元格,我建议使用m
-columns 而不是p
-columns。它们垂直居中到行的基线。m
-columns 需要包array
。
例子:
\documentclass{article}
\usepackage{array}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pdflscape}
\begin{document}
\begin{landscape}
\begin{longtable}{m{5cm} @{}l m{10cm}}
\toprule
Risk Category & Zone & Description \\
\midrule
\endfirsthead
% Longtable rules for subsequent pages
\multicolumn{3}{c}%
{{\tablename\ \thetable{} -- Continued from previous page}} \\
\toprule
Risk Category & Zone & Description \\
\midrule
\endhead
% Footer for all pages except last page.
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\endfoot
% Footer forthe last page
\midrule[\heavyrulewidth]
\multicolumn{3}{r}{{End of table.}} \\
\endlastfoot
\multirow[c]{3}{*}{Group 1} & Zone A of Group 1 & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\cmidrule{2-3}
& Zone B of Group 1 & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column.\\
\midrule
& Zone C & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\midrule
& Zone D & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\end{longtable}
\end{landscape}
\end{document}
顺便说一句:对于第一列,我认为l
会更好。例如,使用包xltabular
可以自动检测最后一列的宽度:
\documentclass{article}
\usepackage{xltabular}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pdflscape}
\begin{document}
\begin{landscape}
\renewcommand*{\tabularxcolumn}[1]{m{#1}}
\begin{xltabular}{\linewidth}{llX}
\toprule
Risk Category & Zone & Description \\
\midrule
\endfirsthead
% Longtable rules for subsequent pages
\multicolumn{3}{c}%
{{\tablename\ \thetable{} -- Continued from previous page}} \\
\toprule
Risk Category & Zone & Description \\
\midrule
\endhead
% Footer for all pages except last page.
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\endfoot
% Footer forthe last page
\midrule[\heavyrulewidth]
\multicolumn{3}{r}{{End of table.}} \\
\endlastfoot
\multirow[c]{3}{*}{Group 1} & Zone A of Group 1 & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\cmidrule{2-3}
& Zone B of Group 1 & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column.\\
\midrule
& Zone C & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\midrule
& Zone D & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\end{xltabular}
\end{landscape}
\end{document}
注意:我没有水平居中“表格末尾”,因为您似乎已经发现,您必须使用\multicolumn{3}
来跨越所有三列。但是,如果您不想让文本居中右对齐,则需要\multicolumn{3}{c}{{End of table.}}
。