使用 siunitx 將小數標記與千位分隔符號對齊

使用 siunitx 將小數標記與千位分隔符號對齊

我的表格由兩組行組成。第一組包含較大的值 (X,XXX)。第二組包含較小的值 (0.XXX)。 Siunitx 根據小數點標記的位置對齊所有數字。結果並不漂亮:有些數字非常靠左對齊,而有些則非常靠右。

是否可以對齊數字,使小數標記(從小值開始)與千位分隔符號(從大值開始)對齊?

這是該表的範例。第二欄和第三欄顯示目前情況。最後兩列是我希望資料顯示方式的範例(使用 Photoshop 編輯)。

例子

如果不存在簡潔的解決方案,那麼駭客解決方案也將受到歡迎。

我已經想到了一種方法來更改表中的小數標記:如果我可以將逗號作為第一組行的小數標記,那麼一切都會好起來的。然而,我無法讓它工作(以我對 LaTeX 的了解很少)。

表中使用的代碼:

\documentclass{article}

\usepackage{booktabs}
\usepackage{siunitx}

\sisetup{group-separator={,},group-minimum-digits={3},output-decimal-marker={.}}

\begin{document}
\begin{tabular}{@{}p{2.3cm}SSp{0.3cm}SS@{}}

\toprule
& \multicolumn{2}{c}{Without Threshold Selector} && \multicolumn{2}{c}{With Threshold Selector}\\
\cmidrule{2-3} \cmidrule{5-6}
& \multicolumn{1}{p{2.3cm}}{\centering{Naive Bayes}} & \multicolumn{1}{p{2.3cm}}{\centering{Logistic}} && \multicolumn{1}{p{2.3cm}}{\centering{Naive Bayes}} & \multicolumn{1}{p{2.3cm}}{\centering{Logistic}}\\
\midrule

True positives  & 2791  & 1831  && 3126     & 3547\\
False positives & 2924  & 995   && 3853     & 3483\\
True negatives  & 36998 & 38927 && 36069    & 36439\\
False negatives & 2498  & 3458  && 2163     & 1742\\

\addlinespace

Sensitivity     & 0.528 & 0.346 && 0.591    & 0.671\\
Precision       & 0.488 & 0.648 && 0.448    & 0.505\\

\bottomrule

\end{tabular}
\end{document}

答案1

雖然我不推薦這種顯示形式,但它當然是可行的。我可能不會使用siunitx基於dcolumn對齊方式的“低級”解決方案。這裡的策略是一種混合方法。首先,使用抓取單元格內容(其作用與第一階段的作用collcell大致相同)。siunitx假設收集的材料是小數或整數,.因此進行快速檢查。對於小數,允許在前面填充一位數字,並且數字按“原樣”排版。對於整數,有一個硬編碼檢查來添加 a,並將數字轉儲到輸出中。在固定寬度的方塊中執行所有操作都可以進行對齊:

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{collcell}
\newlength\mylength
\AtBeginDocument{\settowidth\mylength{$12.345$}}
\newcolumntype{M}{>{\collectcell\multialign}l<{\endcollectcell}}
\makeatletter
\newcommand*\multialign[1]{%
  \setbox0=\hbox to \mylength{%
    \hfil
    $
      \in@{.}{#1}%
      \ifin@
        \settowidth\mylength{$0$}%
        \hspace{\mylength}%
        \expandafter\@firstofone
      \else
        \expandafter\multialignint
      \fi
        {#1}%
    $%
  }%
  \hfil\box0\hfil
}
\newcommand*\multialignint[1]{%
  \multialignintauxi#1\empty\empty\empty\empty\relax\stop
}
\newcommand*\multialignintauxi{}
\def\multialignintauxi#1#2#3#4#5#6\stop{%
 \ifx#4\empty % Three or fewer digits
   #1#2#3%
 \else
  \ifx#5\empty % Four digits
    #1\mathord,#2#3#4%
  \else % Five digits
    #1#2\mathord,#3#4#5%
  \fi
 \fi
}
\makeatother
\begin{document}
\begin{tabular}{@{}p{2.3cm}MMp{0.3cm}MM@{}}

\toprule
& \multicolumn{2}{c}{Without Threshold Selector} && \multicolumn{2}{c}{With Threshold Selector}\\
\cmidrule{2-3} \cmidrule{5-6}
& \multicolumn{1}{p{2.3cm}}{\centering{Naive Bayes}} & \multicolumn{1}{p{2.3cm}}{\centering{Logistic}} && \multicolumn{1}{p{2.3cm}}{\centering{Naive Bayes}} & \multicolumn{1}{p{2.3cm}}{\centering{Logistic}}\\
\midrule

True positives  & 2791  & 1831  && 3126     & 3547\\
False positives & 2924  & 995   && 3853     & 3483\\
True negatives  & 36998 & 38927 && 36069    & 36439\\
False negatives & 2498  & 3458  && 2163     & 1742\\

\addlinespace

Sensitivity     & 0.528 & 0.346 && 0.591    & 0.671\\
Precision       & 0.488 & 0.648 && 0.448    & 0.505\\

\bottomrule
\end{tabular}
\end{document}

相關內容