Rotated table exceeds width of text block

I have a table (code in the final) that is rotated 90 degrees counterclockwise. However, it is too wide. How can I solve that (fit to the width of (rotated) text block)? One solution could be to make the first 2 columns, multi-line columns but I'm not being succeeded in doing do.

\begin{sidewaystable}
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{ccccccccccccccccc}
\hline
\multicolumn{1}{|c|}{\multirow{3}{*}{Algorithm}}         & \multicolumn{1}{c|}{\multirow{3}{*}{Prediction Method}} & \multicolumn{15}{c|}{PH (min)}                                                                                                                                                                                                                                                                                                                                                                                                                                \\ \cline{3-17}
\multicolumn{1}{|c|}{}                                   & \multicolumn{1}{c|}{}                                   & \multicolumn{3}{c|}{10}                                                                & \multicolumn{3}{c|}{20}                                                                 & \multicolumn{3}{c|}{30}                                                                 & \multicolumn{3}{c|}{45}                                                                 & \multicolumn{3}{c|}{60}                                                                \\ \cline{3-17}
\multicolumn{1}{|c|}{}                                   & \multicolumn{1}{c|}{}                                   & \multicolumn{1}{c|}{W}  & \multicolumn{1}{c|}{RMSE} & \multicolumn{1}{c|}{$R_{adj}^2$} & \multicolumn{1}{c|}{W}  & \multicolumn{1}{c|}{RMSE}  & \multicolumn{1}{c|}{$R_{adj}^2$} & \multicolumn{1}{c|}{W}  & \multicolumn{1}{c|}{RMSE}  & \multicolumn{1}{c|}{$R_{adj}^2$} & \multicolumn{1}{c|}{W}  & \multicolumn{1}{c|}{RMSE}  & \multicolumn{1}{c|}{$R_{adj}^2$} & \multicolumn{1}{c|}{W}  & \multicolumn{1}{c|}{RMSE}  & \multicolumn{1}{c|}{Adj.~$r^2$} \\ \hline
\multicolumn{1}{|c|}{Naive}                              & \multicolumn{1}{c|}{-}                                  & \multicolumn{1}{c|}{49} & \multicolumn{1}{c|}{7.43} & \multicolumn{1}{c|}{0.90}        & \multicolumn{1}{c|}{49} & \multicolumn{1}{c|}{13.40} & \multicolumn{1}{c|}{0.69}        & \multicolumn{1}{c|}{49} & \multicolumn{1}{c|}{17.82} & \multicolumn{1}{c|}{0.45}        & \multicolumn{1}{c|}{49} & \multicolumn{1}{c|}{21.53} & \multicolumn{1}{c|}{0.16}        & \multicolumn{1}{c|}{49} & \multicolumn{1}{c|}{22.75} & \multicolumn{1}{c|}{0.00}       \\ \hline
\multicolumn{1}{|c|}{\multirow{2}{*}{Linear Regression}} & \multicolumn{1}{c|}{Direct}                             & \multicolumn{1}{c|}{34} & \multicolumn{1}{c|}{5.10} & \multicolumn{1}{c|}{0.96}        & \multicolumn{1}{c|}{19} & \multicolumn{1}{c|}{11.03} & \multicolumn{1}{c|}{0.82}        & \multicolumn{1}{c|}{19} & \multicolumn{1}{c|}{16.08} & \multicolumn{1}{c|}{0.62}        & \multicolumn{1}{c|}{16} & \multicolumn{1}{c|}{20.87} & \multicolumn{1}{c|}{0.36}        & \multicolumn{1}{c|}{40} & \multicolumn{1}{c|}{21.92} & \multicolumn{1}{c|}{0.16}       \\ \cline{2-17}
\multicolumn{1}{|c|}{}                                   & \multicolumn{1}{c|}{Iterative}                          & \multicolumn{1}{c|}{34} & \multicolumn{1}{c|}{5.10} & \multicolumn{1}{c|}{0.96}        & \multicolumn{1}{c|}{19} & \multicolumn{1}{c|}{11.03} & \multicolumn{1}{c|}{0.82}        & \multicolumn{1}{c|}{19} & \multicolumn{1}{c|}{16.00} & \multicolumn{1}{c|}{0.62}        & \multicolumn{1}{c|}{37} & \multicolumn{1}{c|}{20.71} & \multicolumn{1}{c|}{0.30}        & \multicolumn{1}{c|}{43} & \multicolumn{1}{c|}{21.58} & \multicolumn{1}{c|}{0.16}       \\ \hline
\multicolumn{1}{|c|}{}                                   & \multicolumn{1}{c|}{}                                   & \multicolumn{1}{c|}{}   & \multicolumn{1}{c|}{}     & \multicolumn{1}{c|}{}            & \multicolumn{1}{c|}{}   & \multicolumn{1}{c|}{}      & \multicolumn{1}{c|}{}            & \multicolumn{1}{c|}{}   & \multicolumn{1}{c|}{}      & \multicolumn{1}{c|}{}            & \multicolumn{1}{c|}{}   & \multicolumn{1}{c|}{}      & \multicolumn{1}{c|}{}            & \multicolumn{1}{c|}{}   & \multicolumn{1}{c|}{}      & \multicolumn{1}{c|}{}           \\ \hline
&                                                         &                         &                           &                                  &                         &                            &                                  &                         &                            &                                  &                         &                            &                                  &                         &                            &                                 \\
\end{tabular}
\end{sidewaystable}


