2

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}
  • 1
    Hope dcolumn.sty may suits with your requirement.... – MadyYuvi Oct 23 '18 at 6:58
  • 2
    I think you just need the S column type (instead of c) along with the setup in the preamble: \sisetup{ input-ignore={,}, input-decimal-markers={.}, group-separator={,}, group-minimum-digits=4, }. – Peter Grill Oct 23 '18 at 7:10
  • 1
    In the final column, are you using both , (comma) and . (dot) as the input decimal marker symbol? Please clarify. – Mico Oct 23 '18 at 7:37
  • 1
    In the final column, are you using both , (comma) and . (dot) as the input decimal marker symbol? And, in the second data column, is , acting as the input decimal marker, so that 19,000 is equal to 19? Please clarify. – Mico Oct 23 '18 at 7:45
  • For your fourth column: As far as I know it is considered best practice to include the unit in the header once and not in every other row of that column. – Skillmon Oct 23 '18 at 8:17
4

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}
  • thanks that looks exactly as intended, besides the last column. Imagine there might be a use case when switching between dot and comma is useful, e.g. if the first rows contains only numbers with decimals, but the lower ones contain only numbers with commas (because no decimals are needed). Maybe those table sections are even separated with a line in between.This MWE was maybe too short to illustrate when it could make sense. If it's possible, would be nice if you could edit that solution. – ghx Oct 24 '18 at 6:02
  • 1
    @ghx - If the commas in the final column are not decimal markers but, rather, thousands-separators, you could use the same setup for that column as you do in the first column. – Mico Oct 24 '18 at 6:41
  • sorry I realized just now one column is not correct. The "," is usually used as a thousands separator. However, in column 2, it currently looks as if 900 is larger than e.g. 7,000 or 19,000 - one get the impression it is 900,000 (without the last 3 zeros). Using r would be the correct way to display, but it seems to mess up the distances between columns if you don't use S anymore. What would be the right way with S? – ghx Oct 25 '18 at 21:13
  • @ghx - So, for column 2, should input-decimal-markers={,} be changed to input-ignore={,}? (When I wrote my answer, I was under the impression that in column 2, commas were being used as decimal markers, not thousands-separators. As I wrote at the start of my answer, "Your objectives aren't entirely unambiguous"...) With the modified setup, along with the option table-format=5.0, column 2 would basically be the same as column 4. – Mico Oct 25 '18 at 21:43
  • ah yeah thanks, I tried that but got an error. I realized the error must come from my column label, which goes over multiple rows. Sthg like: \shortstack{To \\ comma}. Can this be changed? – ghx Oct 25 '18 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.