# Align decimal marker with thousands separator using siunitx

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).

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

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

\bottomrule

\end{tabular}
\end{document}

• You could just a r column and insert the , yourself. But let's see if siunitx offers a nice-way here. Though, you do realize that this is the correct way, aligning the numbers in your way may confuse reader …? I would at least use a thin space for the group-separator. – Qrrbrbirlbel Apr 14 '13 at 17:01
• @Qrrbrbirlbel When right-aligning the full column, the numbers would be way more to the right (instead of centered, as in the current situation). On the other hand you're right that it might be a bit confusing… – Jonathan Apr 14 '13 at 17:07
• Just a friendly adivse: according to SI, commas should be used as decimal separators, whereas spaces should be used to separate multiples of 3 in numbers. e.g: 1,94; 5 643; 3,456; 532 323,52356 – Mario S. E. Apr 14 '13 at 20:31
• @MarioS.E. Thanks, didn't knew that! I was only aware of the Dutch (European?) style (1.234,56) and the English style (1,234.56). Using SI seems like the best option indeed. – Jonathan Apr 14 '13 at 23:11
• @Jonathan, you could take a look at what the SI says about this: bipm.org/utils/common/pdf/si_brochure_8_en.pdf – Mario S. E. Apr 14 '13 at 23:18

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


• Note I've hard-coded the format here as either <1>.<3> or <1-5> digits. More flexibility can be added but as I'd not recommend the format I'll add it only if required. – Joseph Wright Sep 8 '14 at 18:42