EDIT1: My preamble is:

\usepackage[usenames,dvipsnames,table]{xcolor}

\usepackage[square, numbers, comma, sort&compress]{natbib} % Use the natbib reference package - read up on this to edit the reference style; if you want text (e.g. Smith et al., 2012) for the in-text references (instead of numbers), remove 'numbers'
\hypersetup{urlcolor=blue, colorlinks=true} % Colors hyperlinks in blue - change to black if annoying
\title{\ttitle} % Defines the thesis title - don't touch this

%\usepackage[disable]{todonotes}
\usepackage[draft]{todonotes}

\usepackage[bottom]{footmisc}
\usepackage[export]{adjustbox}

\usepackage{multirow}
\usepackage{pgfgantt}
\usepackage{fixltx2e}
\usepackage{cals}
\usepackage{rotating}
\usepackage{array}

\usepackage[showframe, nomarginpar]{geometry}
\usepackage{graphics}
\usepackage{amsmath}
\usepackage{caption, makecell}
\setcellgapes{4pt}

• Please can you make your code so we can compile it? – cfr Jun 28 '15 at 17:09

3 Answers

You can make some column heads two-lined, with the makecell package. I took the opportunity to give some vertical padding to the table, and simplified your code, keeping only the necessary \multicolumns. Loading the caption package makes the vertical (?) spacing between caption and table more sensible.

\documentclass[a4paper, twoside]{Thesis}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{makecell}
\setcellgapes{4pt}
\usepackage[usenames,dvipsnames,table]{xcolor}

\usepackage[square, numbers, comma, sort&compress]{natbib} % Use the natbib reference package - read up on this to edit the reference style; if you want text (e.g. Smith et al., 2012) for the in-text references (instead of numbers), remove 'numbers'
\title{\ttitle} % Defines the thesis title - don't touch this

%\usepackage[disable]{todonotes}
\usepackage[draft]{todonotes}

\usepackage[bottom]{footmisc}
\usepackage[export]{adjustbox}

\usepackage{multirow}
%\usepackage{pgfgantt}
%\usepackage{cals}
\usepackage{rotating}
\usepackage{array}

\usepackage[showframe, nomarginpar]{geometry}
\usepackage{graphics}%graphicx
\usepackage{amsmath}
\usepackage{caption, makecell}
\setcellgapes{4pt}
\usepackage{hyperref}
\hypersetup{urlcolor=blue, colorlinks=true} % Colors hyperlinks in blue - change to black if annoying

\begin{document}

