我正在使用siunitx
表格环境中的包。我怎样才能使条目变成粗体?以下是 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 = {<=>\approx\ge\geq\gg\le\leq\ll\sim\lesssim\gtrsim}
}
\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}
答案1
与@leandriss 的答案非常相似,代码略短,粗体数字和普通数字的宽度相等:
\documentclass[oneside,11pt]{book}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{etoolbox}
\newrobustcmd\B{\DeclareFontSeriesDefault[rm]{bf}{b}\bfseries}
\newcommand\mcc[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}
编辑:\B
在第一个版本中丢失了选项的定义\bfseries
。现在已添加并显示更正后的表格。
答案2
与原始代码相比,我添加了decect-weight
,\sisetup
用bfseries
代替 ,并将\textbf
其放在\usepackage{etoolbox}\robustify\bfseries
文档的前言中。最后,我还table-format
为两种类型列使用了适当的选项S
。
\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 = {<=>\approx\ge\geq\gg\le\leq\ll\sim\lesssim\gtrsim},
detect-weight
}
\DeclareSIUnit[number-unit-product = ]\percent{\char`\%}
\usepackage{etoolbox}
\robustify\bfseries
\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}