1

I'm writing a table that should be centered but it is not, and instead of fitting in the page it is cut by the page.

Here's the code:

\begin{table}[h]
    \centering
    \begin{tabular}{|l|c|c|c|c|c|c|c|c|}
    \hline
    \textbf{benchmark} & \textbf{\#input} & \textbf{\#output} & \textbf{SOP} & \textbf{BCLP-1} & \textbf{BCLP-2} & \textbf{BCLP-3} & \textbf{BCLP-4} & \textbf{BCLP-5}\\
    \hline
    accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
    alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\

and here it is the end result: enter image description here

How can I make the table fit into the page, and appear at the center?

EDIT: the documentclass is \documentclass[12pt]{article}

EDIT2: if there is not an easy solution, can I at least rotate the table by 90° and make it fit horizontally?

  • Your table is too wide. Please provide a complete document that starts with \documentclass and ends with \end{document} and produces this table such that others can help you. Answers will depend on the layout of your document. – Schrödinger's cat Sep 22 at 1:14
  • @Schrödinger'scat the column that is cut is the last one. Is there a way to force the table to the left until is centered despite of the documentclass? – Gerardo Zinno Sep 22 at 1:23
  • Yes, \centerline{ \begin{tabular}{|l|c|c|c|c|c|c|c|c|} ... \end{tabular}} but you really don't want to go there. Instead rotate the elements of the first row. – Schrödinger's cat Sep 22 at 1:24
  • Do you know how can I rotate it and put it horizontally? At least it should fit that way – Gerardo Zinno Sep 22 at 1:27
  • Yes, load \usepackage{graphicx} and use \rotatebox. A more detailed reply requires a complete document to test that what one has done is sufficient. – Schrödinger's cat Sep 22 at 1:44
1

It is possible to squeeze the table inside the standard margins, if you want. I have reduced tabcolsep and removed vertical rules. Since the table was too wide due to the headers, I added the common part of the header on a line for itself, and reduced the fontsize. I assumed this did not change the meaning, but if I am wrong, this is not the path to go. If possible for you, you could also abbreviate #input and #output to #in and #out

The five last columns, I typeset with fixed width, using array and w-columns.

I have made three examples, one with vertical rules, and one without and one with the headings you have in your table, except that I have removed the bold and reduced the font size to reduce the size of the header:

Example 1: booktabs version

In addition I did the usual thing to make the table readable: boooktabs rules, grouping the rows by small white space, removing the left and right side bearings (@{}).

If your table has a caption, I suggest using threeparttable, which automatically ensure that table and caption is of same with, if the caption is multiline. caption will help you formatting the captions. And column with only numbers should be right aligned (col 2 to col 6):

enter image description here

\documentclass[12pt]{article}
\usepackage{array, booktabs, caption, threeparttable}
\setlength{\tabcolsep}{0.5em}
\begin{document}

\begin{table}
\centering
\begin{threeparttable}
\caption{A table is much nicer if you use booktabs and remove vertical rules. threepartable ensure that caption and table is of the same length}
\begin{tabular}{@{}lrrr*{2}{wr{\dimexpr(1.15cm-(2\tabcolsep))}}*{3}{wc{\dimexpr(1.15cm-(2\tabcolsep))}}}
\toprule
&&&&\multicolumn{5}{>{\centering\arraybackslash}p{5.71cm}}{\small\bfseries BCLP}\\ \cmidrule(l{\tabcolsep}){5-9}
\small\textbf{Benchmark} & \small\textbf{\#input} & \small\textbf{\#output} & \small\textbf{SOP} & \multicolumn{1}{c}{\small\bfseries 1} & \multicolumn{1}{c}{\small\bfseries 2} & \multicolumn{1}{c}{\small\bfseries 3} & \multicolumn{1}{c}{\small\bfseries 4} & \multicolumn{1}{c}{\small\bfseries 5}\\
\midrule
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\\addlinespace
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\\addlinespace
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
\bottomrule    
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}

Example 2: vertical rules

In this example, I had to decrease the tabcolsep further to 0.411em, because the vertical rules need the side bearings. I also added 2 pt to the rows height to avoid the collision between horizontal rules and the content of he cells.

enter image description here

\documentclass[12pt]{article}
\usepackage{array}
\begin{document}

\begin{table}
\setlength{\extrarowheight}{2pt}
\setlength{\tabcolsep}{0.413em}
\centering
\begin{tabular}{|l|*{5}{r|}*{3}{c|}}
\hline
&&&&\multicolumn{5}{>{\centering\arraybackslash}p{5cm}|}{\small\bfseries BCLP}\\ \cline{5-9}
\small\textbf{Benchmark} & \small\textbf{\#input} & \small\textbf{\#output} & \small\textbf{SOP} & \multicolumn{1}{c|}{\small\bfseries 1} & \multicolumn{1}{c|}{\small\bfseries 2} & \multicolumn{1}{c|}{\small\bfseries 3} & \multicolumn{1}{c|}{\small\bfseries 4} & \multicolumn{1}{c|}{\small\bfseries 5}\\
\hline
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\[1ex]
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\[1ex]
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
\hline
\end{tabular}
\end{table}
\end{document}

Example 3: With original table headings

tabolsep is further decreased. Fontsize in header smaller.

enter image description here

\documentclass[12pt]{article}
\usepackage{array}
\begin{document}

\begin{table}
\setlength{\extrarowheight}{2pt}
\setlength{\tabcolsep}{0.3em}
\centering
\rule{\linewidth}{2pt}
\bigskip
\begin{tabular}{|l|*{5}{r|}*{3}{c|}}
\hline
\footnotesize  Benchmark & \footnotesize \#input & \footnotesize \#output & \footnotesize SOP & \multicolumn{1}{c|}{\footnotesize BCLP-1} & \multicolumn{1}{c|}{\footnotesize BCLP-2} & \multicolumn{1}{c|}{\footnotesize BCLP-3} & \multicolumn{1}{c|}{\footnotesize BCLP-4} & \multicolumn{1}{c|}{\footnotesize BCLP-5}\\
\hline
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\[1ex]
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\[1ex]
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
alu4.pla & 14 & 8 & 575 & 923  & 921 & XXX & YYY & ZZZ\\
accpla.pla & 50 & 69 & 175 &  1457  & 1458 & XXX & YYY & ZZZ\\
\hline
\end{tabular}
\end{table}
\end{document}
0

I've solved the problem by rotating the table. In order to do so I've replaced

\begin{table}...\end{table}

with

\begin{sidewaystable}...\end{sidewaystable} 

from the package rotating

If someone has an answer to say how can I shrink to fit the table without rotating please say it here. It would still be the solution I prefer.

  • The standard solution consists in reducing the value of \tabcolsep (6pt by default) and/or reducing the font size (not too much, so it remains readable – in my opinion, \footnotesize is the lower limit). – Bernard Sep 22 at 11:15
  • @GerardoZinno I think I solved the problem without rotating the table. – Sveinung Sep 22 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.