我怎样才能使 siunitx 的 \num 变为粗体?

我怎样才能使 siunitx 的 \num 变为粗体?

我想要的是:

在此处输入图片描述

最小示例

\documentclass[a4paper]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\usepackage{booktabs}
\usepackage[binary-units,group-separator={,}]{siunitx}
\sisetup{per-mode=fraction,
         binary-units=true,
         group-separator = {\,},
         range-phrase=-}
\DeclareSIUnit\pixel{px}
\DeclareSIUnit\float{float}
\DeclareSIUnit\floats{floats}

\begin{document}

\begin{table}[ht]
    \centering
    \begin{tabular}{cllrrl}
    \toprule
    \# & Type                     & \parbox[t]{3cm}{Filters @\par Patch size / stride} & Parameters & FLOPs & Output size  \\\midrule
       & Input                    &                                           &          0  &            0 & \hphantom{0}1 @ $32\times 32$    \\
    1  & Convolution              & \hphantom{0}6 @ $5 \times 5 \times 1$ / 1 &        156  & \num{307800} & \hphantom{0}\bf{6}\,@\,$\mathbf{28\times 28}$    \\
    2  & Scaled average pooling   & \hphantom{00 @} $2 \times 2 \hphantom{\times 00}$ / 2 &          2  &    \num{336} & \hphantom{0}6 @ $14\times 14$    \\
    3  & Convolution              & 16 @ $5 \times 5 \times 6$ / 1            &  \num{2416} & \bf{\num{942400}} & 16 @ $10\times 10$ \\
    4  & Scaled average pooling   & \hphantom{00 @} $2 \times 2 \hphantom{\times 00}$ / 2 &          2  &   \num{1600} & 16 @ \hphantom{0}$5\times \hphantom{0}5$   \\
    5  & Fully Connected          & 120 neurons                      & \ensuremath{\boldmath\num{48120}} & \num{240000} & 120 \\
    6  & Fully Connected          & \hphantom{0}84 neurons           & \num{10164} &  \num{20580} & \hphantom{0}84 \\
    7  & Fully Connected (output) & \hphantom{0}10 neurons           &        850  &   \num{1730} & \hphantom{0}10 \\\midrule
    $\sum$ &                      &                                  & \num{61710} &\num{15144446}& \num{9118}\\
    \bottomrule
    \end{tabular}
\end{table}
\end{document}

我尝试过

我尝试过将其\num置于数学模式和\mathbf/ \boldmath, \bf, \bfseries。但毫无作用。

答案1

由于粗体字体比非粗体字体更宽,因此对齐不完美。粗体字体是通过siunitx包选项和在第三列和第四列中detect-weight使用列类型实现的:S

\documentclass[a4paper]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\usepackage{booktabs, makecell}
\renewcommand\theadfont{\normalsize}
\usepackage[binary-units,group-separator={,}]{siunitx}
\sisetup{per-mode=fraction,
         binary-units=true,
         group-separator = {\,},
         range-phrase=-}
\DeclareSIUnit\pixel{px}
\DeclareSIUnit\float{float}
\DeclareSIUnit\floats{floats}

\usepackage{etoolbox}% <-- for bold fonts
\newcommand{\ubold}{\fontseries{b}\selectfont}% <-- for bold fonts
\robustify\ubold% <-- for bold fonts

\begin{document}

\begin{table}[ht]
    \centering
    \begin{tabular}{cll 
                    S[detect-weight,group-minimum-digits = 4,table-format=5.0]% <-- changed
                    S[detect-weight,group-minimum-digits = 4,table-format=8.0]% <-- changed
                    l}
    \toprule
\# &    Type                     
        &   \thead[tl]{Filters @\\ Patch size / stride} 
            &   {Parameters}    &   {FLOPs}   & Output size  \\\midrule
   &    Input                    
        &   &           0       &       0   & \hphantom{0}1 @ $32\times 32$    \\
1  &    Convolution              
        &   \hphantom{0}6 @ $5 \times 5 \times 1$ / 1 
            &       156         &   307 800 & \hphantom{0}\textbf{6}\,@\,$\mathbf{28\times 28}$    \\
2  &    Scaled average pooling   
        & \hphantom{00 @} $2 \times 2 \hphantom{\times 00}$ / 2 
            &        2          &       336 & \hphantom{0}6 @ $14\times 14$    \\
3  &    Convolution              
        &   16 @ $5 \times 5 \times 6$ / 1            
            &   2 416           & \ubold 942 400% <-- boldface
                                    & 16 @ $10\times 10$ \\
4  &    Scaled average pooling   
        &   \hphantom{00 @} $2 \times 2 \hphantom{\times 00}$ / 2 
            &        2          &     1 600 & 16 @ \hphantom{0}$5\times \hphantom{0}5$   \\
5  &    Fully Connected          
        &   120 neurons                      
            & \ubold 48 120% <-- boldface
                & 240 000   & 120 \\
