[Tex/LaTex] Align position of decimal point within table of numbers, text and percentage values

charactersdcolumnhorizontal alignmentspacingtables

I have asked sthg similar with a MWE today answered by David C., but this is a more difficult follow-up question. Also I have just made a new account 🙂

The Question is simple: I want the numbers to be aligned to the decimal point, AND if there is no decimal point in a column, align them in another good way.

In the MWE below, you will see the problem. The value 0 is directly over 0%; but the problem is that the '%' of '0%' is right below '0', so '0%' is actually more left than it should be. You would expect the 0 to be right below the other 0 and the % one position further on the right.

So I would like to align all values to the decimal point, but if there is none in a column, like eg in column 3 of the following MWE, there has to be another alignment. I hope I have described the problem well enough. If not, just look at the output of the MWE and check where the %-units are positioned. It's fine in the first column, but wrong in the 2nd one.

Also let me know if the width of the columns can be adjusted.

Here is the MWE. (Note that the mix of %- and decimal notation is intended. In some rows of my final table I will use %, whereas in others I will not)

\documentclass[a4paper, 12pt]{scrreprt}
\usepackage{setspace}
\usepackage{booktabs}
\usepackage{floatrow}
\floatsetup[table]{capposition=top}
\usepackage{dcolumn}
\newcolumntype{Y}{D..{6.4}}

\begin{document}

\begin{table}[htpb]
\begin{tabular}{lYYYY}
\toprule
\multicolumn{1}{l}{Test 1} & \multicolumn{2}{c}{Test 2} & \multicolumn{2}{c}{Test 3} \\
Observations successful &
\multicolumn{1}{c}{no} & 
\multicolumn{1}{c}{yes}  &
\multicolumn{1}{c}{maybe} &
\multicolumn{1}{c}{no}  \\
adsadad & 546546.546 & 4646 & 45.646 & 456.456 \\
AAA & 235.25\% & 22\% & 232.34\% & 234.25\% \\
CCC3 & 0.0033 & 0 & 0.0031 & 23 \\
CCC4 & 0.0033 & 0\% & 0.0031 & 0 \\
\bottomrule

\end{tabular}
\end{table}

\end{document}

Best Answer

It's probably better to use siunitx; the \sisetup settings will be local to this table, since the command appears in the table environment.

\documentclass[a4paper,12pt]{scrreprt}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}

\begin{table}[htpb]

\sisetup{
  table-space-text-post=\%,    % leave space for a ‘%’
  table-align-text-post=false, % push ‘%’ next to the number
}

\centering

\begin{tabular}{
 l
 S[table-format=6.4] % six integer digits, four decimal ones
 S[table-format=4.0]
 S[table-format=3.4]
 S[table-format=3.3]
}
\toprule
\multicolumn{1}{l}{Test 1} & \multicolumn{2}{c}{Test 2} & \multicolumn{2}{c}{Test 3} \\
\cmidrule{1-1}\cmidrule(lr){2-3}\cmidrule(l){4-5}
Observations successful &
\multicolumn{1}{c}{no} & 
\multicolumn{1}{c}{yes}  &
\multicolumn{1}{c}{maybe} &
\multicolumn{1}{c}{no}  \\
\midrule
adsadad & 546546.546 & 4646 & 45.646 & 456.456 \\
\% AAA & 235.25\% & 22\% & 232.34\% & 234.25\% \\
\% CCC3 & 0.0033 & 0 & 0.0031 & 23 \\
\% CCC4 & 0.0033 & 0\% & 0.0031 & 0 \\
\bottomrule

\end{tabular}
\end{table}

\end{document}

enter image description here

Related Question