TransWikia.com

Placing a symbol before a table entry without upsetting alignment by the siunitx package

TeX - LaTeX Asked on March 8, 2021

In the following MWE, I use the siunitx package to align numerical entries in a table. How can I place a sim symbol before the "6" without upsetting the alignment? Table A will compile; Table B will not.

documentclass[oneside,11pt]{book}

usepackage[semibold,tt=false]{libertine}
usepackage{libertinust1math}
usepackage[
  expansion = false ,
  tracking = smallcaps ,
  letterspace = 40 ,
  final
]{microtype}
usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
usepackage{mathtools}
usepackage{booktabs}
usepackage{siunitx}

begin{document}

% A
begin{table}[!h]
  centering
  begin{tabular}{
l
S[table-format=1.1,table-number-alignment=left,table-align-text-post=false]
}
    toprule
    & {X}
    midrule
    A & 6
    B & 0.1
    bottomrule
  end{tabular}
  caption{A}
end{table}

% B
begin{table}[!h]
  centering
  begin{tabular}{
l
S[table-format=1.1,table-number-alignment=left,table-align-text-post=false]
}
    toprule
    & {X}
    midrule
    A & sim 6
    B & 0.1
    bottomrule
  end{tabular}
  caption{B}
end{table}

end{document}

mwe

3 Answers

Try the following:

documentclass[12pt]{article}

usepackage[semibold,tt=false]{libertine}
usepackage{libertinust1math} 
usepackage[expansion = false,
            tracking = smallcaps,
            letterspace = 40,
            final]  {microtype}

usepackage{booktabs}
usepackage{siunitx}

begin{document}
    begin{center}
  begin{tabular}{c
                  S[input-comparators,              % <----
                    table-space-text-pre=$sim$,    % <----
                    table-format=1.1,
                    ]          
                 }
    toprule
type    &   {X}         
    midrule
A       & $sim$ 6      
B       &        0.1    
    bottomrule
    end{tabular}
caption{A}
    end{table}
    
medskip
    
medskip
however, in table is better to use 

verb+table-comparator+ instead of verb+input-comparators+ as suggested in @Bernard's answer:

medskip
  begin{tabular}{c
                  S[table-format=1.1,
                    table-space-text-pre=$sim$,
                    table-comparator]   % <----
                 }
    toprule
type    &   {X}     
    midrule
A       & sim 6                      % <----
B       & 0.1       
    bottomrule
    end{tabular}
    end{center}
end{document}

In both examples result is the same:

enter image description here

Correct answer by Zarko on March 8, 2021

Put the SI{sim 6}{} instead of sim and it is works in pdfLaTeX. Happy new year :-). See the explanation here: How to correctly use siunitx to align number in the center of a table for number with preamble

documentclass[oneside,11pt]{book}
usepackage[semibold,tt=false]{libertine}
usepackage{libertinust1math}
usepackage[
  expansion = false ,
  tracking = smallcaps ,
  letterspace = 40 ,
  final
]{microtype}
usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
usepackage{mathtools}
usepackage{booktabs}
usepackage{siunitx}
begin{document}

% A
begin{table}[!h]
  centering
  begin{tabular}{
l
S[table-format=1.1,table-number-alignment=left,table-align-text-post=false]
}
    toprule
    & {X}
    midrule
    A & 6
    B & 0.1
    bottomrule
  end{tabular}
  caption{A}
end{table}

% B
begin{table}[!h]
  centering
  begin{tabular}{
l
S[table-format=1.1,table-number-alignment=left,table-align-text-post=false]
}
    toprule
    & {X}
    midrule
    A & SI{sim 6}{}

    B & 0.1
    bottomrule
  end{tabular}
  caption{B}
end{table}

end{document}

enter image description here

Edit by comment:

documentclass[oneside,11pt]{book}
usepackage[semibold,tt=false]{libertine}
usepackage{libertinust1math}
usepackage[
  expansion = false ,
  tracking = smallcaps ,
  letterspace = 40 ,
  final
]{microtype}
usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
usepackage{mathtools}
usepackage{booktabs}
usepackage{siunitx}
begin{document}

% A
begin{table}[!h]
  centering
  begin{tabular}{
l
S[table-format=1.1,table-number-alignment=left,table-align-text-post=false]
}
    toprule
    & {X}
    midrule
    A & 6
    B & 0.1
    bottomrule
  end{tabular}
  caption{A}
end{table}

% B
begin{table}[!h]
  centering
  begin{tabular}{
l
S[table-format=1.1,table-number-alignment=left,table-align-text-post=false]
}
    toprule
    & {X}
    midrule
    A & !!!SI{sim 6}{}

    B & 0.1
    bottomrule
  end{tabular}
  caption{B}
end{table}

end{document}

enter image description here

Answered by Sebastiano on March 8, 2021

To add symbols, you have the key input-symbols, but this one is a comparator, so you just have to set the table-comparator switch to true:

documentclass[oneside,11pt]{book}

usepackage[semibold,tt=false]{libertine}
usepackage{libertinust1math}
usepackage[
  expansion = false ,
  tracking = smallcaps ,
  letterspace = 40 ,
  final
]{microtype}
usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
usepackage{mathtools}
usepackage{booktabs}
usepackage{siunitx}

begin{document}

begin{table}[!h]
  centering
  begin{tabular}{
l
S[table-format=1.1,table-number-alignment=left,table-align-text-post=false, table-comparator]
}
    toprule
    & {X}
    midrule
    A & sim 6
    B & 0.1
    bottomrule
  end{tabular}
  caption{B}
end{table}

end{document} 

enter image description here

Answered by Bernard on March 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP