[Tex/LaTex] Align decimal marker with thousands separator using siunitx

horizontal alignmentsiunitxtables

My table consists of two groups of rows. The first group contains large values (X,XXX). The second groups contains smaller values (0.XXX). Siunitx aligns all numbers based on the position of the decimal marker. The result isn't beautiful: some numbers are aligned very much the left, while others are very much to the right.

Is it possible to align the numbers so that the decimal markers (from the small values) are aligned with the thousands separators (from the large values)?

Here's an example of the table. The second and third column show the current situation. The last two columns are an example of how I'd like the data to appear (edited using Photoshop).

Example

If a neat solution doesn't exist, a hacky solution would be welcome as well.

I already thought of a way to change the decimal marker within the table: if I could have a comma as decimal marker for the first group of rows, everything would be fine. However, I couldn't get that to work (with my little knowledge of LaTeX).

Code used for the table:

\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}

Best Answer

While I would not recommend this display form, it is certainly doable. Rather than use siunitx I'd probably go for a 'low level' solution based to some extent on how dcolumn does alignment. The strategy here is a hybrid approach. First, the cell content is grabbed using collcell (which does much the same as siunitx does at the first stage). The collected material is assumed to be either a decimal or integer number, and a quick check for . is therefore made. For decimals, one digit of padding is allowed for at the front and the number is typeset 'as is'. For integers, there is a hard-coded check for adding a , and the number is dumped into the output. Doing everything in a fixed-width box allows for alignment:

\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}
Related Question