2

I would like to have my numbers aligned left but centered in a table as in this solution, which I also tried to apply. Now my numbers are of the form ~24%. For a correct alignment I tried to protect the non-number parts by writing it as {$\sim$}24{ \%}. The % is well behind the number, however the ~ is centered in the column and overlapping the numbers. Same thing happens when using something like {xx}60{ \%} where now the xx and the numbers overlap. How to protect the ~ to stay left of the number?

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

\begin{document}

  \begin{tabular}{lS[table-format=2.0]S[table-format=2.0]}
     \toprule
      & {Before treatment} & {After treatment} \\
      \midrule
     component1 & {$\sim$}60{ \%} & {$\sim$}15{ \%} \\
     component2 & {$\sim$}14{ \%} & {$\sim$}4{ \%} \\
     component3 & {$\sim$}26{ \%} & {$\sim$}81{ \%} \\
     \bottomrule
   \end{tabular}


\end{document}
Tanja
  • 559

1 Answers1

4

As said in the comment, the table-space-text-post option helps with saving the right space after the number. To stay with siunitx default setting, the thin space \, is used instead of a full space.

The \sim macro is already one of the allowed input macros of siunitx and is used as a comparator. This allows you to use \sim without any protection in the column. The space for it is allocated with the table-comparator switch, or, for the special table-format option with an < (or > or …?) in front of the integer part.

Code

\documentclass{article}
\usepackage{booktabs,siunitx}
\begin{document}
\begin{tabular}{l *2{S[table-format=<2.0, table-space-text-post=\,\%]}}
    \toprule
               & {Before treatment} & {After treatment} \\ \midrule
    component1 & \sim 60 \,\%       & \sim 15 \,\%      \\
    component2 & \sim 14 \,\%       & \sim 4  \,\%      \\
    component3 & \sim 26 \,\%       & \sim 81 \,\%      \\ \bottomrule
\end{tabular}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821