TransWikia.com

Completely left align number entries in a table with siunitx

TeX - LaTeX Asked on December 26, 2020

I am using the siunitx package, along with the booktabs package, to make a table.

Here is my output:

output

Table (a) uses the tabular specification llll (i.e., no use of siunitx). Table (a) looks great, except that I want the decimal points in +0.2 and -0.4 to be lined up.

To do that, in Table (b), I used the tabular specification lSSS and used sisetup{table-number-alignment=left}. Now the decimal points are aligned, but I would like the numbers in the last two columns to be fully left aligned (flush with the k_{theta}‘s), since they are all positive. How can I do this?

Here is my code:

documentclass{article}
usepackage{siunitx,booktabs,subcaption}
DeclareSIUnit{calorie}{cal}

begin{document}

begin{table}

  begin{subtable}{textwidth}
    caption{}
    begin{tabular}{llll}
      toprule
      & {$q$ (si{elementarycharge})} & {$k_{theta}$ (si{kilocaloriepermole})} & {$k_{theta}$ (si{kilojoulepermole})}
      midrule
      compound A & +0.2 & 0.066 & 0.276144
      compound B & -0.4 & 0.14 & 0.585760
      bottomrule
    end{tabular}
  end{subtable}

  vspace*{16pt}

  begin{subtable}{textwidth}
    caption{}
    sisetup{table-number-alignment=left}
    begin{tabular}{lSSS}
      toprule
      & {$q$ (si{elementarycharge})} & {$k_{theta}$ (si{kilocaloriepermole})} & {$k_{theta}$ (si{kilojoulepermole})}
      midrule
      compound A & +0.2 & 0.066 & 0.276144
      compound B & -0.4 & 0.14 & 0.585760
      bottomrule
    end{tabular}
  end{subtable}

end{table}

end{document}

2 Answers

With the help of table-number-alignment in combination with the appropriate table-format, you can achieve the following output:

enter image description here

documentclass{article}
usepackage{siunitx,booktabs}
DeclareSIUnit{calorie}{cal}

begin{document}

begin{table}
    begin{tabular}{l
                    S[table-format=-1.1,table-number-alignment=left]
                    S[table-format=1.3,table-number-alignment=left]
                    S[table-format=1.6,table-number-alignment=left]}
      toprule
      & {$q$ (si{elementarycharge})} & {$k_{theta}$ (si{kilocaloriepermole})} & {$k_{theta}$ (si{kilojoulepermole})}
      midrule
      compound A & +0.2 & 0.066 & 0.276144
      compound B & -0.4 & 0.14 & 0.585760
      bottomrule
    end{tabular}

end{table}

end{document}

Correct answer by leandriis on December 26, 2020

You need to specify the format of numbers in each column; the simple S specification applies a default that's not suitable for your case.

On the other hand, the fact that the numbers are positive doesn't seem to have something to do with alignment.

I'd split the header into two rows, one for the symbols and one for the units.

documentclass{article}
usepackage{siunitx,booktabs}
DeclareSIUnit{calorie}{cal}

begin{document}

begin{table}
centering

begin{tabular}{
 @{}
 l
 S[table-format=-1.1,retain-explicit-plus,table-number-alignment=left]
 S[table-format=1.3,table-number-alignment=left]
 S[table-format=1.6,table-number-alignment=left]
 @{}
}
toprule
& {$q$ (si{elementarycharge})}
& {$k_{theta}$ (si{kilocaloriepermole})}
& {$k_{theta}$ (si{kilojoulepermole})} 
midrule
compound A & +0.2 & 0.066 & 0.276144
compound B & -0.4 & 0.14 & 0.585760
bottomrule
end{tabular}

bigskip

begin{tabular}{
 @{}
 l
 S[table-format=-1.1,retain-explicit-plus]
 S[table-format=1.3]
 S[table-format=1.6]
 @{}
}
toprule
& {$q$}
& {$k_{theta}$}
& {$k_{theta}$} 
& {(si{elementarycharge})}
& {(si{kilocaloriepermole})}
& {(si{kilojoulepermole})} 
midrule
compound A & +0.2 & 0.066 & 0.276144
compound B & -0.4 & 0.14 & 0.585760
bottomrule
end{tabular}

end{table}

end{document}

enter image description here

In the second sample, the big white space has disappeared.

Answered by egreg on December 26, 2020

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