5

I have this code which I adapted from Big table with rotated column labels using booktabs:

\usepackage{booktabs}
\begin{document}

\begin{table*}  
\centering
\begin{tabular}{cccccccccc}  

\toprule  
& \multicolumn{2}{>{\begin{minipage}[c]{2cm}}l<{\end{minipage}}}{\centering Group 1\\ some subtitle}
& \multicolumn{4}{>{\begin{minipage}[c]{2cm}}l<{\end{minipage}}}{\centering Group 2\\ some subtitle}
& \multicolumn{3}{>{\begin{minipage}[c]{2cm}}l<{\end{minipage}}}{\centering Group 3\\ some subtitle} \\

 \cmidrule(lr){2-3} \cmidrule(lr){4-7}\cmidrule(lr){8-10}

& \multicolumn{1}{>{\begin{turn}{90}\begin{minipage}[c][0.5cm][c]{2cm}}l<{\end{minipage}\end{turn}}}{some heading\\ blabla}
& \multicolumn{1}{>{\begin{turn}{90}\begin{minipage}[c][0.5cm][c]{2cm}}l<{\end{minipage}\end{turn}}}{not correctly\\ aligned with data}
& \multicolumn{1}{>{\begin{turn}{90}\begin{minipage}[c][0.5cm][c]{2cm}}l<{\end{minipage}\end{turn}}}{some heading \\ blabla}
& \multicolumn{1}{>{\begin{turn}{90}\begin{minipage}[c][0.5cm][c]{2cm}}l<{\end{minipage}\end{turn}}}{some heading}
& \multicolumn{1}{>{\begin{turn}{90}\begin{minipage}[c][0.5cm][c]{2cm}}l<{\end{minipage}\end{turn}}}{some heading}
& \multicolumn{1}{>{\begin{turn}{90}\begin{minipage}[c][0.5cm][c]{2cm}}l<{\end{minipage}\end{turn}}}{some heading\\ this one is long}
& \multicolumn{1}{>{\begin{turn}{90}\begin{minipage}[c][0.5cm][c]{2cm}}l<{\end{minipage}\end{turn}}}{some heading}
& \multicolumn{1}{>{\begin{turn}{90}\begin{minipage}[c][0.5cm][c]{2cm}}l<{\end{minipage}\end{turn}}}{some heading}
& \multicolumn{1}{>{\begin{turn}{90}\begin{minipage}[c][0.5cm][c]{2cm}}l<{\end{minipage}\end{turn}}}{some heading\\ this one is long}\\

\midrule

Sample1 & 122 & 109 &  0 & 0 & 55 & 48 & 63 & 61 & 1 \\
Sample2 & 122 & 109 &  0 & 0 & 55 & 48 & 63 & 61 & 1 \\ 
Sample3 & 122 & 109 &  0 & 0 & 55 & 48 & 63 & 61 & 1 \\ 
Sample4 & 122 & 109 &  0 & 0 & 55 & 48 & 63 & 61 & 1 \\ 

\midrule
Total   & 122 & 109 &  0 & 0 & 55 & 48 & 63 & 61 & 1 \\ 

\bottomrule

\end{tabular}
\end{table*}
\end{document}

It generates this: enter image description here

Here are a few things I'm not so sure how to do:

1) The "Group 2" and "Group 3" headings are not correctly centered. I would like each "Group x" heading to be centered whatever is the number of columns below that heading.

2) The second rotated sub-heading for Group 1 is not properly aligned with the data. I would like each rotated sub-heading to be aligned with the corresponding data.

3) Additionally, I would like to insert a (small) blank column between each group (for instance, between Group 1 and Group 2).

1 Answer 1

3

You can have a much simpler code with the makecell package, using its \rothead command without having to use a minipage. Set the width of the rotated headings to the length of the longest heading line, and the trick is done. The makecell package allows for line breaks in cells while keeping them centred (by default) both vertically and horizontally.

In addition, I use the cellspace package to ensure a minimal spacing above and below the contents of a column cell if the specifier of this column is prefixed with the letter S.

 \documentclass{article}
\usepackage{array, booktabs}
\usepackage{rotating}
\usepackage{makecell}
\renewcommand\theadfont{\normalsize}

\usepackage{cellspace}
\setlength\cellspacetoplimit{3pt}
\setlength\cellspacebottomlimit{3pt}

\begin{document}

\begin{table*}
\settowidth\rotheadsize{\theadfont aligned with data}
\centering
\begin{tabular}{Scc@{\hskip 1pt}cc@{\hskip 3pt}cc@{\hskip 3pt}ccc@{\hskip 3pt}c}
\toprule
& \multicolumn{2}{c}{\thead{Group 1\\some subtitle}}
& \multicolumn{4}{c}{\thead {Group 2\\ some subtitle}}
& \multicolumn{3}{c}{\thead {Group 3\\ some subtitle}} \\
 \cmidrule(lr){2-3} \cmidrule(lr){4-7}\cmidrule(lr){8-10}
& \rothead{some heading\\[-0.5ex] blabla}
& \rothead{now correctly\\[-0.5ex] aligned with data}
& \rothead{some heading \\[-0.5ex] blabla}
& \rothead{some heading}
& \rothead{some heading}
& \rothead{some heading\\[-0.5ex] this one is long}
& \rothead{some heading}
& \rothead{some heading}
& \rothead{some heading\\[-0.5ex] this one is long}\\
\midrule
\addlinespace
Sample1 & 122 & 109 & 0 & 0 & 55 & 48 & 63 & 61 & 1 \\
Sample2 & 122 & 109 & 0 & 0 & 55 & 48 & 63 & 61 & 1 \\
Sample3 & 122 & 109 & 0 & 0 & 55 & 48 & 63 & 61 & 1 \\
Sample4 & 122 & 109 & 0 & 0 & 55 & 48 & 63 & 61 & 1 \\
\midrule
Total & 122 & 109 & 0 & 0 & 55 & 48 & 63 & 61 & 1 \\
\addlinespace
\bottomrule
\end{tabular}
\end{table*}

\end{document} 

enter image description here

2
  • Thanks Bernard for your answer. Is there a way to control the space between two columns? For instance, say that even without using the prefix S, I find the space between the first two columns of "Group 1" two wide: is there a way to reduce that space?
    – tfh
    Commented Nov 7, 2014 at 3:28
  • @tfh: Well, there are different constraints that would require different values of \tabcolsep (6pt by default). You can change this value for pairs of particular columns by trial and error, inserting some @{\hskip xx pt} but it will always be a compromise, I'm afraid. Check if my modified answer suits you better. Also note that the S pre-qualifier has to be used for only one (well-chosen) column.
    – Bernard
    Commented Nov 7, 2014 at 9:42

You must log in to answer this question.

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