6  &    Fully Connected          
        &   \hphantom{0}84 neurons           
                &       10 164  &   20 580  & \hphantom{0}84 \\
7  & Fully Connected (output) 
        &   \hphantom{0}10 neurons           
            &               850 &   1 730   & \hphantom{0}10 \\ \midrule
$\sum$  &   &   &        61 710 & 15 144 446& \num{9118}\\
    \bottomrule
    \end{tabular}
\end{table}
\end{document}

在此处输入图片描述

注意: \bf 的使用已贬值 +20 年。

编辑:

同时(经过多年和下面的评论),我们中的许多人学会了如何管理S列表中的粗体数字。在原始答案中没有添加选项mode=text,并且\sisetup定义ubold也可以改进:

% preamble as before
\usepackage{etoolbox}% <-- for robustify bold fonts in S columns
\newrobustcmd\ubold{\DeclareFontSeriesDefault[rm]{bf}{b}\bfseries}% <-- changed

\begin{document}
    \begin{table}[ht]
    \centering
\sisetup{detect-weight,
         group-minimum-digits = 4,
         mode=text    % <--- added
         }
    \begin{tabular}{cll
                    S[table-format=5.0]% <-- changed
                    S[table-format=8.0]% <-- changed
                    l}
% table body

在 MWE 中考虑上述内容可得出预期的结果:

在此处输入图片描述

答案2

您可以本地重新定义\bfseries使用\fontseries{b},而不是\fontseries{bx}像平常一样。

您还可以通过使用更多列来避免所有这些幻影。

\documentclass[a4paper]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\usepackage{booktabs, makecell,etoolbox}
\renewcommand\theadfont{\normalsize}
\usepackage[binary-units,group-separator={,}]{siunitx}
\sisetup{per-mode=fraction,
         binary-units=true,
         group-separator = {\,},
         range-phrase=-}
\DeclareSIUnit\pixel{px}
\DeclareSIUnit\float{float}
\DeclareSIUnit\floats{floats}

\begin{document}

\begin{table}[ht]
\renewrobustcmd{\bfseries}{\fontseries{b}\selectfont}
\sisetup{detect-weight,mode=text,group-minimum-digits = 4}

\centering

\small
\addtolength{\tabcolsep}{-1.5pt}% decide at the end
\begin{tabular}{
  @{}
  c
  l
  S[table-format=3.0]@{\,}c@{\,}l@{\,}c@{\,}l
  S[table-format=5.0]
  S[table-format=8.0]
  S[table-format=4.0]@{\,}c@{\,}S[table-format=2.0]@{}>{${}}c<{{}$}@{}S[table-format=2.0]
  @{}
    }
\toprule
\# & Type
   & \multicolumn{5}{c}{\begin{tabular}[t]{l}Filters @\\ Patch size / stride\end{tabular}}
   & {Parameters}
   & {FLOPs}
   & \multicolumn{5}{c}{Output size}
\\
\midrule
  &    Input                    
       &&&&&   &           0       &       0   & 1 & @ & 32 & \times & 32    \\
1 &    Convolution
       & 6 & @ & $5 \times 5 \times 1$ & / & 1 
       & 156 & 307 800 & \bfseries 6 & @ & \bfseries 28 & \times & \bfseries 28 \\
2 &    Scaled average pooling   
       &&& $2 \times 2$ & / & 2
       & 2 & 336 & 6 & @ & 14 & \times & 14 \\
3 &    Convolution
       & 16 & @ & $5 \times 5 \times 6$ & / & 1
       & 2 416 & \bfseries 942 400 & 16 & @ & 10 & \times & 10 \\
4 &    Scaled average pooling
       &&& $2 \times 2$ & / & 2
       & 2 & 1 600 & 16 & @ & 5 & \times & 5 \\
5 &    Fully Connected
        & 120 & \multicolumn{4}{@{}l}{\ neurons}
        & \bfseries 48 120 & 240 000   & 120 \\
6 &    Fully Connected
        & 84 & \multicolumn{4}{@{}l}{\ neurons}
        & 10 164 & 20 580  & 84 \\
7 & Fully Connected (output)
        & 10 & \multicolumn{4}{@{}l}{\ neurons}
        & 850 & 1 730 & 10 \\ \midrule
$\sum$  &&&&&&& 61 710 & 15 144 446 & 9118\\
\bottomrule
\end{tabular}

\end{table}
\end{document}

在此处输入图片描述

答案3

由于我还不能发表评论,所以我发布了一个额外的答案。

任何使用较新版本的 Latex 的人,请参阅这个问题和答案

根据答案, \bfseries 的行为发生了变化。@egreg 的解决方案使用 mode=text 在定义新的强大命令时仍然有效,例如

\newrobustcmd\B{\DeclareFontSeriesDefault[rm]{bf}{b}\bfseries}

\B并在表格中使用该命令( )而不是\bfseries

相关内容