![使用“等效”选项在 S 列中得到不同的结果](https://linux22.com/image/433899/%E4%BD%BF%E7%94%A8%E2%80%9C%E7%AD%89%E6%95%88%E2%80%9D%E9%80%89%E9%A1%B9%E5%9C%A8%20S%20%E5%88%97%E4%B8%AD%E5%BE%97%E5%88%B0%E4%B8%8D%E5%90%8C%E7%9A%84%E7%BB%93%E6%9E%9C.png)
笔记
这个问题最初也包含在我的另一个问题。它是分开的,每个线程只有一个问题。
我想使用包siunitx
及其S
列在表格环境中显示数字。
实际手册在第 48 页上说,提供选项table-format=4.2
应该相当于更详细的版本table-figure-integer=4, table-figure-decimal=2
。但我发现,这并不正确。
这是具有两种风格的 MWE。它们应该产生看起来相同的表格,但事实显然并非如此。
第一个例子或多或少地符合预期,即,如果您仅定义 2 位小数并插入超过 2 位的数字,则会出错overfull hbox
。(这种过度填充是出于教育目的!我知道,您必须提供足够的空间,包括正号或负号等,以便 LaTeX 能够生成美观的表格。)
第二次迭代确实编译成功,但给出了“意外”的结果,因为无论您定义的位数是多少,列宽都会被正确计算。还请注意,最后一列没有自动舍入,尽管最后一列已声明了自动舍入。
\documentclass[11pt,final]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{siunitx}
\usepackage{xcolor}
\usepackage{booktabs}
%% For headings
\newcolumntype{H}{>{\scriptsize}c}
\begin{document}
%% This works. The overfull box in the second S column
%% is educationally intended!
\begin{tabular}{%
@{}
lr
S[table-format=4.5]
S[table-format=4.2]
S[table-format=4.2, table-auto-round=true]
@{}}
\toprule
\multicolumn{5}{@{}H@{}}{Columntype} \\
\multicolumn{2}{@{}H}{normal}
& \multicolumn{3}{H@{}}{siunitx} \\
\multicolumn{1}{H@{}}{l} & \multicolumn{1}{H}{r}
& \multicolumn{1}{H}{d\{4\}\{5\}} & \multicolumn{1}{H}{e\{2\}}
& \multicolumn{1}{H@{}}{f\{2\}} \\
\cmidrule(r){1-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(l){5-5}
1.2345 & 1.2345 & 1,2345 & 1,2345 & 1,2345 \\
123.45 & 123.45 & 123,45 & 123,45 & 123,45 \\
.12345 & .12345 & ,12345 & ,12345 & ,12345 \\
1234 & 1234 & 1234 & 1234 & 1234 \\
-123 & -123 & -123 & -123 & -123 \\
\bottomrule
\end{tabular}
%% This works kind of! The second S column is wider than above/wider than
%% defined in the declaration. The last column is not rounded to 2
%% decimal places, as was also declared!
\begin{tabular}{%
@{}
lr
S[table-figures-integer=4, table-figures-decimal=5]
S[table-figures-integer=4, table-figures-decimal=2]
S[table-figures-integer=4, table-figures-decimal=2, table-auto-round=true]
@{}}
\toprule
\multicolumn{5}{@{}H@{}}{Columntype} \\
\multicolumn{2}{@{}H}{normal}
& \multicolumn{3}{H@{}}{siunitx} \\
\multicolumn{1}{H@{}}{l} & \multicolumn{1}{H}{r}
& \multicolumn{1}{H}{d\{4\}\{5\}} & \multicolumn{1}{H}{e\{2\}}
& \multicolumn{1}{H@{}}{f\{2\}} \\
\cmidrule(r){1-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(l){5-5}
1.2345 & 1.2345 & 1,2345 & 1,2345 & 1,2345 \\
123.45 & 123.45 & 123,45 & 123,45 & 123,45 \\
.12345 & .12345 & ,12345 & ,12345 & ,12345 \\
1234 & 1234 & 1234 & 1234 & 1234 \\
-123 & -123 & -123 & -123 & -123 \\
\bottomrule
\end{tabular}
\end{document}
结果如下。
再次:哪里出错了。为什么table-format
成功了,哪里table-figures-*
失败了。为什么请求table-auto-round
没有明显的效果?
答案1
表格格式不仅可以设置数字,还可以table-alignment-mode=format
如果我添加这个,你的表格是相同的(这是用版本 3 编译的,并且我删除了 inputenc,因为它在当前的 latex 中是不需要的)
\documentclass[11pt,final]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{siunitx}
\usepackage{xcolor}
\usepackage{booktabs}
%% For headings
\newcolumntype{H}{>{\scriptsize}c}
\begin{document}
%% This works. The overfull box in the second S column
%% is educationally intended!
\begin{tabular}{%
@{}
lr
S[table-format=4.5]
S[table-format=4.2]
S[table-format=4.2, table-auto-round=true]
@{}}
\toprule
\multicolumn{5}{@{}H@{}}{Columntype} \\
\multicolumn{2}{@{}H}{normal}
& \multicolumn{3}{H@{}}{siunitx} \\
\multicolumn{1}{H@{}}{l} & \multicolumn{1}{H}{r}
& \multicolumn{1}{H}{d\{4\}\{5\}} & \multicolumn{1}{H}{e\{2\}}
& \multicolumn{1}{H@{}}{f\{2\}} \\
\cmidrule(r){1-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(l){5-5}
1.2345 & 1.2345 & 1,2345 & 1,2345 & 1,2345 \\
123.45 & 123.45 & 123,45 & 123,45 & 123,45 \\
.12345 & .12345 & ,12345 & ,12345 & ,12345 \\
1234 & 1234 & 1234 & 1234 & 1234 \\
-123 & -123 & -123 & -123 & -123 \\
\bottomrule
\end{tabular}
%% This works kind of! The second S column is wider than above/wider than
%% defined in the declaration. The last column is not rounded to 2
%% decimal places, as was also declared!
\begin{tabular}{%
@{}
lr
S[table-figures-integer=4, table-figures-decimal=5,table-alignment-mode=format]
S[table-figures-integer=4, table-figures-decimal=2,table-alignment-mode=format]
S[table-figures-integer=4, table-figures-decimal=2, table-auto-round=true,table-alignment-mode=format]
@{}}
\toprule
\multicolumn{5}{@{}H@{}}{Columntype} \\
\multicolumn{2}{@{}H}{normal}
& \multicolumn{3}{H@{}}{siunitx} \\
\multicolumn{1}{H@{}}{l} & \multicolumn{1}{H}{r}
& \multicolumn{1}{H}{d\{4\}\{5\}} & \multicolumn{1}{H}{e\{2\}}
& \multicolumn{1}{H@{}}{f\{2\}} \\
\cmidrule(r){1-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(l){5-5}
1.2345 & 1.2345 & 1,2345 & 1,2345 & 1,2345 \\
123.45 & 123.45 & 123,45 & 123,45 & 123,45 \\
.12345 & .12345 & ,12345 & ,12345 & ,12345 \\
1234 & 1234 & 1234 & 1234 & 1234 \\
-123 & -123 & -123 & -123 & -123 \\
\bottomrule
\end{tabular}
\end{document}