6

I'm trying to get a big table to fit into my document using the booktabs package, but it is too wide. An example of a couple of rows of the table is given below. I'm using the \noindent\makebox[\textwidth]{% line because that seemed to work for me before, but its not helping now.

Example:

\usepackage{graphicx}  
\usepackage{booktabs}  

\begin{document}  

\begin{table}  
\small  
\begin{center}  
\noindent\makebox[\textwidth]{%  
\begin{tabular}{lcccccccccccccccccc}  
\toprule  
Event & \multicolumn{2}{c}{Counts}& \multicolumn{8}{c}{Genres} &\multicolumn{8}{c}{Genres blabla} \\  
\cmidrule(r){1-1} \cmidrule(r){2-3} \cmidrule(r){4-11}\cmidrule(r){12-19}  
&&&&&&&&&&&&&&&&&&\\  
\midrule  
Test1 & 138 & 8 & 0 & 0 & 60 & 4 & 61 & 4 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 16 & 0\\  
Test2 & 162 & 136 & 0 & 0 & 71 & 63 & 89 & 72 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 2 & 1\\  
Test3 & 161 & 120 & 0 & 0 & 66 & 40 & 85 & 77 & 0 & 0 & 0 & 0 & 0 & 0 & 6 & 3 & 4 & 0\\  
Test4 & 122 & 109 & 0 & 0 & 55 & 48 & 63 & 61 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 3 & 0\\  
Test5 & 126 & 3 & 1 & 0 & 45 & 1 & 36 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 43 & 1\\  
\bottomrule
\end{tabular}
}
\end{center}
\end{table}

\end{document}

And secondly, on the &&&&&&&&&&&&&&&&&&\\ line, I need to add the column names. But these are quite long, so i would like to have them rotated about 45 degrees and close to eachother. I tried \rotatebox{45}{sentence1} & \rotatebox{45}{sentence2} & ... , but that doesn't do exactly what I want. Using \rotatebox, the head of sentence2 will start where the tail of sentence1 ended. I would like the head of sentence2 to be right next to the head of sentence1.

Below is an example of what i mean. When this code is compiled, there is a big spacing between the 3 headings. I would like the word "this" of the second heading to be right next to the "this" of the first one. etc.

\documentclass[12pt]{article}
\usepackage{graphicx}

\begin{document} 
\begin{table}  
\begin{tabular}{ccc}
\rotatebox{45}{This is a very long heading} & \rotatebox{45}{This is a very long heading} & \rotatebox{45}{This is a very long heading}
\end{tabular}
\end{table}
\end{document}

I hope that makes sense

1
  • Welcome to tex.sx! A tip: If you indent lines by 4 spaces, then they are marked as a code sample. You can also highlight the code and click the "code" button (with "101010" on it).
    – Seamus
    Commented Apr 1, 2011 at 15:43

2 Answers 2

9

You could try reducing \tabcolsep (the space between the beginning of a column and its contents; default value of 6pt) and changing to a smaller font size.

EDIT: Rotating the headings 45 degrees and then overlapping then is not a good idea since it could be visually confusing; a better approach would be to rotate the headings 90 degrees; this can be done rotating a minipage with the turn environment from the rotating package:

\documentclass{article}
\usepackage{rotating} 
\usepackage{array}
\usepackage{booktabs}  

\newcolumntype{R}[1]{>{\begin{turn}{90}\begin{minipage}{#1}\scriptsize}l%
<{\end{minipage}\end{turn}}%
}

\begin{document}  

\begin{table}  
  \footnotesize
  \setlength\tabcolsep{3pt}
  \centering
  \begin{tabular}{lcccccccccccccccccc}  
    \toprule  
    Event & \multicolumn{2}{c}{Counts}& \multicolumn{8}{c}{Genres} &
      \multicolumn{8}{c}{Genres blabla} \\  
    \cmidrule(r){1-1} \cmidrule(r){2-3} \cmidrule(r){4-11}\cmidrule(l){12-19}  
    \multicolumn{1}{R{2cm}}{some long long\\ heading} 
      & \multicolumn{1}{R{2cm}}{some long long\\ heading}
      & \multicolumn{1}{R{2cm}}{some long long\\ heading}
      & \multicolumn{1}{R{2cm}}{short heading}
      & \multicolumn{1}{R{2cm}}{some long long\\ heading}
      & \multicolumn{1}{R{2cm}}{some long long\\ heading}
      & \multicolumn{1}{R{2cm}}{short heading}
      & \multicolumn{1}{R{2cm}}{some long long\\ heading}
      & \multicolumn{1}{R{2cm}}{some long long\\ heading}
      & \multicolumn{1}{R{2cm}}{short heading}
      & \multicolumn{1}{R{2cm}}{some long long\\ heading}
      & \multicolumn{1}{R{2cm}}{short heading}
      & \multicolumn{1}{R{2cm}}{some long long\\ heading}
      & \multicolumn{1}{R{2cm}}{short heading}
      & \multicolumn{1}{R{2cm}}{some long long\\ heading}
      & \multicolumn{1}{R{2cm}}{short heading}
      & \multicolumn{1}{R{2cm}}{short heading}
      & \multicolumn{1}{R{2cm}}{short heading}
      & \multicolumn{1}{R{2cm}}{short heading} \\
    \cmidrule(r){1-1} \cmidrule(rl){2-2} \cmidrule(rl){3-3} \cmidrule{4-4} \cmidrule(rl){5-5}
       \cmidrule(rl){6-6} \cmidrule{7-7} \cmidrule(rl){8-8} \cmidrule(rl){9-9} \cmidrule{10-10}
       \cmidrule(rl){11-11} \cmidrule{12-12} \cmidrule(rl){13-13} \cmidrule{14-14}
      \cmidrule(rl){15-15} \cmidrule(r{1pt}){16-16} \cmidrule(l{1pt}r{1pt}){17-17}
      \cmidrule(l{1pt}r{1pt}){17-17}\cmidrule(l{1pt}r{1pt}){18-18}\cmidrule(l{1pt}){19-19}  
    Test1 & 138 & 8 & 0 & 0 & 60 & 4 & 61 & 4 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 16 & 0 \\  
    Test2 & 162 & 136 & 0 & 0 & 71 & 63 & 89 & 72 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 2 & 1 \\  
    Test3 & 161 & 120 & 0 & 0 & 66 & 40 & 85 & 77 & 0 & 0 & 0 & 0 & 0 & 0 & 6 & 3 & 4 & 0 \\  
    Test4 & 122 & 109 & 0 & 0 & 55 & 48 & 63 & 61 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 3 & 0 \\  
    Test5 & 126 & 3 & 1 & 0 & 45 & 1 & 36 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 43 & 1 \\  
  \bottomrule
  \end{tabular}
\end{table}

\end{document}

If none of the above serves, another option could be to rotate your the whole table using, for example, the rotating package.

5
  • Thanks. Especially the \footnotesize is very useful. But your RotHead command still has the same problem that the next heading doesn't align with the head of the previous heading, but starts where the tail of the previous heading ends
    – Bart
    Commented Apr 1, 2011 at 21:32
  • @Bart: can you please add some minimal and compilable code to your question illustrating exactly what you mean by this? Commented Apr 1, 2011 at 21:48
  • i've added an example to make it a bit clearer
    – Bart
    Commented Apr 1, 2011 at 22:42
  • @Bart: now I understand what you want to do; however, this will cause the headings to overlap and this can be confusing. A better option would be to rotate the headings 90 degrees; I've changed my example code to illustrate this approach. Commented Apr 2, 2011 at 1:18
  • 1
    Ok thanks. I'll use the 90 degrees headings instead. Is there a specific reason to use the turn environment instead of the rotatebox command?
    – Bart
    Commented Apr 2, 2011 at 10:48
0

Here is a way to construct that table with {NiceTabular} of nicematrix ( ≥ 6.22 2023-08-14) with the labels rotated by 90° as suggested by Gonzalo Medina.

\documentclass{article}
\usepackage{booktabs}  
\usepackage{nicematrix}

\begin{document}  

\begin{table}  
  \footnotesize
  \setlength\tabcolsep{3pt}
  \centering
  \begin{NiceTabular}{l*{18}{c}}  
    \toprule  
    Event & \Block{1-2}{Counts} && \Block{1-8}{Genres} &&&&&&&&
      \Block{1-8}{Genres blabla} \\  
    \cmidrule(r){1-1} \cmidrule(r){2-3} \cmidrule(r){4-11}\cmidrule(l){12-19}  
    \RowStyle{\rotate\scriptsize}
        \Block{}{some long long\\ heading} 
      & \Block{}{some long long\\ heading}
      & \Block{}{some long long\\ heading}
      & short heading
      & \Block{}{some long long\\ heading}
      & \Block{}{some long long\\ heading}
      & short heading
      & \Block{}{some long long\\ heading}
      & \Block{}{some long long\\ heading}
      & short heading
      & \Block{}{some long long\\ heading}
      & short heading
      & \Block{}{some long long\\ heading}
      & short heading
      & \Block{}{some long long\\ heading}
      & short heading
      & short heading
      & short heading
      & short heading \\
    \cmidrule(r){1-1} \cmidrule(rl){2-2} \cmidrule(rl){3-3} \cmidrule{4-4} \cmidrule(rl){5-5}
       \cmidrule(rl){6-6} \cmidrule{7-7} \cmidrule(rl){8-8} \cmidrule(rl){9-9} \cmidrule{10-10}
       \cmidrule(rl){11-11} \cmidrule{12-12} \cmidrule(rl){13-13} \cmidrule{14-14}
      \cmidrule(rl){15-15} \cmidrule(r{1pt}){16-16} \cmidrule(l{1pt}r{1pt}){17-17}
      \cmidrule(l{1pt}r{1pt}){17-17}\cmidrule(l{1pt}r{1pt}){18-18}\cmidrule(l{1pt}){19-19}  
    Test1 & 138 & 8 & 0 & 0 & 60 & 4 & 61 & 4 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 16 & 0 \\  
    Test2 & 162 & 136 & 0 & 0 & 71 & 63 & 89 & 72 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 2 & 1 \\  
    Test3 & 161 & 120 & 0 & 0 & 66 & 40 & 85 & 77 & 0 & 0 & 0 & 0 & 0 & 0 & 6 & 3 & 4 & 0 \\  
    Test4 & 122 & 109 & 0 & 0 & 55 & 48 & 63 & 61 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 3 & 0 \\  
    Test5 & 126 & 3 & 1 & 0 & 45 & 1 & 36 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 43 & 1 \\  
  \bottomrule
  \end{NiceTabular}
\end{table}

\end{document}

Output of the above code

You must log in to answer this question.

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