Rotate a table in LaTeX (sidewaystable, landscape, rotatebox)
The sidewaystable environment from the rotating package turns a table with its caption by 90 degrees and puts it on a page of its own. \rotatebox from graphicx rotates a tabular or a single cell.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{rotating}
\usepackage{booktabs}Rotate a table with its caption
Replace table with sidewaystable. The environment is a float like table, with \caption and \label, and the available width inside is the height of the text block.
\begin{sidewaystable}
\centering
\caption{Monthly rainfall in millimeters}
\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 \\
Madrid & 33 & 35 & 25 & 45 & 51 & 21 & 12 & 10 & 22 & 60 & 58 & 51 \\
\bottomrule
\end{tabular}
\end{sidewaystable}Turn whole pages to landscape
The landscape environment from pdflscape starts a new page and rotates everything inside it, also a longtable that runs over several pages. Header and page number stay where they are on portrait pages. The PDF viewer shows these pages turned, so the table is readable on screen.
Complete document:
\documentclass{article}
\usepackage{pdflscape}
\usepackage{booktabs}
\begin{document}
Text on a portrait page.
\begin{landscape}
\begin{table}
\centering
\caption{Monthly rainfall in millimeters}
\begin{tabular}{lrrrrrr}
\toprule
City & Jan & Feb & Mar & Apr & May & Jun \\
\midrule
Bergen & 190 & 152 & 170 & 114 & 106 & 132 \\
\bottomrule
\end{tabular}
\end{table}
\end{landscape}
\end{document}Rotate only the tabular
\rotatebox{90}{...} turns its content by 90 degrees counterclockwise. The caption outside the box stays horizontal, and the table stays on a normal page between the text.
\begin{table}
\centering
\rotatebox{90}{%
\begin{tabular}{lr}
\toprule
Item & Price \\
\midrule
Apple & 0.50 \\
Banana & 0.25 \\
\bottomrule
\end{tabular}}
\caption{Fruit prices}
\end{table}Rotate the column headers
Long headers above narrow columns take less width when each header cell is rotated. The angle is free; 90 gives vertical text.
\begin{tabular}{lccc}
\toprule
Name & \rotatebox{90}{Attendance} & \rotatebox{90}{Homework} & \rotatebox{90}{Final exam} \\
\midrule
Anna & yes & yes & no \\
Ben & yes & no & yes \\
\bottomrule
\end{tabular}Pitfalls
- A
sidewaystablealways takes a whole page, also when the table is small. - In a
twosidedocument,rotatingturns the table in opposite directions on left and right pages and needs a second compile run to know the page. The package optionsfiguresrightandfiguresleftfix one direction.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
rotating - Package
pdflscape - Package
graphicx - How-to Rotate a single page to landscape in LaTeX
- How-to Fit a table that is too wide in LaTeX
- Package
booktabs