# Cannot space table headers properly

I am unable to space my table headers as expected in LaTeX.

My table is supposed to look like this:

But unfortunately it is showing up in the document like this:

Where it is not fitting the page's width, and the header's labels, such as "Number of recommended items" is taking over a single line on the table thus taking a lot of space. Part of the solution would be increasing the header's height.

I want to reduce as much whitespace as possible on the table to make it legible and fit the page.

Here is the code:

%Please add the following packages if necessary:
%\usepackage{booktabs, multirow} % for borders and merged ranges
%\usepackage{soul}% for underlines
%\usepackage[table]{xcolor} % for cell colors
%\usepackage{changepage,threeparttable} % for wide tables
%If the table is too wide, replace \begin{table}[!htp]...\end{table} with
\begin{table}[!htp]\centering
\scriptsize
\begin{tabular}{lrrrrrrrr}\toprule
Execution environment &Number of recommended items &Dataset size &Total API endpoint execution time (seconds) &Endpoint execution latency &Recommendation execution &Data processing time (seconds) &Recommender algorithm execution time (seconds) \\\midrule
Local (API) &3 &20000263 &50.05549 &0.01421 &50.04128 &46.52477 &3.51651 \\
Local (Algorithm only) &3 &20000263 & & &53.21693 &48.71163 &4.50530 \\
Heroku (API) &3 &20000263 &MEMORY OVERFLOW & & & & \\
PAW (API) &3 &20000263 &MEMORY OVERFLOW & & & & \\
Local (API) &5 &20000263 &48.26075 &0.00957 &48.25118 &44.64684 &3.60434 \\
Local (Algorithm only) &5 &20000263 & & &64.97844 &60.10595 &4.87248 \\
Heroku (API) &5 &20000263 &MEMORY OVERFLOW & & & & \\
PAW (API) &5 &20000263 &MEMORY OVERFLOW & & & & \\
Local (API) &10 &20000263 &43.31767 &0.01661 &43.30106 &39.85879 &3.44227 \\
Local (Algorithm only) &10 &20000263 & & &44.16319 &40.60699 &3.55620 \\
Heroku (API) &10 &20000263 &MEMORY OVERFLOW & & & & \\
PAW (API) &10 &20000263 &MEMORY OVERFLOW & & & & \\
\multicolumn{1}{c}{AVERAGE} &45.78921 &0.01309 &50.78216 &46.78604 &3.99612 \\
& &STDV &3.4952824449691047 &0.00497738403016791 &8.855024886265406 &8.2442634610736 &0.6482773532436392 \\
\bottomrule
\end{tabular}
\end{table}


Thank you

• You cannot have break lines in l, r, and c columns, but you can do it with p columns (e.g., p{5cm}) and with the tabulary package to use L,C, R,J columns and tabularx package to use X columns. There are a lot of examples of use if these packages in this site. See also thee manual of these packages and package array (run texdoc array). Said that, nothing can fix a very bad table design except redesign the table (rounded numbers, shoter headers, traspose row by columns, etc..) .
– Fran
Aug 8 at 4:35
• On the other hand, the fix depend largely of the page dimensions and margins, so post a mininal but complete (compilable) example, not a chunk of code where is impossible reproduce your problem properly.
– Fran
Aug 8 at 4:38
• Welcome to TeX.SE! Aug 8 at 7:22
• The makecell package allows manual line breaks in standard columns with it \thead and \makecell commands. Aug 8 at 8:46

You can fit the table even in the standard LaTeX layout but you would have to get rid of those wordy headings and long numbers. For instance, I substituted headings with some symbols and added explanation of what each of the symbols represents at the bottom of the table. I don't think you need 15 decimal places. Finally, data size seems to be the same for each execution environment and I'd move it to somewhere else (annotation or title) to avoid repetition.

One way to put everything together is to use threeparttable. It can help with splitting a table environment into three parts: caption, the table itself and annotation. siunitx rounds long numbers to only a few decimal places; you can still keep the numbers in the table and work out the best option for you.

\documentclass{article}
\usepackage{mathtools}
\usepackage{threeparttable}
\usepackage{booktabs}
\usepackage{siunitx}

\sisetup{
round-mode = places,
round-precision = 2,
minimum-decimal-digits = 1,
}

