![使用 siunitx 对不同单元进行垂直对齐](https://linux22.com/image/448016/%E4%BD%BF%E7%94%A8%20siunitx%20%E5%AF%B9%E4%B8%8D%E5%90%8C%E5%8D%95%E5%85%83%E8%BF%9B%E8%A1%8C%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90.png)
我试图对齐逗号,但做不到。我已经在论坛上寻找解决方案,但就是找不到可行的解决方案。一定是我忽略了什么。最后一列标题也存在问题,它没有完全显示出来,我不知道如何修复它。
\documentclass[class=scrreprt, fontsize=10pt]{standalone}
\usepackage{tikz}
\usepackage[T1]{fontenc}
\usepackage[lf, p, sflf, sfp]{libertine}
\usepackage[libertine]{newtxmath}
\usepackage[stretch=10, shrink=10, final]{microtype}
\usepackage{booktabs}
\usepackage{nicematrix}
\usepackage{enumitem}
\usepackage[detect-all, locale=DE, detect-weight]{siunitx}
\usepackage{bm}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\SetMathAlphabet{\mathsf}{bold}{OT1}{\sfdefault}{b}{n}
\begin{document}
\begin{NiceTabularX}{15cm}{>{}C{0.02\textwidth}L{0.32\textwidth}*{4}{Y}}
\CodeBefore
\rowcolor{yellow}{1}
\Body
\toprule
\RowStyle{\footnotesize\bfseries}
\# & {test} & Häufigkeit & \bm{$\mathsf{\overline{R1}}$} & R2& Problemwertindex \\
\midrule
A & a & 6 & 5 & 30 & \SI{45.4}{\percent} \\
B & b & 8 & 2 & 16 & \SI{24.2}{\percent} \\
% C & c & $-$ & $-$ & $-$ & $-$ \\
D & d & 2 & 5 & 10 & \SI{15.2}{\percent} \\
E & e & 2 & 3 & 6 & \SI{9.1}{\percent} \\
% F & f & $-$ & $-$ & $-$ & $-$ \\
G & g & 1 & 4 & 4 & \SI{ 6.1}{\percent} \\
\midrule
& Total & 19 & 3,8 & 66 & \SI{100.0}{\percent} \\
\bottomrule
\end{NiceTabularX}
\end{document}
答案1
为了对齐最后一列的数字,您应该使用包S
中定义的列类型:siunitx
\documentclass[class=scrreprt, fontsize=10pt]{standalone}
\usepackage{tikz}
\usepackage[T1]{fontenc}
\usepackage[lf, p, sflf, sfp]{libertine}
\usepackage[libertine]{newtxmath}
\usepackage[stretch=10, shrink=10, final]{microtype}
\usepackage{booktabs}
\usepackage{nicematrix}
\usepackage{enumitem}
\usepackage[detect-all, locale=DE, detect-weight]{siunitx}
\usepackage{bm}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\SetMathAlphabet{\mathsf}{bold}{OT1}{\sfdefault}{b}{n}
\begin{document}
\begin{NiceTabularX}{15cm}{>{}C{0.02\textwidth}L{0.32\textwidth}*{3}{Y} S[table-format=3.1{\%}] <{\%}}
\CodeBefore
\rowcolor{yellow}{1}
\Body
\toprule
\RowStyle{\footnotesize\bfseries}
\# & {test} & Häufigkeit & \bm{$\mathsf{\overline{R1}}$} & R2& \multicolumn{1}{c}{Problemwertindex} \\
\midrule
A & a & 6 & 5 & 30 & 45.4 \\
B & b & 8 & 2 & 16 & 24.2 \\
% C & c & $-$ & $-$ & $-$ & $-$ \\
D & d & 2 & 5 & 10 & 15.2 \\
E & e & 2 & 3 & 6 & 9.1 \\
% F & f & $-$ & $-$ & $-$ & $-$ \\
G & g & 1 & 4 & 4 & 6.1 \\
\midrule
& Total & 19 & 3,8 & 66 & 100.0 \\
\bottomrule
\end{NiceTabularX}
\end{document}
经过三次编译结果是:
附录:
对于练习,使用与包相同的表tabularray
。MWE 只需要一次编译:
\documentclass[class=scrreprt, fontsize=10pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage[lf, p, sflf, sfp]{libertine}
\usepackage[libertine]{newtxmath}
\usepackage{bm}
\SetMathAlphabet{\mathsf}{bold}{OT1}{\sfdefault}{b}{n}
\usepackage[stretch=10, shrink=10, final]{microtype}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}
\sisetup{detect-all, locale=DE, detect-weight}
\begin{document}
\begin{tblr}{width=15cm,
colspec = {Q[c,0.02\linewidth]
Q[l,0.32\linewidth]
*{3}{X[c]}
S[table-format=3.1{\%}]
},
row{1} = {font=\bfseries, bg=yellow},
}
\toprule
\# & test & Häufigkeit & $\bm{\overline{R1}}$ & R2 & {{{Problemwertindex}}} \\
\midrule
A & a & 6 & 5 & 30 & 45.4\,\% \\
B & b & 8 & 2 & 16 & 24.2\,\% \\
% C & c & -- & -- & -- & -- \\
D & d & 2 & 5 & 10 & 15.2\,\% \\
E & e & 2 & 3 & 6 & 9.1\,\% \\
% F & f & -- & -- & -- & -- \\
G & g & 1 & 4 & 4 & 6.1\,\% \\
\midrule
& Total & 19 & 3,8 & 66 & 100.0\,\% \\
\bottomrule
\end{tblr}
\end{document}