Tables in LaTeX
A LaTeX table is a tabular grid, usually wrapped in a table float that gives it a number and a caption. This guide goes from the first grid to clean academic tables, merged cells, tables that fit the page width and tables that run over several pages.
A first table
\begin{tabular}{lcr}
Name & Role & Hours \\
Amira & Backend & 120 \\
Youssef & Frontend & 95 \\
\end{tabular}The argument {lcr} declares three columns: left-aligned, centred and right-aligned. & separates cells and \\ ends a row. Every row needs the same number of cells as there are columns.
Caption, number and placement
\begin{table}[htbp]
\centering
\caption{Team workload during the internship}
\label{tab:workload}
\begin{tabular}{lcr}
...
\end{tabular}
\end{table}
Table~\ref{tab:workload} shows ...tablemakes the grid a float: LaTeX numbers it and places it where it fits, which may be the top of the next page.- Table captions go above the table. Figure captions go below.
\labelcomes after\caption, or the reference gets the wrong number.[htbp]allows here, top, bottom or a page of floats. Avoid a bare[h]: LaTeX often cannot honour it and moves the table to the end.- With
\usepackage[french]{babel}the label reads "Tableau".
Columns
| Specifier | Column |
|---|---|
l, c, r | Left, centre, right. As wide as the widest cell; text never wraps. |
p{4cm} | Fixed width, text wraps inside the cell. |
X | Takes the remaining width (tabularx, below). |
S | Numbers lined up on the decimal point (siunitx). |
| | A vertical line between columns. |
Long text in an l column runs straight off the page. Give that column a p{...} width, or use tabularx.
Clean rules with booktabs
Tables in journals and good reports use three horizontal rules and no vertical lines. The booktabs package draws them with the right weights and spacing:
\usepackage{booktabs} % preamble
\begin{tabular}{lcc}
\toprule
Model & Accuracy & Time (s) \\
\midrule
Baseline & 0.81 & 12 \\
CNN & 0.86 & 31 \\
Ours & 0.89 & 9 \\
\bottomrule
\end{tabular}Do not mix booktabs rules with | columns: the lines will not meet. To rule off part of the header, use \cmidrule(lr){2-3} under columns 2 to 3. If you want a full grid instead, use | in the column list and \hline between rows, and keep the same style across the report.
Merged cells
Across columns
\begin{tabular}{lcccc}
\toprule
& \multicolumn{2}{c}{Dataset A} & \multicolumn{2}{c}{Dataset B} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
Model & Precision & Recall & Precision & Recall \\
\midrule
Ours & 0.91 & 0.88 & 0.87 & 0.84 \\
\bottomrule
\end{tabular}\multicolumn{2}{c}{...} spans two columns and centres the text. It is built in.
Down rows
\usepackage{multirow} % preamble
\begin{tabular}{llc}
\toprule
Layer & Component & Tests \\
\midrule
\multirow{2}{*}{Backend} & API & 42 \\
& Worker & 17 \\
\multirow{2}{*}{Frontend} & Editor & 23 \\
& Settings & 8 \\
\bottomrule
\end{tabular}\multirow{2}{*}{...} spans two rows at the text's natural width. Leave the cell empty in the rows it covers.
Fitting the page width
\usepackage{tabularx} % preamble
\begin{tabularx}{\textwidth}{lX}
\toprule
Requirement & Description \\
\midrule
RF1 & The user can sign in with an email address and a password, and reset
the password from a link sent by email. \\
RF2 & The administrator can export the monthly report as a PDF. \\
\bottomrule
\end{tabularx}The table is exactly \textwidth wide and each X column takes the space that is left, wrapping its text. This is the right tool for requirement lists and comparison tables with sentences in them.
Avoid shrinking a table with \resizebox: it scales the text too, often below a readable size. Try, in order: \small before the tabular, tabularx, fewer columns, or a landscape page.
Tables longer than a page
A table float cannot break across pages. For long tables use longtable on its own, without a table around it:
\usepackage{longtable} % preamble
\begin{longtable}{lll}
\caption{Test cases}\label{tab:tests} \\
\toprule
ID & Scenario & Expected result \\
\midrule
\endfirsthead
\multicolumn{3}{l}{\tablename\ \thetable{} (continued)} \\
\toprule
ID & Scenario & Expected result \\
\midrule
\endhead
\bottomrule
\endlastfoot
T1 & Valid sign-in & Dashboard opens \\
T2 & Wrong password & Error shown \\
% ... as many rows as you need
\end{longtable}Everything before \endfirsthead appears on the first page, what comes before \endhead repeats at the top of every following page, and the block before \endlastfoot closes the table. For wrapping text in a long table, use p{...} columns, or the xltabular package for X columns.
Numbers that line up
\usepackage{siunitx} % preamble
\begin{tabular}{l S[table-format=3.2]}
\toprule
Endpoint & {Latency (ms)} \\
\midrule
/login & 120.5 \\
/export & 8.25 \\
/search & 42.0 \\
\bottomrule
\end{tabular}An S column aligns numbers on the decimal point. table-format=3.2 reserves three digits before the point and two after. Wrap text cells in that column in braces, like the header.
Spacing and colour
\renewcommand{\arraystretch}{1.3} % taller rows, inside the table or in the preamble
\usepackage[table]{xcolor} % preamble
\rowcolors{2}{gray!10}{white} % light stripes from row 2Stripes help in wide tables with many rows; with booktabs rules, extra space between groups of rows (\addlinespace) usually does the job instead. More in the colours guide.
Quick reference
| You want | Use | Package |
|---|---|---|
| Caption and number | table with \caption and \label | built in |
| Clean horizontal rules | \toprule, \midrule, \bottomrule, \cmidrule | booktabs |
| Cell across columns | \multicolumn{n}{c}{...} | built in |
| Cell down rows | \multirow{n}{*}{...} | multirow |
| Wrapping text | p{4cm} or X | built in, tabularx |
| Full page width | tabularx | tabularx |
| Several pages | longtable | longtable |
| Decimal alignment | S column | siunitx |
| Row colours | \rowcolors, \rowcolor, \cellcolor | xcolor with [table] |
| Wide table on its own page | landscape | pdflscape |
Where to go next
- PGFPlots, when a chart would say it better than a table
- Page size and margins, for landscape pages
- Common LaTeX mistakes in PFE reports