对于彩色文本,表格中的数字与小数点的对齐并不总是有效

对于彩色文本,表格中的数字与小数点的对齐并不总是有效

我有一张表格,其中单元格中的所有数字都对齐到小数点(通过包“siunitx”)。此外,我通过将数字的颜色更改为红色/蓝色来突出显示每列中的两个最低数字。

现在的问题是,有时(例如在第二列),彩色数字未与小数点对齐。我做错了什么?

呈现的表格(在 Overleaf 中)如下所示: 在此处输入图片描述

该表的 Latex 源代码如下:

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\begin{document}
% We define a macro for aligning a number to the decimal point, with 2 digits
% The 'S' command is from 'siunitx' package
\newcommand{\MyAlign}[1]{\multicolumn{1}{S[detect-weight,table-format=2.2]}{#1}}

\begin{table}[b]
\small
\centering
\caption{Results. The best and second-best result for each model is marked in {\color{Maroon}red} and {\color{NavyBlue}blue}. }
\label{tbl:cifar10}
\begin{tabular}{l c c c c}
\toprule
\textbf{Algorithm} &
ResNet-50 & DenseNet-121 & MobileNetV2 & EfficientNet-B0 \\
\midrule
$Adam$ &               \MyAlign{12.89} & \MyAlign{10.31} & \MyAlign{14.33} & \MyAlign{21.18} \\
$AdamW$ &              \MyAlign{13.27} & \MyAlign{9.32} & \MyAlign{15.18} & \MyAlign{21.41} \\
$AdaBelief$ &          \MyAlign{12.70} & \MyAlign{8.93} & \MyAlign{14.97} & \MyAlign{21.45} \\
$AdaMomentum$ &        \MyAlign{14.11} & \MyAlign{9.48} & \MyAlign{{\color{Maroon}14.15}} & \MyAlign{{\color{NavyBlue}19.56}} \\
$AdaFamily_{(0.0)}$  & \MyAlign{{\color{NavyBlue}12.69}} & \MyAlign{8.93} & \MyAlign{15.07} & \MyAlign{21.61} \\
$AdaFamily_{(0.25)}$ & \MyAlign{12.71} & \MyAlign{{\color{Maroon}8.89}} & \MyAlign{15.34} & \MyAlign{22.29} \\
$AdaFamily_{(0.5)}$  & \MyAlign{{\color{Maroon}12.65}} & \MyAlign{{\color{NavyBlue}8.92}} & \MyAlign{14.85} & \MyAlign{21.55} \\
$AdaFamily_{(0.75)}$ & \MyAlign{13.79} & \MyAlign{9.21} & \MyAlign{14.19} & \MyAlign{{\color{Maroon}19.36}} \\
$AdaFamily_{(1.0)}$  & \MyAlign{14.56} & \MyAlign{9.50} & \MyAlign{{\color{NavyBlue}14.18}} & \MyAlign{19.67} \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

答案1

我不会c对四个数字数据列使用列类型并将所有数字包含在宏中,而是直接使用列类型。或者,考虑使用环境(将宽度设置为)而不是环境;这样就不必对表的内容使用。\MyAlignStabular*\textwidthtabular\small

在此处输入图片描述

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}  % for 'S' column type
\usepackage[dvipsnames]{xcolor}
\newcommand\NavyBlue{\color{NavyBlue}} % 2 handy shortcut macros
\newcommand\Maroon{\color{Maroon}}
\begin{document}

\begin{table}[b]
\setlength{\tabcolsep}{0pt}
\caption{Results. The best and second-best result for each model 
is marked in \textcolor{Maroon}{red} and \textcolor{NavyBlue}{blue}.\strut}
\label{tbl:cifar10}

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{4}{S[detect-weight,table-format=2.2]} }
\toprule
\textbf{Algorithm} & {ResNet-50} & {DenseNet-121} & {MobileNetV2} & {EfficientNet-B0} \\
\midrule
Adam                 &           12.89 &          10.31 &           14.33 & 21.18 \\
AdamW                &           13.27 &           9.32 &           15.18 & 21.41 \\
AdaBelief            &           12.70 &           8.93 &           14.97 & 21.45 \\
AdaMomentum          &           14.11 &           9.48 & \Maroon   14.15 & \NavyBlue 19.56 \\
AdaFamily$_{(0.0)}$  & \NavyBlue 12.69 &           8.93 &           15.07 & 21.61 \\
AdaFamily$_{(0.25)}$ &           12.71 & \Maroon   8.89 &           15.34 & 22.29 \\
AdaFamily$_{(0.5)}$  & \Maroon   12.65 & \NavyBlue 8.92 &           14.85 & 21.55 \\
AdaFamily$_{(0.75)}$ &           13.79 &           9.21 &           14.19 & \Maroon 19.36   \\
AdaFamily$_{(1.0)}$  &           14.56 &           9.50 & \NavyBlue 14.18 & 19.67 \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}

答案2

您在两个有问题的数字 ( \MyAlign{{\color{Maroon}8.89}}) 周围使用了双重括号。删除双重括号即可解决您的问题。

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\begin{document}
% We define a macro for aligning a number to the decimal point, with 2 digits
% The 'S' command is from 'siunitx' package
\newcommand{\MyAlign}[1]{\multicolumn{1}{S[detect-weight,table-format=2.2]}{#1}}

\begin{table}[b]
\small
\centering
\caption{Results. The best and second-best result for each model is marked in {\color{Maroon}red} and {\color{NavyBlue}blue}.}
\medskip
\label{tbl:cifar10}
\begin{tabular}{l c c c c}
\toprule
\textbf{Algorithm} &
ResNet-50 & DenseNet-121 & MobileNetV2 & EfficientNet-B0 \\
\midrule
$Adam$ &               \MyAlign{12.89} & \MyAlign{10.31} & \MyAlign{14.33} & \MyAlign{21.18} \\
$AdamW$ &              \MyAlign{13.27} & \MyAlign{9.32} & \MyAlign{15.18} & \MyAlign{21.41} \\
$AdaBelief$ &          \MyAlign{12.70} & \MyAlign{8.93} & \MyAlign{14.97} & \MyAlign{21.45} \\
$AdaMomentum$ &        \MyAlign{14.11} & \MyAlign{9.48} & \MyAlign{{\color{Maroon}14.15}} & \MyAlign{{\color{NavyBlue}19.56}} \\
$AdaFamily_{(0.0)}$  & \MyAlign{{\color{NavyBlue}12.69}} & \MyAlign{8.93} & \MyAlign{15.07} & \MyAlign{21.61} \\
$AdaFamily_{(0.25)}$ & \MyAlign{12.71} & \MyAlign{\color{Maroon}8.89} & \MyAlign{15.34} & \MyAlign{22.29} \\%<--
$AdaFamily_{(0.5)}$  & \MyAlign{{\color{Maroon}12.65}} & \MyAlign{\color{NavyBlue}8.92} & \MyAlign{14.85} & \MyAlign{21.55} \\%<--
$AdaFamily_{(0.75)}$ & \MyAlign{13.79} & \MyAlign{9.21} & \MyAlign{14.19} & \MyAlign{{\color{Maroon}19.36}} \\
$AdaFamily_{(1.0)}$  & \MyAlign{14.56} & \MyAlign{9.50} & \MyAlign{{\color{NavyBlue}14.18}} & \MyAlign{19.67} \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

在此处输入图片描述

相关内容