Tables and matrices

Build tables with tabular, merge cells and format them for publication.

The tabular environment lays out data in rows and columns.

A basic grid

The argument after \begin{tabular} defines the columns: l is left-aligned, c centered, r right-aligned, and | draws a vertical line. Inside the table, & separates columns and \\ ends a row. \hline draws a horizontal line.

\begin{tabular}{ | l | c | r | }
  \hline
  Item & Qty & Price \\
  \hline
  Widget & 42 & \$10.00 \\
  Gadget & 13 & \$19.99 \\
  \hline
\end{tabular}

This gives a three-column grid with a left-aligned, a centred and a right-aligned column.

A bare $ starts math mode, so write dollar signs in text as \$.

Merging cells

Use \multicolumn{columns}{alignment}{text} to span several columns.

\begin{tabular}{|c|c|}
  \hline
  \multicolumn{2}{|c|}{Team Stats} \\
  \hline
  Wins & Losses \\
  \hline
\end{tabular}

Publication-quality tables

Published tables usually avoid vertical lines. The booktabs package gives clean horizontal rules and better spacing.

\usepackage{booktabs}

\begin{tabular}{llr}
  \toprule
  Item & Qty & Price \\
  \midrule
  Apple & 5 & \$1.00 \\
  Pear & 10 & \$2.00 \\
  \bottomrule
\end{tabular}

Captions

To number and caption a table, wrap it in a table environment. By convention the caption goes above the table.

\begin{table}[htbp]
  \centering
  \caption{Prices}
  \label{tab:prices}
  \begin{tabular}{llr}
    \toprule
    Item & Qty & Price \\
    \midrule
    Apple & 5 & \$1.00 \\
    \bottomrule
  \end{tabular}
\end{table}

Matrices are math, not tables. See Advanced mathematics.