Create a table in LaTeX (tabular)
The tabular environment builds a table. Its argument has one letter per column (l, c or r), & separates the cells and \\ ends a row. Put the tabular into a table environment to give it a caption and a number.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{booktabs}Write rows and columns
{lcr} defines three columns: left-aligned, centered and right-aligned. Each row has one & fewer than the table has columns. Spaces around & do not matter, so you can line up the source.
\begin{tabular}{lcr}
Item & Color & Price \\
Apple & red & 0.50 \\
Banana & yellow & 0.25 \\
\end{tabular}Add horizontal rules
The booktabs package provides \toprule, \midrule and \bottomrule. Without a package, \hline draws a horizontal line and a | in the column argument draws a vertical one.
\begin{tabular}{lcr}
\toprule
Item & Color & Price \\
\midrule
Apple & red & 0.50 \\
Banana & yellow & 0.25 \\
\bottomrule
\end{tabular}Add a caption and a number
The table environment is a float: LaTeX moves it to a place where it fits, numbers it and prints the \caption. \centering centers the table, and \label gives it a name that \ref turns into the table number.
\begin{table}
\centering
\caption{Fruit prices}
\label{tab:prices}
\begin{tabular}{lcr}
\toprule
Item & Color & Price \\
\midrule
Apple & red & 0.50 \\
Banana & yellow & 0.25 \\
\bottomrule
\end{tabular}
\end{table}
Table~\ref{tab:prices} lists the prices.Let long text wrap
Text in an l, c or r column stays on one line, however long it is. A p{5cm} column is 5 cm wide and breaks its text into lines.
\begin{tabular}{lp{5cm}}
\toprule
Term & Meaning \\
\midrule
Float & An environment that LaTeX moves to a place where it fits. \\
Rule & A horizontal line in a table. \\
\bottomrule
\end{tabular}Pitfalls
- A row with more cells than the column argument defines stops with Extra alignment tab has been changed to \cr. Count the
&in that row. - A bare
&always starts the next cell. Write\&for an ampersand in the text, and\%and\_for a percent sign and an underscore.
Compiled with pdfLaTeX from TeX Live 2023.