Tables

tabular and its extensions, tables over several pages, and tables built from data.

A table

\begin{table}[htbp]
  \centering
  \caption{Convergence of the projection scheme}
  \label{tab:convergence}
  \begin{tabular}{lrr}
    \hline
    Mesh  & Error           & Rate \\
    \hline
    $h$   & $2.1\times10^{-3}$ & --   \\
    $h/2$ & $5.3\times10^{-4}$ & 1.99 \\
    \hline
  \end{tabular}
\end{table}

tabular

\begin{tabular}{columns} takes one letter for each column, then the rows: & between cells and \\ at the end of a row.

ColumnContents
lLeft-aligned
cCentred
rRight-aligned
p{width}A paragraph of that width, wrapped, top-aligned
|A vertical rule between columns

Rules

\hline draws a rule across the table, and \cline{2-3} across columns 2 to 3 only.

Spanning columns

\multicolumn{2}{c}{Heading} makes one cell across two columns, with its own alignment.

The table float

The table block places the table and gives it its caption and label. See Pictures and Floats for how floats are positioned.


booktabs

booktabs' rules are drawn with the weights and spacing booktabs gives them, heavy at the top and bottom and light between.

\usepackage{booktabs}

\begin{tabular}{lr}
  \toprule
  Name & Value \\
  \midrule
  Mass & 2.3   \\
  \cmidrule(lr){1-2}
  Charge & 1.6 \\
  \bottomrule
\end{tabular}

\toprule, \midrule and \bottomrule

The rule above the table, the rule under its head, and the rule closing it.

\cmidrule

\cmidrule{1-2} draws a light rule across some columns. (lr) trims it at the left and right, so rules over columns side by side do not touch.


array

array's additions are read by tabular itself.

m and b columns

m{width} is a paragraph column centred against the row, and b{width} one aligned at its bottom.

Before and after a column

>{...} runs code before every cell of a column and <{...} after it, as in >{\bfseries}l for a bold column.

Between columns

@{...} replaces the space between two columns with what it holds, and !{...} puts something between them while keeping the space.

Column types of your own

\newcolumntype names a column type:

\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\begin{tabular}{C{3cm}C{3cm}} ... \end{tabular}

Tables to a width

tabularx

tabularx takes a width and shares what the other columns leave among its X columns:

\usepackage{tabularx}
\begin{tabularx}{\textwidth}{lX}
  Name & A description long enough to wrap onto a second line. \\
\end{tabularx}

tabulary

tabulary's columns L, C, R and J share the width by how much text they hold.

tabu

\begin{tabu} to \linewidth {X[l] X[c] X[r]} sets a table to a width with X columns aligned as given.

tabularray

tabularray's tblr, with colspec=, is read as the tabular it describes.


Tables over several pages

longtable

longtable is set a row at a time, so a page can end between any two rows:

\usepackage{longtable}

\begin{longtable}{lr}
  \caption{Prices} \\
  \hline Item & Price \\ \hline
  \endfirsthead
  \hline Item & Price \\ \hline
  \endhead
  \hline
  \endfoot
  Tea  & 2 \\
  Cake & 3 \\
\end{longtable}

Heads and feet

  • The rows before \endhead repeat at the top of every page the table continues on.
  • The rows before \endfoot close every page it breaks off.
  • \endfirsthead and \endlastfoot give the first and last pages their own.

Other long tables

longtable*, supertabular, xtabular, xltabular, tabularray's longtblr and tabu's longtabu break the same way.


Cells

multirow

\multirow{2}{*}{Text} makes one cell down two rows. It stands in the middle of the rows it spans, or level with the first for [t] and the last for [b]:

\usepackage{multirow}
\begin{tabular}{ll}
  \multirow{2}{*}{Fluids} & Water \\
                          & Oil   \\
\end{tabular}

makecell

\makecell{Two\\lines} puts several lines in one cell, and \thead sets a bold head.

diagbox

\diagbox{Row}{Column} splits a cell by a diagonal, a label either side.

colortbl

\rowcolor, \cellcolor and \columncolor colour rows, cells and columns. Rules stay black.

\usepackage{colortbl}
\begin{tabular}{lr}
  \rowcolor{lightgray} Item & Price \\
  Tea & \cellcolor{yellow} 2 \\
\end{tabular}

threeparttable

threeparttable keeps a table, its caption and its notes together.

bigstrut

\bigstrut makes a cell taller, for a cell a rule runs close to.

Approximated packages

dcolumn's D column sets numbers centred rather than aligned on the decimal point, and hhline's rules are drawn as one rule across the table.


Tables from data

csvsimple and datatool are built in, so a table can be filled from a file of comma-, semicolon- or tab-separated values.

csvsimple

\csvautobooktabular{file} sets a whole file as a booktabs table, its first line the head:

\usepackage{csvsimple}
\csvautobooktabular{results.csv}

\csvreader with head to column names sets each row as you write it.

datatool

\DTLloaddb loads a file, and \DTLforeach, \DTLsort and \DTLsumcolumn go through, sort and add up its rows.

pgfplotstable

pgfplotstable's \pgfplotstabletypeset sets a table from its file.


Right to left

A table in Arabic or Hebrew runs right to left: its first column at the right, its rules and padding mirrored, as polyglossia sets it. See Languages.


  • Tables: every package, with its commands and an example.