¶ Writing & documents

Tables and figures in LaTeX: booktabs, floats and captions

How to insert images and tables in LaTeX without losing your mind: graphicx, float environments, captions, references, booktabs and controlling placement.

Tables and images are where many people give up on LaTeX, usually for two reasons: table syntax looks hostile and figures «run away» to another page. Both have an explanation and a solution. Once you understand the mechanism, LaTeX produces cleaner tables than almost any word processor.

Inserting an image

You need the graphicx package in the preamble. The basic command is:

\includegraphics[width=0.8\textwidth]{img/network-diagram.pdf}
  • width=0.8\textwidth sets the width to 80% of the text block: better than absolute measures, because it adapts if you change the margins.
  • The formats accepted by pdfLaTeX are PDF, PNG and JPG. For diagrams and charts prefer vector PDF, which stays sharp at any zoom; PNG for screenshots; JPG for photographs.
  • Avoid spaces and accented characters in file names: they are a frequent cause of mysterious errors.

Float environments

An image inserted like that sits exactly where you write it. The problem is that, if there is no room at the bottom of the page, it leaves a white gap. That is why LaTeX offers the float environments figure and table, which can move to the typographically best spot:

\begin{figure}[htbp]
  \centering
  \includegraphics[width=0.7\textwidth]{img/network-diagram.pdf}
  \caption{Office network diagram.}
  \label{fig:network}
\end{figure}

The letters in brackets state the allowed positions: h here, t at the top of a page, b at the bottom, p on a page of floats only. Giving more options leaves LaTeX free to find a good solution. Using only [h] is the quickest way to end up with all your figures piled up at the end of the chapter.

References instead of «below»

The fact that figures move is the reason why in the text you do not write «the figure below», but:

As figure~\ref{fig:network} shows, the firewall separates the two networks.

The number is assigned and updated automatically. With the hyperref package the reference also becomes clickable in the PDF. Always place \label after \caption, otherwise the reference will point to the wrong number.

Table syntax

The content of a table sits in the tabular environment. The column specification says how many there are and how they are aligned:

\begin{tabular}{l c r}
  Service & Port & Users \\
  Mail    & 993  & 42    \\
  Web     & 443  & 120   \\
\end{tabular}
  • l, c, r align left, centre, right.
  • & separates cells, \\ ends the row.
  • p{4cm} creates a fixed-width column in which text wraps: useful for long descriptions.

Professional tables with booktabs

Tables with a full grid, vertical lines and double horizontal rules are hard to read. The booktabs package proposes a cleaner approach: only three horizontal rules of different weight and no vertical ones.

\begin{table}[htbp]
  \centering
  \caption{Exposed services and active users.}
  \label{tab:services}
  \begin{tabular}{l c r}
    \toprule
    Service & Port & Users \\
    \midrule
    Mail    & 993  & 42    \\
    Web     & 443  & 120   \\
    \bottomrule
  \end{tabular}
\end{table}

For tables, the common convention is to put the caption above, while for figures it goes below. Align numbers to the right, or on the decimal point with the siunitx package, so that figures can be compared at a glance.

Typical problems and solutions

Problem Likely cause Solution
The figure ends up at the end of the chapter Only [h] or figures too large Use [htbp] and reduce the size
The table runs past the margin Columns with long text p{} columns or the tabularx package
Table longer than a page tabular does not break The longtable package
Reference shows «??» A second compilation is missing Recompile or use latexmk
Wrong number in the reference \label before \caption Move \label after

A practical tip

Writing large tables by hand is tedious. There are generators that convert a spreadsheet into tabular code, and Pandoc converts Markdown tables to LaTeX. For data that changes often, it is even better to keep it in a CSV file and import it with packages such as csvsimple: the table then updates itself.

In short

  • graphicx and \includegraphics insert images; vector PDFs are best for diagrams.
  • figure and table are floats: they move to avoid gaps on the page.
  • You cite them with \ref and \label, placing \label after \caption.
  • booktabs produces clean tables with three horizontal rules and no vertical ones.
  • tabularx, longtable and siunitx solve width, length and number alignment.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. A computer science graduate, he combines rigorous technical analysis with attention to the people who use technology.

Read also