\begin{document}
\begin{table}[!htp]\centering
\small
\begin{threeparttable}
\label{tab: }
\begin{tabular}{
l
r
S[table-format=2.1, round-precision = 1]
S[table-format=1.3, round-precision = 4]
*2{S[table-format=2.1, round-precision = 1]}
S[table-format=1.2, round-precision = 2]
}
\toprule
$E$\textsubscript{ENV}\tnote{1}
& $N$\textsubscript{REC}\tnote{2}   % etc.
& {$T$\tnote{3}}
& {$L$\tnote{4}}
& {$R$\tnote{5}}
& {$P$\tnote{6}}
& {$A$\tnote{7}} \\
\midrule
Local (API)            &  3 & 50.05549 &  0.01421 & 50.04128 & 46.52477 & 3.51651 \\
Local (Algorithm only) &  3 &     {--} &     {--} & 53.21693 & 48.71163 & 4.50530 \\
Heroku (API)           &  3 &     {MO} &     {--} &     {--} &     {--} &    {--} \\
PAW (API)              &  3 &     {MO} &     {--} &     {--} &     {--} &    {--} \\
Local (API)            &  5 & 48.26075 &  0.00957 & 48.25118 & 44.64684 & 3.60434 \\
Local (Algorithm only) &  5 &     {--} &     {--} & 64.97844 & 60.10595 & 4.87248 \\
Heroku (API)           &  5 &     {MO} &     {--} &     {--} &     {--} &    {--} \\
PAW (API)              &  5 &     {MO} &     {--} &     {--} &     {--} &    {--} \\
Local (API)            & 10 & 43.31767 &  0.01661 & 43.30106 & 39.85879 & 3.44227 \\
Local (Algorithm only) & 10 &     {--} &     {--} & 44.16319 & 40.60699 & 3.55620 \\
Heroku (API)           & 10 &     {MO} &     {--} &     {--} &     {--} &    {--} \\
PAW (API)              & 10 &     {MO} &     {--} &     {--} &     {--} &    {--} \\
\midrule[\cmidrulewidth]
\multicolumn{2}{r}{AVERAGE} & 45.78921 & 0.01309  & 50.78216 & 46.78604 &  3.99612 \\
\multicolumn{2}{r}{STDV} &  3.4952824449691047 &  0.00497738403016791 &  8.855024886265406 & 8.2442634610736 & 0.6482773532436392 \\
\bottomrule
\end{tabular}
MO: Memory Overflow\par
\medskip
\tnote{3}~Total API endpoint execution time (seconds),\quad%
\tnote{7}~Recommender algorithm execution time (seconds)
\end{threeparttable}
\end{table}

\end{document}

• a nice answer by @Celdor Aug 8 at 10:59

Welcome, I have a a start for you

\documentclass[12pt, A4]{article}
\usepackage[utf8]{inputenc}
\usepackage{fourier}
\usepackage{array}
\usepackage{makecell}

\renewcommand\cellgape{\Gape[4pt]}

\begin{document}

\setlength\extrarowheight{3pt}

\begin{table}[ht]
\centering
\small

\begin{tabular}{|c|clc|c|c|c|c|c|}
\hline
Local (API) &3 &20000263 &50.05549 &0.01421 &50.04128 &46.52477  &3.51651 \\
Local (Algorithm only) &3 &20000263 & & &53.21693 &48.71163 &4.50530  \\
Heroku (API) &3 &20000263 &MEMORY OVERFLOW & & & & \\
PAW (API) &3 &20000263 &MEMORY OVERFLOW & & & & \\
Local (API) &5 &20000263 &48.26075 &0.00957 &48.25118 &44.64684  &3.60434 \\
Local (Algorithm only) &5 &20000263 & & &64.97844 &60.10595 &4.87248  \\
Heroku (API) &5 &20000263 &MEMORY OVERFLOW & & & & \\
PAW (API) &5 &20000263 &MEMORY OVERFLOW & & & & \\
Local (API) &10 &20000263 &43.31767 &0.01661 &43.30106 &39.85879  &3.44227 \\
Local (Algorithm only) &10 &20000263 & & &44.16319 &40.60699 &3.55620  \\
Heroku (API) &10 &20000263 &MEMORY OVERFLOW & & & & \\
PAW (API) &10 &20000263 &MEMORY OVERFLOW & & & & \\
\multicolumn{1}{c}{AVERAGE} &45.78921 &0.01309 &50.78216 &46.78604  &3.99612 \\
& &STDV &3.4952824449691047 &0.00497738403016791 &8.855024886265406  &8.2442634610736 &0.6482773532436392 \\
\hline
\end{tabular}