# Aligning numbers in grouped tabularx columns

I am trying to format a table with tabularx, see below.

The table content does not matter, I just put some random numbers there. As you can see, I tried to group the four categories (Apple, ...) and a total one (columns 2-4) into blocks of three columns using custom column types and modifying column separators.

I spent quite some time on trying to properly align the cmidrules below the categories, but I think they are matching the column groups properly now. The following two problems remain:

• I introduced a new column type for centering the column contents, but apparently everything is still left-aligned.
• I need to align possibly negative values in the last row with the exclusively positive numbers in the top rows. So I guess this means that I don't want to center the columns anyway, but need them to be right-justified. I have also read about siunitx column types, but could not successfully integrate it into my table.

\documentclass[11pt]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[scaled=1]{uarial}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{array}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{siunitx}

\begin{document}

\newcommand{\ap}[1]{AP\textsubscript{#1}}
\newcommand{\apavg}[0]{AP\textsubscript{$$\varnothing$$}}

\begin{figure}[!htbp]
\centering
\tiny
\newcolumntype{C}{>{\centering\arraybackslash}X} % centered X-column
\newcolumntype{B}{*{3}{C}} % block of 3 columns
\newcolumntype{d}{@{\hspace{3\tabcolsep}}} % 150% spacing
\begin{tabularx}{\textwidth}{c d B d B d B d B d B @{\hspace{2\tabcolsep}}}
\toprule
\multirow{2}[3]{*}{Model} & \multirow{2}[3]{*}{\ap{50}} & \multirow{2}[3]{*}{\ap{75}} & \multirow{2}[3]{*}{\apavg} & \multicolumn{3}{@{}cd}{Apple} & \multicolumn{3}{@{}cd}{Banana} & \multicolumn{3}{@{}cd}{Potato} & \multicolumn{3}{@{}c}{Orange} \\ \cmidrule(r{2\tabcolsep}){5-7} \cmidrule(r{2\tabcolsep}){8-10} \cmidrule(r{2\tabcolsep}){11-13} \cmidrule(r{\tabcolsep}){14-16}
&  &  &  & \ap{50} & \ap{75} & \apavg & \ap{50} & \ap{75} & \apavg & \ap{50} & \ap{75} & \apavg & \ap{50} & \ap{75} & \apavg \\
\midrule
Baseline & \num{0.100} & \num{0.460} & \num{0.810} & \num{0.210} & \num{0.450} & \num{0.520} & \num{0.880} & \num{0.020} & \num{0.080} & \num{0.240} & \num{0.080} & \num{0.600} & \num{0.050} & \num{0.990} & \num{0.030} \\
Improved & \num{0.200} & \num{0.360} & \num{0.860} & \num{0.390} & \num{0.400} & \num{0.600} & \num{0.900} & \num{0.020} & \num{0.080} & \num{0.200} & \num{0.080} & \num{0.600} & \num{0.100} & \num{0.990} & \num{0.100} \\
$$\Delta$$ & \num{0.100} & \num{-0.100} & \num{0.050} & \num{0.180} & \num{-0.050} & \num{0.080} & \num{0.020} & \num{0.000} & \num{0.000} & \num{-0.040} & \num{0.000} & \num{0.000} & \num{0.050} & \num{0.000} & \num{0.070} \\
\bottomrule
\end{tabularx}
\end{figure}

\end{document}

• Welcome to TeX.SE.
– Mico
Sep 2, 2020 at 21:36
• Why not use the S column type from the siunitx package? Sep 2, 2020 at 21:46

My main suggestion would be to use a tabular* environment instead of a tabularx environment. I say this because one of the main features of the X column type -- automatic line-breaking of cell contents -- does not appear to be relevant for the table at hand.

I'd use the S column type instead of the X (or C) column type for the 15 data columns, and I wouldn't encase the numbers in \num statements. Finally, I'd encase the tabular material in a table environment, not in a figure environment.

\documentclass[11pt]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[scaled=1]{helvet} % I don't seem to have 'uarial' package
\renewcommand{\familydefault}{\sfdefault}
\usepackage{array,amssymb,booktabs,siunitx}

\newcommand{\ap}[1]{AP\textsubscript{#1}}
\newcommand{\apavg}[0]{AP\textsubscript{$$\!\varnothing$$}}

\begin{document}

\begin{table}[!htbp] % <-- 'table', not 'figure'
\setlength\tabcolsep{0pt}
\tiny
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} % <-- use 'tabular*'
l *{15}{S[table-format=1.3]} }
\toprule
Model & &&&
\multicolumn{3}{c}{Apple}  & \multicolumn{3}{c}{Banana} &
\multicolumn{3}{c}{Potato} & \multicolumn{3}{c}{Orange} \\
\cmidrule{5-7} \cmidrule{8-10} \cmidrule{11-13} \cmidrule{14-16}
& {\ap{50}} & {\ap{75}} & {\apavg}
& {\ap{50}} & {\ap{75}} & {\apavg} & {\ap{50}} & {\ap{75}} & {\apavg}
& {\ap{50}} & {\ap{75}} & {\apavg} & {\ap{50}} & {\ap{75}} & {\apavg} \\
\midrule
Baseline & 0.100 & 0.460 & 0.810 & 0.210 & 0.450 & 0.520 & 0.880 & 0.020 & 0.080 & 0.240 & 0.080 & 0.600 & 0.050 & 0.990 & 0.030\\
Improved & 0.200 & 0.360 & 0.860 & 0.390 & 0.400 & 0.600 & 0.900 & 0.020 & 0.080 & 0.200 & 0.080 & 0.600 & 0.100 & 0.990 & 0.100\\
Diff. & 0.100 & -0.100 & 0.050 & 0.180 & -0.050 & 0.080 & 0.020 & 0.000 & 0.000 & -0.040 & 0.000 & 0.000 & 0.050 & 0.000 & 0.070\\
\bottomrule
\end{tabular*}
\end{table}
\end{document}

• Thanks for your help, I couldn't get the S column type to work before. Is there a way to reintroduce the "grouping" of 3 columns? If you look closely at my example, you can see that the space between AP50 , AP75, APavg for each category is smaller than the one between the categories. Sep 3, 2020 at 10:00
• @lerei - I had noticed the slight variations in intercolumn whitespace in your code, but I chose not to keep them as, in my view, it suffices to lay out the header material with visual clarity in mind. The marginal product of adding a further "decorative element", viz., having uneven amounts of intercolumn whitespace, to the table is negligible.
– Mico
Sep 3, 2020 at 10:53
• Okay, I see your point. I am pretty happy with it now, after managing to readd the \multirows. Sep 3, 2020 at 11:27