我正在尝试指定一个自定义列命令,采用宽度参数并按内容的小数点对齐列。
我正在使用 tabularx 包来使用具有预定义宽度的列
p{<width>}
使用我定义的命令通过表格边距校正宽度\COLW{<width>}
(参见使用 tabularx 的表格布局(列宽:50%|25%|25%)以供参考)
我试图实现的是沿小数点对齐的列,这是由的“S”列选项提供的siunitx
。但这不允许指定列宽。我发现array
提供\centerdots
和\endcenterdots
命令的包可以使用自定义列类型“d”手动定义此行为,如
\newcolumntype{d}[1]{>{\centerdots\arraybackslash}p{#1}<{\endcenterdots}}
但不知何故这些命令没有被正确识别
undefined control sequence [...] \centerdots
有什么想法可以如何将点对齐的列与明确的宽度规范结合起来?
最小工作示例
\documentclass[a4paper,11pt]{report}
\usepackage{array} %should provide \centerdots command!?
\usepackage{tabularx} %tabularx environment with fixed size columns
\usepackage{ragged2e} %Provide \RaggedLeft and \RaggedRight commands
\usepackage{siunitx} %Add 'S' option in tables to align along decimal point
\newcommand{\COLW}[1]{\dimexpr#1\textwidth-2\tabcolsep-1.3333\arrayrulewidth} %defined column width with margin correction
\newcolumntype{R}[1]{>{\RaggedLeft\arraybackslash}p{#1}} %right-aligned column with given column width
\newcolumntype{L}[1]{>{\RaggedRight\arraybackslash}p{#1}} %left-aligned column with given column width
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} %centered column with given column width
\newcolumntype{d}[1]{>{\centerdots\arraybackslash}p{#1}<{\endcenterdots}} %dot-aligned column with given column width
\begin{document}
\begin{table}[hbtp!]
\centering
%this setup uses the 'S' column from siunitx package which properly centeres the content along the dot, BUT does not allow for column width specification
\begin{tabularx}{1.0\textwidth}{|L{\COLW{0.25}}|C{\COLW{0.25}}|R{\COLW{0.25}}|d{\COLW{0.25}}|}
%this setup uses \centerdots command from array package and defined column width, but doesn't work
%\begin{tabularx}{1.0\textwidth}{|L{\COLW{0.25}}|C{\COLW{0.25}}|R{\COLW{0.25}}|S|}
\hline
left-aligned & centered & right-aligned & \multicolumn{1}{c|}{dot-aligned} \\ %multicolumn as workaround for non decimal number conflicting with 'S' option
\hline
1 & 1 & 1 & 100.0 \\
2 & 2 & 2 & 10.0 \\
3 & 3 & 3 & 1.0 \\
4 & 4 & 4 & 0.1 \\
5 & 5 & 5 & 0.01 \\
6 & 6 & 6 & 0.001 \\
\hline
\end{tabularx}
\end{table}
\end{document}
答案1
tabularx
除非您指定至少一X
列以允许其拉伸该列以达到规定的宽度,否则您无法使用。
在这里我只需使用siunitx
(或dcolumn
)并通过指定数字的(更大)格式来控制列宽(如果需要),例如下面两列被强制加宽。
\documentclass[a4paper,11pt]{report}
\usepackage{array} %should provide \centerdots command!?
\usepackage{ragged2e} %Provide \RaggedLeft and \RaggedRight commands
\usepackage{siunitx} %Add 'S' option in tables to align along decimal point
\begin{document}
\begin{table}[hbtp!]
\centering
\begin{tabular}{SSS[table-format=6.2]S[table-format=4.4]}
\hline
1 & 1 & 1 & 100.0 \\
2 & 2 & 2 & 10.0 \\
3 & 3 & 3 & 1.0 \\
4 & 4 & 4 & 0.1 \\
5 & 5 & 5 & 0.01 \\
6 & 6 & 6 & 0.001 \\
\hline
\end{tabular}
\bigskip
or to a specified width
\bigskip
\begin{tabular*}{.9\textwidth}{
@{\extracolsep{\fill}}
SSS[table-format=6.2]S[table-format=4.4]
@{}}
\hline
1 & 1 & 1 & 100.0 \\
2 & 2 & 2 & 10.0 \\
3 & 3 & 3 & 1.0 \\
4 & 4 & 4 & 0.1 \\
5 & 5 & 5 & 0.01 \\
6 & 6 & 6 & 0.001 \\
\hline
\end{tabular*}
\end{table}
\end{document}