Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 38 ยท Tables, Figures and Lists

Tables in LaTeX

Build LaTeX tables with tabular, column alignment, \hline and booktabs rules, \multicolumn and \multirow, fixed-width columns with p{} and tabularx, long tables and good style.

Tables in LaTeX are written as text grids: one line of source per row, with & between cells. The tabular environment is the foundation; a few packages (booktabs, multirow, tabularx, longtable) cover professional styling, merged cells, width control and tables that span pages. This lesson takes you from a simple 3-column table to publication style.

The tabular environment

LATEX
\begin{tabular}{lcr}
  Name  & Score & Rank \\
  Ada   & 95    & 1    \\
  Ben   & 88    & 2    \\
\end{tabular}

The argument after \begin{tabular} is the column specification, one letter per column.

Letter Meaning
l left aligned
c centered
r right aligned
p{3cm} paragraph column of fixed width, text wraps (top aligned)
m{3cm} like p but vertically centered (needs array)
b{3cm} like p but bottom aligned (needs array)
the vertical bar character a vertical rule between columns
@{...} replace the space between columns with your own content

Cells in a row are separated by &, rows end with \\. Numbers should be right aligned (r) so that the digits line up, or use the siunitx package's S column to align decimal points.

Lines

\hline draws a horizontal line across the whole table; \cline{2-3} draws one under columns 2 and 3. Vertical bars in the specification draw vertical rules.

LATEX
\begin{tabular}{|l|c|r|}
  \hline
  Name & Score & Rank \\ \hline
  Ada  & 95    & 1    \\
  Ben  & 88    & 2    \\ \hline
\end{tabular}

This is the "grid" look many beginners start with. Professional typographers avoid vertical rules and doubled lines, because they chop the table into cells and make it harder to read. The next package solves it.

Professional rules with booktabs

LATEX
\usepackage{booktabs}

\begin{tabular}{lcr}
  \toprule
  Name & Score & Rank \\
  \midrule
  Ada  & 95    & 1    \\
  Ben  & 88    & 2    \\
  \bottomrule
\end{tabular}

\toprule, \midrule and \bottomrule replace \hline, with correct thickness and spacing, and \cmidrule(lr){2-3} draws a short rule under a group of columns. The booktabs documentation suggests three rules: never use vertical lines, use rules sparingly, and avoid double lines. Most journals expect this style.

Compare both styles in the stand-in below. It translates a tabular source into an HTML table, including \hline, the booktabs rules and \multicolumn. It is not LaTeX, but the way columns, rows and rules are described is the same.

tabular to HTML (stand-in, not a real compiler)

Change lcr to rcl, replace the booktabs commands with \hline, or merge another row and watch the table update.

Merging cells: multicolumn and multirow

\multicolumn{n}{alignment}{text} makes one cell span n columns and sets its own alignment:

LATEX
\begin{tabular}{lcc}
  \toprule
  & \multicolumn{2}{c}{Results} \\
  \cmidrule(lr){2-3}
  Method & Time & Memory \\
  \midrule
  A & 1.2 & 30 \\
  B & 0.8 & 45 \\
  \bottomrule
\end{tabular}

For cells that span several rows, use the multirow package: \multirow{2}{*}{Text} goes in the first of the merged rows, and the cells below stay empty.

LATEX
\usepackage{multirow}
...
\multirow{2}{*}{Group A} & Ada & 95 \\
                         & Ben & 88 \\

Count columns carefully: every row must have the same number of cells after merging, or you see "Extra alignment tab has been changed to \cr".

Fixed width columns and automatic width

A column of long text should wrap. Use p{width}:

LATEX
\begin{tabular}{l p{6cm}}
  \toprule
  Term & Meaning \\
  \midrule
  Engine & The program that reads the source file and writes the PDF. \\
  Package & A bundle of macros that extends LaTeX. \\
  \bottomrule
\end{tabular}

Instead of measuring, let the tabularx package fill a given width. The X column absorbs the leftover space and wraps its text:

LATEX
\usepackage{tabularx}
\begin{tabularx}{\textwidth}{l X r}
  \toprule
  Item & Description & Price \\
  \midrule
  Pen & A very long description that wraps inside the flexible column. & 2.50 \\
  \bottomrule
\end{tabularx}

Other helpful packages: array (extended column types), tabulary, makecell (line breaks inside cells with \makecell{a\\b}), siunitx (number alignment), colortbl and xcolor for colored rows (\rowcolors{2}{gray!10}{white}), and diagbox for diagonal header cells.

Wide tables

If a table does not fit the text width, you have these options in order of preference: simplify it, reduce the font (\small or \footnotesize before the table), reduce column padding (\setlength{\tabcolsep}{4pt}), shrink it with \resizebox{\textwidth}{!}{...} from graphicx (use sparingly: it scales the fonts), or rotate the page with pdflscape.

Tables across pages

A tabular cannot break across pages. For a long table, load longtable:

LATEX
\usepackage{longtable}
\begin{longtable}{lr}
  \toprule
  Item & Price \\
  \midrule
  \endhead                % repeated at the top of every page
  Pen  & 2.50 \\
  Book & 12.00 \\
  \bottomrule
\end{longtable}

longtable replaces the table float; give it a caption directly with \caption{...} before \\. It usually needs two runs to settle column widths.

Captioned tables

For a table with a number, caption and a list entry, place the tabular inside a table float. That topic has its own lesson: floats, captions and placement.

Good table style

  • Round numbers to a sensible precision and align digits.
  • Use units in column headings, not in every cell.
  • Put the caption above the table (tables are read like text; figure captions go below).
  • Use few lines, thin horizontal rules, no vertical rules.
  • Do not make a table where a sentence would do.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In \begin{tabular}{lcr}, what do the letters mean?

    Each letter is one column: l left, c centered, r right.
  2. 2. Which package gives \toprule, \midrule and \bottomrule?

    booktabs provides professional horizontal rules.
  3. 3. How do you make a cell that spans two columns and is centered?

    \multicolumn takes the number of columns, the alignment and the content.
  4. 4. Which package lets a table continue across several pages?

    longtable breaks across pages and can repeat a header with \endhead.

Summary

  • tabular takes a column specification (l, c, r, p{width}); cells use & and rows end with \\.
  • Use booktabs rules and avoid vertical lines.
  • \multicolumn and multirow merge cells; tabularx fills a given width; longtable breaks across pages.
  • Wrap a table in a table float to add a caption and number.

Next: Images and Figures