[Tex/LaTex] Grouping of aligned table columns in combination with multicolumn and a long header

columnsmulticolumntables

I want to create a table with three pairs of columns. The pairs should be identifiable based on the horizontal distance between the different pairs of columns. Each pair of columns consists of a column with an absolute number and a column with a relative number (percentage). Absolute and relative numbers are integers but do not not have the same number of digits, and numbers should be aligned to the right. Basically, I have this:

\documentclass{scrreprt}

\usepackage{booktabs}
\begin{document}
\begin{table}
    \centering
    \begin{tabular}{l@{\hskip 0.25in}
                    r@{\hskip 0.05in}r@{\hskip 0.25in}
                    r@{\hskip 0.05in}r@{\hskip 0.25in}
                    r@{\hskip 0.05in}r @{\hskip 0.25in}
                   }
    \toprule
    %   &  \multicolumn{2}{c}{Short} & \multicolumn{2}{c}{Short} & \multicolumn{2}{c}{LongLongLongLong}\\
    \midrule
    A&  777& (80\%)&265&(16\%)& 330&(80\%)\\
    B&  409& (72\%) &999&(64\%) &163&(57\%) \\
    C&  208&(100\%) &54&(100\%) &66&(100\%)\\
    D   &218& (90\%)&   98&(73\%)&84&(77\%)\\
    \bottomrule
    \end{tabular}
\end{table}
\end{document}

As long as the header is not displayed the output I desire is pretty much the same as generated by the code. However, if the header is displayed the "Short" headers are not centered on the pairs of columns and, even worse, in case of the "LongLongLongLong" header the space between the last two columns is enlarged.

How can I manage to have headers that are centered on the pairs of columns and prevent the stretching of the space between grouped columns in case of the long header? Solutions, for instance, not relying on using a separate column for absolute and relative numbers that still satisfy my requirements are also welcome.

Best Answer

The reason why the "Short" headers don't look like they're centered properly is that the instruction

\multicolumn{2}{c}{Short}

centers the string over two columns that are defined as

@{\hskip 0.25in}r@{\hskip 0.05in}r

To get the correct centering of the "Short" headers, you need to write

\multicolumn{2}{@{}c@{\hskip0.25in}}{Short}

The problem with the "LongLongLongLong" header is that it's assigned the column type c, which doesn't allow line breaks. If you want to keep the overall width of the pair of columns, you must allow one or more line breaks in the header cell. In the code below, I suggest using a centered version of the p column type.

enter image description here

\documentclass{scrreprt}
\usepackage{booktabs,array,ragged2e}
\newcolumntype{C}[1]{>{\Centering\arraybackslash}p{#1}}
\begin{document}
\begin{table}
    \centering
    \begin{tabular}{ l *{3}{@{\hskip0.25in}r@{\hskip 0.05in}r} }
    \toprule
       & \multicolumn{2}{@{}c@{\hskip0.25in}}{Short} 
       & \multicolumn{2}{@{}c@{\hskip0.25in}}{Short} 
       & \multicolumn{2}{@{\,}C{1cm}}{LongLong\-LongLong}\\
    \midrule
    A&  535& (97\%)&265&(96\%)& 250&(90\%)\\
    B&  409& (72\%) &181&(64\%) &163&(57\%) \\
    C&  108&(100\%) &54&(100\%) &54&(100\%)\\
    D   &208& (93\%)&   98&(88\%)&84&(75\%)\\
    \bottomrule
    \end{tabular}
\end{table}
\end{document}
Related Question