2

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.

0

1 Answer 1

2

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}
4
  • Thanks for your quick response! I still have the feeling that "Short" is not exactly centered. Is there any offset, e.g. due to the additional spacing (\hskip 0.05in) that has to be considered? Or am I wrong regarding the definition of the center?
    – MVH
    Commented Aug 23, 2015 at 11:59
  • @MVH - I've updated my answer to tweak the centering of the "short" headers.
    – Mico
    Commented Aug 23, 2015 at 12:12
  • Is it possible that the picture showing the result of the tex code was updated but the tex code that is displayed on the page was not? I can't figure out the change you made and when I build the document myself the shift is still present (opposed to the picture). Thanks again!
    – MVH
    Commented Aug 23, 2015 at 13:04
  • @MVH - Sorry for that. Indeed, I had updated the code in the text -- \multicolumn{2}{@{}c@{\hskip0.25in}}{Short} -- but not in the MWE. I've supplied the missing @{} particles now.
    – Mico
    Commented Aug 23, 2015 at 13:18

You must log in to answer this question.

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