2

I have a table where I am aligning all numbers in the cells to the decimal point (via package 'siunitx'). Additionally, I am highlighting in each column the two lowest number via changing the number's color to red/blue.

The problem is now, that sometimes (e.g. in the second column), the colored numbers are not aligned to decimal point. What do I make wrong ?

The rendered table (in Overleaf) looks like this: enter image description here

The latex source code for the table is the following:

\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
  • Welcome to TeX.SE!
    – Mensch
    Nov 30, 2021 at 17:40

2 Answers 2

4

Instead of employing the c column type for the four numeric data columns and encasing all numbers in \MyAlign macros, I'd use the S column type directly. Optionally, consider using a tabular* environment (with width set to \textwidth) rather than a tabular environment; this should make it unnecessary to have to employ \small on the contents of the table.

enter image description here

\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

You have doubled braces around the 2 problematic numbers (\MyAlign{{\color{Maroon}8.89}}). Remove the doubling and your issue is resolved.

\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}

enter image description here

4
  • For the sake of aesthetics, you may want to insert a bit of whitespace between the caption and the table's \toprule.
    – Mico
    Nov 30, 2021 at 18:22
  • Wow, that was fast ! Many thanks to Mico and Dan ! Both methods work. @Mico - good idea with the whitespace after the caption. How can I do that ?
    – Hannes F.
    Nov 30, 2021 at 18:35
  • @HannesF. I just edited it in. Just add an \medskip after the caption.
    – Dan
    Nov 30, 2021 at 18:36
  • @HannesF. - You may use either Dan's solution (\medskip after \caption{...}) or mine (\strut at the end of the argument of \caption).
    – Mico
    Nov 30, 2021 at 19:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .