How to Generate and Scale a LaTeX Table
At a glance
- 1. Choose the columns
Write one column specification for each column, such as l for left-aligned text or c for centered values.
- 2. Enter the rows
Separate cells with ampersands and end each row with two backslashes.
- 3. Add clear rules
Use booktabs commands for clean horizontal rules instead of drawing a grid around every cell.
- 4. Fit the width
Try tabularx for text that should wrap; use resizebox only when you need to scale the entire table.
To generate a table in LaTeX, use the tabular environment. Its column specification determines how many columns the table has and how their contents are aligned. Inside it, put an ampersand (&) between cells and end each row with \\.
For readable tables, the booktabs package provides horizontal rules designed for tables. If the finished table is too wide, you can either make text wrap within a set width or scale the entire table. The example below shows a complete document with a scaled table.
A complete example
\documentclass{article}
\usepackage{booktabs}
\usepackage{graphicx}
\begin{document}
\begin{table}[ht]
\centering
\caption{Measurements from three trials}
\label{tab:measurements}
\resizebox{\textwidth}{!}{%
\begin{tabular}{lrr}
\toprule
Trial & Distance (m) & Time (s) \\
\midrule
1 & 12.4 & 3.1 \\
2 & 15.0 & 3.8 \\
3 & 10.8 & 2.7 \\
\bottomrule
\end{tabular}%
}
\end{table}
Table~\ref{tab:measurements} summarizes the measurements.
\end{document}
This document needs the booktabs package for \toprule, \midrule, and \bottomrule, and graphicx for \resizebox. The lrr column specification creates three columns: the first is left-aligned, and the next two are right-aligned. The column specifier count must match the number of cells in each row.
The optional [ht] asks LaTeX to place the floating table here or near the top of a page; it does not force placement. \caption supplies a caption, and \label lets you refer to the table elsewhere in the document.
Build a table row by row
For a table with three columns, a row has this form:
Name & Score & Status \\
An ampersand separates neighboring cells. The two backslashes end the row. In a tabular environment, a line break in your source code by itself does not end a row; use \\ explicitly. A blank line inside tabular can cause an error because LaTeX does not expect a new paragraph there.
The column specification goes immediately after \begin{tabular}. Common choices are:
| Specifier | Alignment |
|---|---|
l | Left |
c | Centered |
r | Right |
p{3cm} | Fixed-width paragraph, aligned at the top |
For example, \begin{tabular}{lcr} defines three columns. Use p{3cm} when a column contains longer text that should wrap to a specified width. To include a literal ampersand in cell text, write \&.
Choose a way to fit the table
First, consider whether the table needs to be wider at all. Shorten headings, remove unnecessary columns, or adjust the column alignment. For text-heavy tables, tabularx can make a table fit a target width while allowing designated columns to wrap:
\usepackage{tabularx}
\begin{tabularx}{\textwidth}{lXr}
\toprule
Item & Description & Cost \\
\midrule
A & A longer description that wraps within the available space. & 12 \\
\bottomrule
\end{tabularx}
The X column expands or contracts to fill the available width. This is often more legible than shrinking every character. The code belongs in a document that also loads booktabs if you use its rule commands.
For a small table that is only slightly too wide, \resizebox{\textwidth}{!}{...} scales it to the text width. The ! preserves the table’s proportions while LaTeX calculates the height. In a two-column document, \textwidth is the width of both columns together; use \columnwidth when the table must fit within one column. A table spanning both columns typically needs a table* float.
Common mistakes
- Wrong number of cells: Every row should have the number of entries implied by the column specification. A missing or extra
&can produce alignment errors. - Using a single backslash:
\\ends a row. A single\is not the normal row terminator intabular. - Expecting source lines to make rows: Put
\\at the end of each row except where the table ends. - Scaling before trying wrapping:
resizeboxalso shrinks the text, which can make a table hard to read. Trytabularxor a fixed-widthp{...}column for long text. - Using
\textwidthin a narrow column: It can make a table wider than its column. Use\columnwidthwhen sizing to a single column. - Adding vertical rules everywhere: Dense grids are usually harder to scan. The
booktabsstyle uses a few horizontal rules and spacing to separate the header and body.
If your data already exists in a spreadsheet or a program, a table generator can convert it into LaTeX source. Check the generated column specification, escape special characters such as %, &, and _, and compile the result in a minimal document before adding it to a larger project.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.