TransWikia.com

How to make siunitx entries in a table boldface?

TeX - LaTeX Asked on July 5, 2021

I am using the siunitx package in a tabular environment. How can I make entries boldface? Here is a MWE:

documentclass[oneside,11pt]{book}

usepackage[
  expansion = false ,
  tracking = smallcaps ,
  letterspace = 40 ,
]{microtype}
usepackage{booktabs}
usepackage{siunitx}
sisetup{%
  detect-family, detect-shape,
  product-units = power,
  list-final-separator = {, and },
  retain-explicit-plus,
  input-comparators = {<=>approxgegeqggleleqllsimlesssimgtrsim}
}
DeclareSIUnit[number-unit-product = ]percent{char`%}

begin{document}

begin{table}[!h]
  centering
  begin{tabular}{lll}
    toprule
    A & {B} & {C}
    midrule
    textbf{Bob} & textbf{SI{75}{percent}} & textbf{-11.11}
    Carla & SI{75}{percent} & 2.22
    Dale & SI{75}{percent} & -3.33
    Ena & SI{75}{percent} & 4.44
    bottomrule
  end{tabular}
  caption{Caption 1}
end{table}

begin{table}[!h]
  centering
  begin{tabular}{lSS}
    toprule
    A & {B} & {C}
    midrule
    textbf{Bob} & textbf{SI{75}{percent}} & textbf{-11.11}
    Carla & SI{75}{percent} & 2.22
    Dale & SI{75}{percent} & -3.33
    Ena & SI{75}{percent} & 4.44
    bottomrule
  end{tabular}
  caption{Caption 2}
end{table}

begin{table}[!h]
  centering
  begin{tabular}{lSS}
    toprule
    A & {B} & {C}
    midrule
    Bob & SI{75}{percent} & -11.11
    Carla & SI{75}{percent} & 2.22
    Dale & SI{75}{percent} & -3.33
    Ena & SI{75}{percent} & 4.44
    bottomrule
  end{tabular}
  caption{Caption 3}
end{table}

end{document}

mwe

2 Answers

Quite similar to @leandriss answer, with slightly shorter code and equal width of boldface and normal face digits:

documentclass[oneside,11pt]{book}
usepackage{booktabs}
usepackage{siunitx}

usepackage{etoolbox}
newrobustcmdB{DeclareFontSeriesDefault[rm]{bf}{b}bfseries}
newcommandmcc[1]{multicolumn{1}{c}{#1}}

begin{document}
    begin{table}[!h]
    centering
sisetup{detect-weight,
         mode=text, 
         table-format=2.0
        }
begin{tabular}{lS[table-space-text-post=,%]<{,%}
                 S[table-format=-2.2]}
    toprule
A       & mcc{B}   & mcc{C}
    midrule
Bob     & 75        &B -11.11
Carla   & 75        &     2.22
Dale    & 75        &    -3.33
Ena     & 75        &     4.44
    bottomrule
  end{tabular}
caption{Caption 3}
label{tab:boldsiunitx}
    end{table}
end{document}

enter image description here

Edit: In the first version was in definition of the B lost option bfseries. Now is added and showed corrected table.

Correct answer by Zarko on July 5, 2021

In comparison to your original code, I added decect-weight to sisetup, used bfseries instead of textbf and placed usepackage{etoolbox}robustifybfseries in the preamble of the document. Lastly, I also used appropriate table-format options for both S type columns.

enter image description here

documentclass[oneside,11pt]{book}

usepackage[
  expansion = false ,
  tracking = smallcaps ,
  letterspace = 40 ,
]{microtype}
usepackage{booktabs}
usepackage{siunitx}
sisetup{%
  detect-family, detect-shape,
  product-units = power,
  list-final-separator = {, and },
  retain-explicit-plus,
  input-comparators = {<=>approxgegeqggleleqllsimlesssimgtrsim}, 
  detect-weight
}
DeclareSIUnit[number-unit-product = ]percent{char`%}

usepackage{etoolbox}
robustifybfseries
begin{document}

begin{table}[!h]
  centering
  begin{tabular}{lS[table-format=2,table-space-text-post=%]S[table-format=-2.2]}
    toprule
    A & {B} & {C}
    midrule
    Bob & SI{75}{percent} & bfseries -11.11
    Carla & SI{75}{percent} & 2.22
    Dale & SI{75}{percent} & -3.33
    Ena & SI{75}{percent} & 4.44
    bottomrule
  end{tabular}
  caption{Caption 3}
end{table}

end{document}

Answered by leandriis on July 5, 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