Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
Take a look at the table-space-text-* options. Can you provide a sample table? For \sim for a ~, please see How does one insert a backslash or a tilde (~) into LaTeX? On that note, \sim is also allowed as input-comparator so it shouldn’t be a problem if you properly declare the comparator usage for that column. –  Qrrbrbirlbel Nov 18 '13 at 16:32
add comment

1 Answer

up vote 4 down vote accepted

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

share|improve this answer
    
If I would use some text like xx before the number, then it would have to be done with table-space-text-pre? –  Tanja Nov 18 '13 at 17:54
    
@user2758804 Correct. For more information, check out the manual, and for examples, see Can the siunitx table alignment feature, S, align ratios? –  Qrrbrbirlbel Nov 18 '13 at 18:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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