# Aligning digits in table with percentage sign on first row

I'd like to align the digits taking into account the percent symbols on the first row.

MWE:

\documentclass{article}
\usepackage{booktabs}
\begin{document}

\begin{table}
\centering
\begin{tabular}{@{}rrrr@{}}\toprule
Col1 & Col2 & Col3 & Col4\\ \midrule
10\% & 20\% & 30\% & 40\%\\
5    &  6   &  7   &  8  \\
115    &116   &117   &118  \\
\bottomrule
\end{tabular}
\end{table}

\end{document}


I get the following output:

However, I'd like to align the digits as shown in this image, taking into account the percent symbol on the first row and indenting the following numbers accordingly.

Your readers will not be sure that every entry refers to a percentage: state it in the header.

\documentclass{article}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{tabular}{
@{}
l
*{2}{S[table-format=3.0]}
@{}
}
\toprule
Debt/Assets & {2003} & {2004} \\
& {(\%)} & {(\%)}   \\
\midrule
\SI{<50}{\percent}           & {--} &  10 \\
\SIrange{50}{59.9}{\percent} &  20  &   5 \\
\SIrange{60}{69.9}{\percent} & {--} &  10 \\
\SIrange{70}{79.9}{\percent} & {--} &  10 \\
\SIrange{80}{89.9}{\percent} &  40  &  10 \\
\SI{\ge90}{\percent}         &  40  &  55 \\
\cmidrule(lr){2-2} \cmidrule(l){3-3}
Total                        & 100  & 100 \\
Mean                         &  85  &  80 \\
Median                       &  85  &  94 \\
\midrule
Number of projects           & {5}  & {20} \\
\bottomrule
\end{tabular}

\end{document}


The final \midrule helps in making clear that the row is about different data.

If you prefer the bad style in the picture…

\documentclass{article}
\usepackage{siunitx,booktabs}

\newcommand{\dashpercent}{%
\makebox[1.5em][r]{--\makebox[0pt][l]{\,\%}}%
}
\newcommand{\dash}{\makebox[1.5em][r]{--}}%
\newcommand{\ppercent}{\makebox[0pt][l]{\,\%}}

\begin{document}

\begin{tabular}{
@{}
l
*{2}{S[table-format=3.0]@{\hspace{1.2em}}}
@{}
}
\toprule
Debt/Assets & {2003} & {2004} \\
\midrule
\SI{<50}{\percent}           & {\dashpercent} &  10\ppercent \\
\SIrange{50}{59.9}{\percent} &  20  &   5 \\
\SIrange{60}{69.9}{\percent} & {\dash} &  10 \\
\SIrange{70}{79.9}{\percent} & {\dash} &  10 \\
\SIrange{80}{89.9}{\percent} &  40  &  10 \\
\SI{\ge90}{\percent}         &  40  &  55 \\
\cmidrule(lr){2-2} \cmidrule(lr){3-3}
Total                        & 100\ppercent  & 100\ppercent \\
Mean                         &  85\ppercent  &  80\ppercent \\
Median                       &  85\ppercent  &  94\ppercent \\
\midrule
Number of projects           & {5}  & {20} \\
\bottomrule
\end{tabular}

\end{document}


• I agree this is better. But it doesn't really answer the question :-) – Sandy G Mar 19 at 18:32
• @SandyG Now you can use bad style, if you so wish. ;-) – egreg Mar 19 at 21:23
• Ha! I like my bad style better than your bad style! – Sandy G Mar 19 at 21:34

You could define a \phantom % symbol:

\newcommand{\pct}{\phantom{\%}}


Then

\begin{tabular}{@{}rrrr@{}}\toprule
Col1 & Col2 & Col3 & Col4\\ \midrule
10\% & 20\% & 30\% & 40\%\\
5\pct    &  6\pct   &  7\pct   &  8\pct  \\
115\pct   &116\pct   &117\pct   &118\pct  \\
\bottomrule
\end{tabular}


produces the desired output, without the % sign going past the edge of the column.

Temporarily redefine \% as an \rlap. Also, extend the table on the right by the width of a \%. The advantage is the content of the tabular definition does not change.

\documentclass{article}
\usepackage{booktabs,calc}
\begin{document}

\begin{table}
\centering
\let\svpc\%
\setbox0=\hbox{\%}
\edef\tmp{\the\wd0}
\renewcommand\%{\rlap{\svpc}}
\begin{tabular}{@{}rrrr@{\hspace{\tmp}}}\toprule
Col1 & Col2 & Col3 & Col4\\ \midrule
10\% & 20\% & 30\% & 40\%\\
5    &  6   &  7   &  8  \\
115    &116   &117   &118  \\
\bottomrule
\end{tabular}
\end{table}

\end{document}