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}
Result
The compiled example

Open in the editor

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}
Result
The compiled example

Open in the editor

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.
Result
The compiled example

Open in the editor

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}
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages