我在表中使用了固定宽度的列,并且同时使用了longtable
和booktabs
包(以及tabularx
因为\newcolumntype
命令)。
在我的表中,我将有大量的数字数据我想右对齐的单元格。 然而,我仍然希望保持标题居中对齐,因为有些标题文本会被分成两行(或更多行)。
我该如何解决这个问题?这是一个小的测试用例:
\documentclass{article}
\usepackage[brazilian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{longtable}
\usepackage{tabularx}
\usepackage{booktabs}
\title{Table header aligment}
\begin{document}
\newcolumntype{D}{m{20mm}}
\newcolumntype{L}{>{\raggedright\arraybackslash}m{20mm}}
\newcolumntype{C}{>{\centering\arraybackslash}m{20mm}}
\newcolumntype{R}{>{\raggedleft\arraybackslash}m{20mm}}
\begin{longtable}{DLCR}
\toprule
I want this header text centered &
I want this header text centered &
I want this header text centered &
I want this header text centered \\
\midrule
\endhead
Justified alignment &
Left alignment &
Center alignment &
Right alignment \\
% This row is just to show which columns will hyphenate, and which ones won't
Love alignment &
Love alignment &
Love alignment &
Love alignment \\
\midrule
% I wanna keep the numbers of this column aligned to the right, while keeping
% the headers aligned to the center.
dummy text & dummy text & dummy text & 123456 \\
dummy text & dummy text & dummy text & 1234 \\
dummy text & dummy text & dummy text & 456 \\
\bottomrule
\end{longtable}
\end{document}
答案1
将表格标题更改为以下内容是否存在问题:
\toprule
\centering I want this header text centered &
\centering I want this header text centered &
\centering I want this header text centered &
\centering I want this header text centered \tabularnewline
\midrule
答案2
我建议使用
\multicolumn{1}{C}{I want this header text centered}
\multicolumn
允许更改单个单元格的格式。这里您已经定义了 C 类型,因此只需使用此类型即可。虽然\centering
在单元格中直接使用 是可行的,但它会改变 的含义\\
,因此您可能必须\arraybackslash
在之后使用 或使用\tabularnewline
代替\\
。您的C
类型已经内置了它。
由于您多次使用该格式,也许在进一步的表格中,您可以为其定义一个快捷方式,例如
\newcommand{\centercell}[1]{\multicolumn{1}{C}{#1}}
为了进一步实现语义标记,我将定义一个头部细胞样式并只写
\head{This is my column head}
在文档主体中并在标题中定义我想要的格式文档宽度,例如
\newcommand{\head}[1]{\centercell{\bfseries#1}}
这样以后更改起来就简单且一致了。想象一下,您以后决定省略居中 - 而不是\centering
在文档正文中搜索和删除,只需在序言中进行小幅更改即可。在这里,您可以快速切换到粗体或中等字体,或者切换\Centering
到ragged2e
。这没有问题,因为你将格式与内容分开了。
答案3
从 2012 年起(实际上从 2011 年开始),我们还可以选择使用禁忌包。此处描述的问题可以通过灵活的\rowfont
命令解决。我还加载了 ragged2e 包,以获得更漂亮的粗糙(右侧)文本。
您不需要定义居中、对齐、左对齐或右对齐的列类型。它们已经定义好了。但是tabu
缩进对齐的段落,所以有时 anoindent
是必要的。
为了避免不良间距(即使booktabs
已加载),\tabulinesep=
-command 很重要。
\documentclass{article}
\usepackage[UKenglish]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{longtable,tabu}
\usepackage{booktabs}
\usepackage[newcommand]{ragged2e}
\title{Table header aligment}
\begin{document}
\tabulinesep=1.2mm % Always use this command to avoid bad spacing
\begin{longtabu} to 90mm {>{\noindent}X[J]X[L]X[C]X[R]}
\toprule\rowfont[c]\bfseries
I want this header text centred &
I want this header text centred &
I want this header text centred &
I want this header text centred \\
\midrule
\endhead
Justified alignment &
Left alignment &
Centre alignment &
Right alignment \\
% This row is just to show which columns will hyphenate, and which ones won't
Love alignment &
Love alignment &
Love alignment &
Love alignment \\
\midrule
% I want to keep the numbers of this column aligned to the right, while keeping
% the headers aligned to the centre.
dummy text & dummy text & dummy text & 123456 \\
dummy text & dummy text & dummy text & 1234 \\
dummy text & dummy text & dummy text & 456 \\
\bottomrule
\end{longtabu}
\end{document}
得出:
答案4
longtblr
环境的替代解决方案tabularray
包裹:
\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{longtblr}[
caption = {Long Table},
label = {tblr:test},
]{
colspec = {Q[m,j,20mm]Q[m,l,20mm]Q[m,c,20mm]Q[m,r,20mm]},
row{1} = {c},
rowhead = 1,
}
\toprule
I want this header text centered & I want this header text centered & I want this header text centered & I want this header text centered \\
\midrule
Justified alignment & Left alignment & Center alignment & Right alignment \\
Love alignment & Love alignment & Love alignment & Love alignment \\
\midrule
dummy text & dummy text & dummy text & 123456 \\
dummy text & dummy text & dummy text & 1234 \\
dummy text & dummy text & dummy text & 456 \\
\bottomrule
\end{longtblr}
\end{document}