The five parts of a table
A published table is not one environment but a stack. The outer layer is the float: \begin{table}[⟨specs⟩], which exists only to let the page breaker move the thing around and to carry the caption (see the float lesson for placement). Inside it comes \centering, then the body — tabular, tabularx or longtable — then the caption and the label.
Order inside the float matters. For a table the caption goes above the body by convention, which the caption package can enforce per type with \captionsetup[table]{position=top}; without it, LaTeX guesses from where you typed \caption. Put \label{tab:...} after \caption, always: the label picks up the counter value that the caption just stepped, so a label placed before it refers to the section number instead and every \ref silently lies.
The third layer is the geometry switches, all of them local to the float: \small or \footnotesize as a declaration (not a command around the table, which would leave a stray space), \setlength{\tabcolsep}{4pt} to squeeze the inter-column padding, \renewcommand{\arraystretch}{1.2} to add breathing room in rows, and \setlength{\extrarowheight}{2pt} from the array package when the problem is specifically that capitals and math look glued to the rule above them.
The fourth layer is what is not a table at all: the source of the numbers. A table typed by hand is wrong within two revisions. If the values come from an analysis, generate the .tex fragment (pandas' to_latex, R's kable/booktabs back end, or \input{autotab.tex}) and keep the hand-written part to the caption and the column specification. The fifth layer is the reader's entitlement: a table with numbers needs units in the header, a stated error measure, and — if any cell needs explaining — footnotes, for which threeparttable keeps the notes the width of the table rather than the page.
Column specifiers, completely
The argument of tabular is a specification, not a list of columns to be filled: {l c r p{3cm} m{3cm} b{3cm}}. l, c and r are the three single-token alignments. p{⟨width⟩} creates a column that paragraph-breaks its content with the baseline of its first line — that is the baseline the whole row hangs on — which is why a p column next to a numeric one looks top-heavy. m{} centres the paragraph box vertically and b{} puts its last line on the row baseline; both come from the array package, so load it.
@{⟨material⟩} and !{⟨material⟩} are the two most useful and most ignored tokens. Wherever it appears, @{} deletes the inter-column space that \tabcolsep would have inserted and replaces it with your material; !{} keeps that space and inserts your material in addition. \tabcolsep is applied on both sides of every column, so a tabular carries 2n copies of it and a table that touches the margin of a minipage can be made to fit simply by writing @{} at the start and end of the specification — a saving of exactly 2\tabcolsep, commonly 12 pt.
| draws a vertical rule. *{⟨n⟩}{⟨spec⟩} repeats a fragment, so {l*{6}{r}} saves typing and stays correct when the column count changes. >{\raggedright\arraybackslash}p{4cm} (again from array) injects code before each cell and <{...} after it; wrapped in \newcolumntype, that is how you build a house style instead of repeating a five-token incantation in every table.
One trap with @{} and >{}: they replace the space and can swallow the alignment you asked for. \raggedright inside a column specification breaks the \\ that ends a row's last cell unless you put \arraybackslash after it, so {>{\raggedright\arraybackslash}p{4cm}} is the correct form and {>{\raggedright}p{4cm}} is the version that fails with a confusing error at the end of the row.
W = \sum_{k=1}^{n} w_{k} + 2\,n\,c + m\,r \qquad (c=\text{column padding},\; r=\text{rule width})
c is \tabcolsep (6 pt by default) and w_k is the natural width of column k; m counts the vertical rules at \arrayrulewidth each.booktabs: three rules, and the reasons
booktabs replaces the primitive \hline with three rules of different weight and, more importantly, with rules that carry the right spacing: \toprule at the head, \midrule under the header, \bottomrule at the foot, plus \cmidrule{⟨i-j⟩} for a rule that spans only some columns and \addlinespace[⟨len⟩] for a visual gap where a rule would lie. The package is small, required in every distribution, and its manual is three pages; it is the single highest-yield typography package in the language.
The rules are spaced differently from \hline on purpose: \toprule and \bottomrule are 0.08em thick and add \aboverulesep/\belowrulesep around themselves, while \midrule is thinner. That asymmetry is what makes the header read as a header. It also means booktabs rules and \hline must not be mixed in one table: the spacing disagrees by a couple of points and the result looks subtly wrong without anything in the log saying so.
Two prohibitions are stated flatly in the manual and both are load-bearing. Never use vertical rules. A vertical rule forces the eye to read down a column and compare glyphs it was never designed to be compared; it also cannot stop at a cell boundary, so it cuts through the descenders of a p column and looks broken. And never use two or three horizontal rules (the classic \hline\hline double line): the inter-line gap becomes a hairline artefact whose size depends on the resolution of the output device, which is exactly the thing print was designed not to show. If a line must be emphasised, \heavyrulewidth is the knob, not a second rule.
Trimming partial rules is the detail that separates a typeset table from a converted one: \cmidrule(l){2-3} or \cmidrule(r){1-2} shortens the rule at that end so it does not run into a neighbouring column, and \morecmidrules stacks a second line where a two-level header really needs it. Keep \cmidrule ranges in sync with the \multicolumn spans above them, and when you change the column count, check both.
\documentclass[a4paper]{article}
\usepackage{booktabs}
\usepackage{array} % p/m/b refinements, \newcolumntype
\begin{document}
\begin{table}[htbp]
\centering
\caption{Measured flow and pressure for the two sample series.}
\label{tab:samples}
\begin{tabular}{@{}lrr@{\quad}lr@{}}
\toprule
& \multicolumn{2}{c}{Flow (L/min)} & & \\
\cmidrule(l){2-3}
Sample & \multicolumn{1}{c}{mean} & \multicolumn{1}{c}{s.d.} & Set & Notes \\
\midrule
A & 1.02 & 0.04 & 101.3 & valve closed \\
B & 2.04 & 0.06 & 198.7 & open, 3 turns \\
\addlinespace
\multicolumn{2}{@{}l}{pooled} & 1.53 & --- & both series \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
Aligning numbers: why r is not enough
r aligns on the right edge of the box, so 3.5, 12.05 and 1000 end up in three different places. The correct alignment for a column of numbers is on the decimal marker, and the package that does it properly is siunitx: declare the column as S and tell it the shape of the data, S[table-format=4.2] meaning up to four integer digits and two decimals. The column is then as wide as the widest allowed number and every entry lands on the point.
The table-format declaration is not cosmetic. Without it, siunitx sizes the column from the first cell it sees and every later number overflows to the left. Units and uncertainties go in the header or in table-space-text-post=\,\%, which reserves room for a suffix so percentages stay aligned; text inside an S column must be protected by braces, so a header cell is written {Fit (\%)} — the braces are what say "this is text, not a number".
Old-style figures are the other silent failure: a font whose digits have varying heights and widths (text figures, common in classic serif setups) makes a column of numbers look wobbly even when every column is aligned correctly. Tables want lining tabular figures; either ask siunitx to follow the surrounding font with \sisetup{detect-all}, or enable the tabular figure feature in the document font setup (a fontspec or newtx option), which fixes it for the whole document instead of per table.
For a two-line mean-and-error cell, do not stack them with \\ inside a cell — that is illegal in tabular — use \begin{tabular}[t]{@{}r@{}} nested, or the \SI/\num uncertainty syntax. And when the table is generated by a script, emit S columns with the right table-format computed from the data rather than r: it is four lines of code and it removes the whole class of alignment bug reports.
W_{\mathrm{with\;@\{\}}} = W - 2c = W - 2\times 6\,\mathrm{pt}
\documentclass[a4paper]{article}
\usepackage{booktabs,array,tabularx,siunitx}
\usepackage{ragged2e} % \RaggedRight: keeps the left edge, no ugly rags
% X that is justified is ugly in a narrow column: define a ragged X.
\newcolumntype{L}{>{\RaggedRight\arraybackslash}X}
% Vertically centred p column, so a one-line cell sits on the maths axis.
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\begin{table}[htbp]
\centering
\caption{Runs with the fitted parameter and its standard error.}
\label{tab:runs}
\begin{tabularx}{\textwidth}{@{}
l
S[table-format=2.3,table-space-text-post=\,\%]
S[table-format=1.3]
L @{}}
\toprule
Run & {Fit (\%)} & {s.e.} & Comment \\
\midrule
r01 & 12.481\,\% & 0.104 & clean decay, no afterpulse \\
r02 & 9.7\,\% & 0.21 & truncated early by the interlock, so the
tail is unconstrained and the error is large \\
r03 & 12.01\,\% & 0.098 & repeats r01 within one standard error \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
tabularx and how the X column is computed
A fixed p{3cm} column is a decision you have to re-make every time the layout changes. tabularx replaces that with one unknown: you give the total width (\begin{tabularx}{\textwidth}{⟨spec⟩}), write X wherever the flexible columns go, and the package measures the natural widths of all fixed columns, then divides what remains among the X columns equally. It is implemented on top of array and calc, and each X is a p column whose width is computed rather than declared.
The computation is the reason a tabularx table takes an extra compile: the package has to typeset the body once to find the fixed widths, and it stores intermediate values so that the second pass converges. If you see a table that is one line too wide or one line too short for the space, that is the X allocation settling, and it is not a bug to fight — compile again.
Unequal shares are explicit, because the package hands each X column a \hsize of "1" in units of the share: {>{\hsize=1.5\hsize}X>{\hsize=0.5\hsize}X} gives a 3:1 split, and the multipliers of all X columns in the table must sum to the number of X columns, or the table comes out wider or narrower than requested. That arithmetic is the one thing about tabularx that people get wrong, and it fails silently in the layout rather than in the log.
Because X is a p column, it is justified by default — which in a 3 cm cell produces rivers. Define a ragged variant once: \newcolumntype{L}{>{\RaggedRight\arraybackslash}X}. To make X vertically centred rather than top-aligned, redefine what X means: \renewcommand{\tabularxcolumn}[1]{m{#1}}. A tabularx inside a minipage or a subfloat should be sized to \linewidth, not \textwidth, and the caption-plus-tabularx combination belongs inside a table float rather than being floated by itself.
Spanning cells, row heights and tables that run over
\multicolumn{⟨n⟩}{⟨spec⟩}{⟨text⟩} merges ⟨n⟩ cells and — this is the part that surprises people — replaces their specification. That makes \multicolumn{1}{l}{...} a legitimate device for changing the alignment of a single cell, and it is also why a spanning cell loses the @{} padding of the columns it covers: write \multicolumn{3}{@{}l}{...} to keep the left edge flush.
\multirow{⟨n⟩}{⟨width⟩}{⟨text⟩} from the multirow package spans rows. With positive ⟨n⟩ the content is centred on the first row of the span, which is nearly never what you want; the standard fix is to place the cell at the last row of the span with a negative count, \multirow{-2}{*}{Label}, and to use \cline{2-3} instead of \hline so the rule does not cut through the spanning text.
Row heights come from the tallest box in the row plus the strut-ish spacing \arraystretch, and p/m/b decide where a short cell sits in that height: on the first line, centred, or on the last line. A table mixing a two-line paragraph column with single-line numbers usually wants m{}, and the fine-tuning knob for the optical gap between rules is \extrarowheight from array, which adds height above each cell without moving its baseline.
A table longer than a page is not a float at all: longtable is a broken environment that participates in the page builder, so it cannot be given a placement option, cannot be centred with \centering around it (use \setlength{\LTpre}, \LTcapwidth and \endhead machinery instead), and repeats its header through \endfirsthead, \endhead, \endfoot and \endlastfoot. Because it is not a float, \caption numbers it correctly but \label needs care on a multi-page table: put the caption and label in the \endfirsthead block.
\documentclass[a4paper]{article}
\usepackage{booktabs,longtable,array}
\begin{document}
\begin{longtable}{@{}lrrr@{}}
\caption{Annual counts by station.} \label{tab:counts}\\
\toprule
Station & n & mean & s.d. \\
\midrule
\endfirsthead % what appears on the FIRST page only
\multicolumn{4}{@{}l}{Table \thetable{} continued from page \pageref{lastpage}}\\
\toprule
Station & n & mean & s.d. \\
\midrule
\endhead % repeated on every following page
\midrule
\multicolumn{4}{r@{}}{\textit{continued on next page}}\\
\endfoot % foot of every page but the last
\bottomrule
\endlastfoot % foot of the last page
North & 1\,204 & 42.8 & 1.42 \\
South & 1\,188 & 41.9 & 1.51 \\
East & 1\,231 & 43.1 & 1.38 \\
\addlinespace
\textbf{All} & \textbf{3\,623} & \textbf{42.6} & \textbf{1.44} \\
\end{longtable}
\end{document}
resizebox is the wrong answer to a wide table
\resizebox{\textwidth}{!}{\begin{tabular}...\end{tabular}} does make the table fit, and that is the last useful thing it does. It scales the type, so a table set at 8 pt inside a document whose body is 10 pt now has 6 pt digits that no reader can compare with the caption, and its font size agrees with nothing else in the document. It scales the rules too, so a \toprule that was 0.08em becomes a faint hairline that disappears on a projector or in a cheap print run. It also disables the line-breaking of any p column, because the box is measured before being scaled.
The honest fixes, in the order a copy editor will try them. Delete columns: a table with 14 columns is two tables. Transpose it, if the columns were samples and the rows were variables. Move the long explanatory text out of the cells and into the caption or into \tablenotes. Let a tabularx X column absorb the overflow, or set the widest column as an explicit p{} and let it break. Then reduce padding and stretch: \setlength{\tabcolsep}{3pt}, @{} at both edges, \renewcommand{\arraystretch}{0.95}. Then reduce the font one notch inside the float — \small or \footnotesize, declared after \centering so it does not leak — which is legitimate because the caption is scaled with the body.
If the table is still too wide, rotate the page, not the table: \begin{landscape}...\end{landscape} from pdflscape turns the page and leaves the text upright to the reader, while sidewaystable from rotating rotates the box inside an unrotated page — which is fine for print and hostile to a screen reader and to anybody reading on a phone.
The measured, honest version of scaling is adjustbox's max width=\textwidth: it shrinks only what is already too wide, leaves a correctly sized table at 100 percent, and keeps the type size visible in the source rather than hidden in a scale factor. Use it as the last resort, and if you find yourself reaching for it on more than one table, the problem is the column specification, not the width.
\Delta_{\mathrm{over}} = W - w_{\mathrm{line}} > 0 \;\Longleftrightarrow\; \text{an Overfull hbox report}
\hbox (12.34pt too wide) is the exact number to buy back with \tabcolsep, @{} or a narrower p{}.\documentclass[a4paper]{article}
\usepackage{booktabs,adjustbox,tabularx}
\usepackage{pdflscape} % \begin{landscape}: rotates the PAGE, text stays upright
\begin{document}
% (a) Let the prose columns absorb the overflow: X takes what is left.
\begin{table}[htbp]
\centering
\small % applies inside this float only
\begin{tabularx}{\textwidth}{@{}lXr@{}}
\toprule
Model & Interpretation & $R^2$ \\
\midrule
M1 & linear in the driving term & 0.981 \\
M2 & saturating response, two free parameters & 0.997 \\
\bottomrule
\end{tabularx}
\end{table}
% (b) Shrink only the box, never the type: max width keeps 1.0 as an upper
% bound, so a narrow table is not blown up to full width.
\begin{table}[htbp]
\centering
\begin{adjustbox}{max width=\textwidth}
\begin{tabular}{@{}lrrrrrr@{}}
\toprule
& A & B & C & D & E & F \\ \midrule
2021 & 1 & 2 & 3 & 4 & 5 & 6 \\
2022 & 7 & 8 & 9 & 10 & 11 & 12 \\
\bottomrule
\end{tabular}
\end{adjustbox}
\caption{Six columns, scaled at most to the text width.}
\end{table}
% (c) Genuinely wide: rotate the page, not the table.
\begin{landscape}
\begin{table}[htbp]
\centering
\caption{Full parameter matrix, typeset on a landscape page.}
\begin{tabular}{@{}lcccccccccc@{}}
\toprule
& a & b & c & d & e & f & g & h & i & j \\ \midrule
row & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
\bottomrule
\end{tabular}
\end{table}
\end{landscape}
\end{document}
Reading the errors a table produces
Extra alignment tab has been changed to \cr. You used one more & than the specification has columns (or the reverse produces a truncated row rather than an error). Count the tokens in the specification, and remember that @{} and | are not columns while *{5}{r}} is five.
Misplaced \noalign means a rule or \hline arrived where TeX expected the start of a row — typically because a \\ is missing above it, or because \hline was placed inside a cell. Undefined control sequence on \toprule or \multirow is a missing \usepackage; \cmidrule without booktabs and m{} without array are the two most frequent instances.
A caption that will not compile usually contains a character that is fine in a cell and fatal in the list of tables: &, #, _. Escape them, or give the short form for the list: \caption[Flow and pressure]{Flow and pressure for samples A&B, 3 runs}. The same applies to \tabular inside a caption, which needs \parbox rather than a real table.
Spacing that is wrong but not an error. A row that looks too tall almost always has a \\ and a blank line before \bottomrule; a table that is not centred under its caption has \centering after the tabular; a table that overhangs a minipage was sized against \textwidth; and a rule that stops short of the text has @{} on one side only. None of these produce a message, which is why the check for tables is visual and the last thing you do before a submission.
Lesson cheat sheet
One key line of code from each section — jump to it or copy it straight from here.
W = \sum_{k=1}^{n} w_{k} + 2\,n\,c + m\,r \qquad (c=\text{column padding},\; r=\text{rule width})
\documentclass[a4paper]{article}
Common mistakes
- Vertical rules and double
\hlines.booktabsforbids both: a vertical line breaks the horizontal reading the table needs, and a doubled rule becomes a resolution-dependent artefact. - Putting
\label{tab:x}before\caption{...}: the reference then resolves to the section number, and every\refto the table is quietly wrong. - Sizing a
tabularxinside a minipage to\textwidthinstead of\linewidth, and reading the resulting overfull box as a bug in the package. - Forgetting that
Xcolumn multipliers must add up:{>{\hsize=1.4\hsize}X>{\hsize=0.4\hsize}X}is a 3.5:1 split but is 1.8 of 2 shares, so the table comes out too wide. - A single
\at the end of a row instead of\\, or a\\immediately before\bottomrule, which adds an empty row and a visible gap. \resizebox{\textwidth}{!}{...}as a routine: the type, the rules and the caption stop matching the rest of the document, and the real problem — too many columns — survives.- Using
Scolumns withouttable-format: the column is sized from the first number it sees and the rest overflow to the left.
Questions about this lesson
What is the difference between tabular and tabularx?
Why does my LaTeX table need two compilations?
Should I use vertical lines in a LaTeX table?
How do I repeat the header row of a long table?
\endfirsthead, the repeated header above \endhead, the running foot above \endfoot and the final foot above \endlastfoot. longtable is not a float, so it has no placement option and it breaks across pages inside the text flow.How do I align numbers on the decimal point in LaTeX?
My table is wider than the page: what is the right fix?
\small inside its float. Only after that, scale with adjustbox max width; never with resizebox, which also shrinks the rules and the font.Live playground: edit the LaTeX, see it render
The preview updates as you type — break it on purpose to see how errors look.
Keep reading
Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.