[Tex/LaTex] Align large numbers and small ones on comma and decimal point

siunitx

I am trying to align values in multiple columns of a table.

There is a column with very large numbers and decimals, there is one with commas and no comma at all by intention, there is another one with a unit involved (%), if it plays a role, and one with very large numbers and no decimals. Finally, there is also a mixed one, where the dot should be aligned to the comma (optional, I believe this is hard!).

Can this be done with siunitx? I played around with the options, but mostly I get errors which I don't know how to debug. Any hint appreciated. I am fine with a solution for column 2 to column 5 really; column 6 (mixed) is really optional. I just added it because I am curious if it is possible.

edit based on the comments: If only part of my request is possible in siunitx, that would be very helpful as well. Thanks!

\documentclass{article}

\usepackage{siunitx}

\begin{document}
\begin{tabular}{lccccc}

row & alignToDec & alignToComma & alignWithUnit & AlignLargeNumbers & alignMixedOptional \\
row1: & 19,000,000.0 & 19,000 &  12.31\%  & 12,222,222 & 12.31 \\
row2: & 7,000,434.12 & 7,000 & 11.31\% & 13,222,222 & 12,31 \\
row3: & 900,342.12   & 900 & 10.98\% & 142,222,222 & 12.31 \\
row4: & 90.0         & 90 & 12.21\% & 1,531,222,222 & 12,31 \\
\end{tabular}
\end{document}

\end{document}

Best Answer

Your objectives aren't entirely unambiguous. I've interpreted them as follows:

  • Col. 1: Only "." should be treated as the input decimal marker, while commas should be used as the thousands-separator in the output.

  • Col. 2: The "," symbol should be treated as both the input decimal marker and the output decimal marker. ("." is the default output decimal marker for the siunitx package.)

  • Col. 3: Treat "." as the input decimal marker, and leave enough space to also typeset the "units" -- here, the % symbol.

  • Col. 4: Large numbers without explicit decimal marker; treat commas as thousands separators both on input and output.

  • Col. 5: Treat both "." and "," as input decimal markers; in the output, use "." as the decimal marker. Aside: I think your readers are going to be very confused if both "." and "," are used as the output decimal markers -- especially if "," is used as the thousands separator as well.

As the following code shows, these objectives may be achieved by (a) modifying some default settings globally via an \sisetup instruction and (b) providing additional options as arguments of each of the 5 S columns. Observe that each S column is given its own table-format=... option to specify the overall width of the numeric material.

enter image description here

\documentclass{article}
\usepackage{booktabs} % for "\midrule" macro
\usepackage{siunitx}
\sisetup{group-separator       = {,}, % default: \, ("thinspace")
         group-minimum-digits  = 4,   % default: 5
         input-decimal-markers = {.}} % default: "{.,}"

\begin{document}
\centering % optional
\begin{tabular}{@{}
       S[table-format=8.2,
         input-ignore={,}]
       S[table-format=3.3,
         input-decimal-markers={,},
         output-decimal-marker={,}]    % default: . ("dot")
       S[table-format=3.2,
         table-space-text-post={\%}]   % set aside space for "%" symbol
       S[table-format=10.0,
         input-ignore={,}]
       S[table-format=2.2,
         input-decimal-markers={.,}] % treat both "." and "," as decimal markers
       @{}}
{To Dec} & {To Comma} & {With Unit} & {Large Numbers} & {Mixed Optional} \\
\midrule
19,000,000.0  & 19,000 &   2.31\% &    12,222,222 &  2.31 \\
 7,000,434.12 &  7,000 &  11.31\% &    13,222,222 & 12,31 \\
   900,342.12 &    900 &  10.98\% &   142,222,222 &  2.31 \\
        90.0  &     90 & 312.21\% & 1,531,222,222 & 12,31 \\
\end{tabular}
\end{document}
Related Question