\begin{sidewaystable}
\centering \makegapedcells%
\setlength\tabcolsep{5pt}
\caption{My caption}
\label{my-label}
\begin{tabular}{|*{17}{c|}}
\hline
\multirowcell{3}[-1.5ex]{Algorithm} & \multirowcell{3}[-1.5ex]{Prediction\\ Method} & \multicolumn{15}{c|}{PH (min)} \\
\cline{3-17}
\multicolumn{1}{|c|}{}& \multicolumn{1}{c|}{} & \multicolumn{3}{c|}{10} & \multicolumn{3}{c|}{20} & \multicolumn{3}{c|}{30} & \multicolumn{3}{c|}{45} & \multicolumn{3}{c|}{60} \\
\cline{3-17}
\multicolumn{1}{|c|}{} & \multicolumn{1}{c|}{} & W & RMSE & $R_\text{adj}^2$ & W & RMSE & $R_\text{adj}^2$ & W & RMSE & $R_\text{adj}^2$ & W & RMSE & $R_\text{adj}^2$ & W & RMSE & Adj.~$r^2$ \\
\hline
\multicolumn{1}{|c|}{Naive} & --- & 49 & 7.43 & 0.90 & 49 & 13.40 & 0.69 & 49 & 17.82 & 0.45 & 49 & 21.53 & 0.16 & 49 & 22.75 & 0.00 \\
\hline
\multicolumn{1}{|c|}{\multirowcell{2}{Linear \\ Regression}} & Direct & 34 & 5.10 & 0.96 & 19 & 11.03 & 0.82 & 19 & 16.08 & 0.62 & 16 & 20.87 & 0.36 & 40 & 21.92 & 0.16 \\
\cline{2-17}
\multicolumn{1}{|c|}{} & Iterative & 34 & 5.10 & 0.96 & 19 & 11.03 & 0.82 & 19 & 16.00 & 0.62 & 37 & 20.71 & 0.30 & 43 & 21.58 & 0.16 \\
\hline
\multicolumn{1}{|c|}{\multirowcell{2}{SVR}} & Direct & & & & & & & & & & & & & & & \\
\cline{2-17}
\multicolumn{1}{|c|}{} & Iterative & & & & & & & & & & & & & & & \\
\hline
\end{tabular}
\end{sidewaystable}

\end{document}


• There is something wrong with the code and I can't understand what it is (I'm still a noob on this). – user25847 Jun 28 '15 at 18:03
• What do you mean? You can't compile? Or it doesn't correspond to what you have in mind? – Bernard Jun 28 '15 at 18:21
• @Bernard It doesn't compile. I've corrected what I think was the mistake. Could you check that it is what you meant? My output looks like the picture you posted. – cfr Jun 28 '15 at 18:22
• From what I understand from your code it seems that you put the other word (for example, Regression) on the cell bellow Linear. Is it correct? If so, I think that it won't work for me as I'll have an cell on column A with just a small word that groups 2 cells on column B (like the linear regression row). Maybe what I need is to merge the 2 cells on column A. What do you think? – user25847 Jun 28 '15 at 18:22
• Your correction is fine. Thank you. It was a problem with my editor at the time I copied the code to post it. For the placement of regression, it is not exactly on cell below linear: it's the makecell version of \multirow, which allows for line breaks. I put this multirow on the second row because I thought on first row, there was too much blank space . But if you have some text to put, it's different. Could you post a sample of what you intend to put? I'm not sure I understand very well. – Bernard Jun 28 '15 at 18:42

Here's a version using booktabs which follows at least some (though not all) of the recommendations it makes. This does not exceed the width (\textheight) available.

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{rotating,multirow,booktabs,tabularx}
\begin{document}
\begin{sidewaystable}
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{*{17}{c}}
\toprule
\multirow{3}{*}{Algorithm}  & \multirow{3}{.075\textwidth}{\centering Prediction\newline Method} & \multicolumn{15}{c}{PH (min)}                                                                                                                                                                                                                                                                                                                                                                                                                                \\\cmidrule{3-17}
& & & 10& & &20& & &30& & &45& & &60&\\ \cmidrule{3-17}
& & W  & RMSE & $R_{adj}^2$ & W  & RMSE  & $R_{adj}^2$ & W  & RMSE  & $R_{adj}^2$ & W  & RMSE  & $R_{adj}^2$ & W  & RMSE  & Adj.\thinspace $r^2$ \\ \midrule
Naive  &  & 49 & 7.43 & 0.90 & 49 & 13.40 & 0.69 & 49 & 17.82 & 0.45 & 49 & 21.53 & 0.16  & 49 & 22.75 & 0.00       \\\cmidrule{1-17}
\multirow{2}{.0775\textwidth}{\centering Linear\newline Regression} & Direct & 34 & 5.10 & 0.96 & 19 & 11.03 & 0.82  & 19 & 16.08 & 0.62  & 16 & 20.87 & 0.36  & 40 & 21.92 & 0.16       \\ \cmidrule{2-17}
& Iterative                          & 34 & 5.10 & 0.96        & 19 & 11.03 & 0.82        & 19 & 16.00 & 0.62        & 37 & 20.71 & 0.30        & 43 & 21.58 & 0.16       \\\cmidrule{1-17}
&                                    &    &      &             &    &       &             &    &       &             &    &       &             &    &       &            \\\cmidrule{1-17}
&                                                         &                         &                           &                                  &                         &                            &                                  &                         &                            &                                  &                         &                            &                                  &                         &                            &                                 \\\bottomrule
\end{tabular}
\end{sidewaystable}
\end{document}


