# Align position of decimal point within table of numbers, text and percentage values

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

• Have you tried using the siunitx package? it comes with a special S column type to handle this – Mario S. E. Oct 23 '13 at 22:30
• @MarioS.E. yes I have tried but without success. The % were the problem. – TomX Oct 23 '13 at 22:32
• you (or I) could have used \rlap{\%} :-) – David Carlisle Oct 23 '13 at 23:25
• @DavidCarlisle Used how? Could you elaborate plz. – TomX Oct 23 '13 at 23:32
• done, Hopefully this will be enough to grab the tick from that @egreg bloke:-) – David Carlisle Oct 23 '13 at 23:44

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


• wow that was fast. Excellent! – TomX Oct 23 '13 at 22:50
• If I try to caption this with ttabbox from the floatrowpackage in order to create a caption with the same width as the table, it doesnt work anymore - there is a newline after the first letter which should not be there. The code is simply: \begin{table} \ttabbox {\caption{A small table ...}\label{...}} {\begin{tabular}...\end{tabular}} \end{table} Do you know the reason? I got the feeling that ttabbox doesn't work under the changes you have made in the setup of situnitx. – TomX Oct 23 '13 at 23:20
• @TomX Sorry, but I'm not a floatrow user. And I don't think that the caption should be the same width as the table. – egreg Oct 23 '13 at 23:21

\documentclass[a4paper, 12pt]{scrreprt}
\usepackage{setspace}
\usepackage{booktabs}
\usepackage{floatrow}
\floatsetup[table]{capposition=top}
\usepackage{dcolumn}
\newcolumntype{Y}{D..{6.4}}
\newcommand\pct{\rlap{$\%$}}
\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}  \\