Fit a table that is too wide in LaTeX (tabularx, resizebox)
A table that sticks out into the right margin has four fixes, from the mildest to the strongest: let the text wrap with tabularx, reduce font size and column padding, scale the whole table with \resizebox, or turn it sideways.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{tabularx}
\usepackage{graphicx}
\usepackage{booktabs}Let the text wrap
A table is usually too wide because long text sits in l columns, which never wrap. tabularx with the width \textwidth and X for the text columns produces a table exactly as wide as the text block.
\begin{table}
\begin{tabularx}{\textwidth}{lXX}
\toprule
Method & Advantage & Drawback \\
\midrule
Wrap & The font size stays the same as in the text. & Rows get higher. \\
Scale & The layout of the table does not change. & Small text is hard to read. \\
\bottomrule
\end{tabularx}
\end{table}Reduce font size and column padding
\small or \footnotesize inside the table environment sets the table in a smaller font. \setlength{\tabcolsep}{3pt} halves the default padding of 6 pt on each side of a column, which saves 6 pt per column.
\begin{table}
\centering
\small
\setlength{\tabcolsep}{3pt}
\begin{tabular}{lrrrrrrrrrrrr}
\toprule
City & Jan & Feb & Mar & Apr & May & Jun & Jul & Aug & Sep & Oct & Nov & Dec \\
\midrule
Bergen & 190 & 152 & 170 & 114 & 106 & 132 & 148 & 190 & 283 & 271 & 259 & 235 \\
\bottomrule
\end{tabular}
\end{table}Scale the table to the text width
\resizebox{\textwidth}{!}{...} from graphicx scales the tabular to the width of the text block; ! scales the height by the same factor. Text and rules shrink with it. \resizebox also enlarges a narrow table; \adjustbox{max width=\textwidth}{...} from the adjustbox package only shrinks a table that is too wide.
\begin{table}
\resizebox{\textwidth}{!}{%
\begin{tabular}{lrrrrrrrrrrrr}
\toprule
City & January & February & March & April & May & June & July & August & September & October & November & December \\
\midrule
Bergen & 190 & 152 & 170 & 114 & 106 & 132 & 148 & 190 & 283 & 271 & 259 & 235 \\
\bottomrule
\end{tabular}}
\caption{Monthly rainfall in millimeters}
\end{table}Turn the table sideways
A table with many columns fits better on a landscape page, where the height of the text block becomes the available width. Rotate a table in LaTeX shows sidewaystable and landscape.
Pitfalls
- LaTeX reports a table that is too wide as Overfull \hbox (263.664pt too wide) in paragraph at lines 3--6; the message says by how many points the table sticks out.
\resizeboxchanges the font size of the table. A table scaled to half its width shows 10 pt text at 5 pt.\resizeboxtakes thetabular, not thetableenvironment; a float inside a box stops with Not in outer par mode. Alongtablecannot be scaled this way either.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
tabularx - Package
graphicx - How-to Set the column width of a table in LaTeX
- How-to Rotate a table in LaTeX
- Error Overfull \hbox (263.664pt too wide) in paragraph at lines 3--6
- Package
booktabs