• Could you put all the lines around the cells? Thanks. Normally I would accept exactly as you have but I have to present this and they don't want the table like that. – user25847 Jun 28 '15 at 19:06
• I mean put also the vertical lines ... – user25847 Jun 28 '15 at 19:36
• @user25847 Then you should use Bernard's solution, I think. booktabs doesn't work well with vertical lines. It is ideologically opposed to them. – cfr Jun 28 '15 at 19:39
• @user25847 The thing is that reverting to a standard layout and adding the vertical rules back in makes my solution exceed the available space. I immediately get overfull boxes again. Since I'd already trimmed stuff to make it fit, this doesn't leave many options. You can reduce the horizontal spacing between columns but the table will be even less readable (the loss of vertical spacing already makes it somewhat less readable) or you can reduce the font size which will also make it less readable. – cfr Jun 28 '15 at 19:46

A little bit late to the game, but hopefully still useful.

Note that all \multicolumn{1}{c|}{...} wrappers are gone.

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[nomarginpar]{geometry}
\usepackage{amsmath}
\usepackage{rotating,caption,booktabs}
%% macro for a "small", i.e., compressed table
\newcommand\smtab[1]{\smash[b]{\begin{tabular}[t]{@{}l@{}}#1\end{tabular}}}

\begin{document}
\begin{sidewaystable}
\caption{My caption}
\label{my-label}
\centering
\begin{tabular}{@{} ll *{15}{c} @{}}
\toprule
Algorithm &
\smtab{Prediction\\ Method} & \multicolumn{15}{c}{PH (min)} \\
\cmidrule(l){3-17}
&  & \multicolumn{3}{c}{10}
& \multicolumn{3}{c}{20}
& \multicolumn{3}{c}{30}
& \multicolumn{3}{c}{45}
& \multicolumn{3}{c}{60} \\
\cmidrule(lr){3-5} \cmidrule(lr){6-8} \cmidrule(lr){9-11} \cmidrule(lr){12-14} \cmidrule(l){15-17}
& & W & RMSE & $R_{\textit{adj}}^2$
& W & RMSE & $R_{\textit{adj}}^2$
& W & RMSE & $R_{\textit{adj}}^2$
& W & RMSE & $R_{\textit{adj}}^2$
& W & RMSE & $R_{\textit{adj}}^2$ \\
\midrule
Naive & -- & 49 & 7.43 & 0.90 & 49 & 13.40 & 0.69 & 49 & 17.82 & 0.45        & 49 & 21.53 & 0.16        & 49 & 22.75 & 0.00       \\ \addlinespace
Lin.\ Regr. & Direct                             & 34 & 5.10 & 0.96        & 19 & 11.03 & 0.82        & 19 & 16.08 & 0.62        & 16 & 20.87 & 0.36        & 40 & 21.92 & 0.16       \\
& Iterative                          & 34 & 5.10 & 0.96        & 19 & 11.03 & 0.82        & 19 & 16.00 & 0.62        & 37 & 20.71 & 0.30        & 43 & 21.58 & 0.16       \\
\\
\\
\bottomrule
\end{tabular}
\end{sidewaystable}

\end{document}