Captions & subfigures

The \caption command that labels a figure or table looks humble, but it sits at the center of numbering, cross-referencing, and the lists of figures and tables. This page first nails down the basics of \caption — that it only works inside a float, that it auto-numbers, how to give it a short heading, and where to put \label — then moves on to the caption package for styling captions in detail, and the subcaption package for splitting one figure into (a), (b), and so on.

The basics of `\caption`

\caption{…} attaches explanatory text — a caption — to a figure or table. The key restriction is that **it works only inside a float, that is, inside a figure or table environment. Writing it in ordinary text is an error. Placed inside a float, LaTeX auto-numbers it per type** — “Figure 1,” “Table 3” — and sets that number together with the caption text in a fixed format. You never write the number yourself.

Here is a minimal figure. \centering centers the artwork, \includegraphics pulls in the image, and \caption sits below it.

latex
\begin{figure}
  \centering
  \includegraphics[width=0.6\textwidth]{plot}
  \caption{測定値と理論曲線の比較。}
  \label{fig:plot}
\end{figure}

When a caption is long and you want only a short line in the List of Figures (or List of Tables), use the optional argument in square brackets: \caption[short heading]{full text}. What is in the brackets goes to the list, while what is in the braces is set beside the figure. Omit the brackets and the full text is used in the list as well.

latex
\caption[測定値と理論曲線]{測定値(点)と理論曲線(実線)の比較。誤差棒は標準偏差を表す。}

For cross-referencing, \label carries an easily-missed rule: **always put \label after \caption.** LaTeX updates “the current number” to the figure’s number only when it processes \caption, so a \label placed before it picks up the preceding counter (a section number, say), and \ref then returns the wrong value. Put \label right after \caption — same line or the next — and \ref{fig:plot} correctly yields “1.”

A word on convention: a figure’s caption usually goes below the figure, a table’s caption above the table. LaTeX sets \caption wherever you write it, so place it before the artwork for a caption on top, or after it for one below (the format itself — the gap between number and text, and so on — does not change with position). Journals often dictate the placement, so check your venue’s rules first.

Styling with the caption package

To change how captions look — their font, the separator between number and text, their alignment — use the caption package by Axel Sommerfeldt. Load it in the preamble with \usepackage{caption} and set options with \captionsetup{key=value, …}. The keys you will reach for most often are these.

KeyCommon valuesEffect
formatplain / hangHow the body is set; hang indents continuation lines to the label width
labelsepcolon / period / space / quadSeparator between the label and the text; colon gives “: ”, period “. ”
fontsmall / bf / it …Font and size of the whole caption
labelfontbf / sc …Font of just the “Figure 1” label part
justificationcentering / raggedright / justifiedAlignment: centered, flush-left (ragged-right), or justified
widtha length (e.g. 0.8\textwidth)Limit the wrapping width of the caption
singlelinechecktrue / falseWhether short, one-line captions are auto-centered

For instance, “bold label, period separator, smaller font throughout” is set once in the preamble like this. A \captionsetup written in the preamble affects the whole document; written inside a float, it affects only that one caption.

latex
\usepackage{caption}
\captionsetup{labelfont=bf, labelsep=period, font=small, justification=raggedright}

To style figures and tables differently, name the float type in square brackets: \captionsetup[figure]{…} and \captionsetup[table]{…}. Note that singlelinecheck is on by default and centers any caption that fits on one line, regardless of your justification. Add singlelinecheck=false to keep such captions flush-left.

The caption package adds one more handy command, \captionof{type}{text}. It lets you caption something that is not in a float: inside a minipage or center, where plain \caption will not work, \captionof{figure}{…} numbers and references the item correctly as a “Figure.” It is useful when you want a figure and its caption fixed at a spot in the text rather than riding the float-placement machinery.

latex
\begin{center}
  \includegraphics[width=0.5\textwidth]{diagram}
  \captionof{figure}{フロートに入れずに置いた図。}
  \label{fig:inline}
\end{center}

Splitting a figure with subcaption

Often you want several small figures side by side inside one figure, each with its own (a), (b), (c) sub-label. The current standard for this is the subcaption package, also by Sommerfeldt. Just \usepackage{subcaption} — it automatically loads the caption package underneath.

The workhorse is the subfigure environment (subtable for tables). You give it a mandatory width argument\begin{subfigure}[pos]{width} — where the optional [b] and the like set vertical alignment. It is in effect a minipage of that width, holding an \includegraphics and a \caption. **A \caption inside a subfigure becomes an (a)/(b) sub-label**, while the \caption placed outside the subfigure environments numbers the figure as a whole.

Here is a complete example with two subfigures side by side. Each subfigure is 0.45\textwidth wide, with \hfill between them to push them to the two sides.

latex
\usepackage{graphicx}
\usepackage{subcaption}

\begin{figure}
  \centering
  \begin{subfigure}[b]{0.45\textwidth}
    \centering
    \includegraphics[width=\textwidth]{before}
    \caption{処理前}
    \label{fig:before}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.45\textwidth}
    \centering
    \includegraphics[width=\textwidth]{after}
    \caption{処理後}
    \label{fig:after}
  \end{subfigure}
  \caption{画像処理の前後比較。}
  \label{fig:compare}
\end{figure}

Here the whole figure becomes “Figure 1,” and the two subfigures are set as “(a) Before” and “(b) After.” From the text, \ref{fig:before} returns “1a” and \ref{fig:compare} returns “1.” When you want just the sub-label — “(a)” and the like — write \subref{fig:before} for a bare “a,” or \subref*{fig:before} for “(a).”

Besides the subfigure environment, there is the one-line \subcaptionbox command. Its form is \subcaptionbox[list entry]{heading}[width][inner-pos]{contents}, and here you put **the \label inside the heading argument** (for example \subcaptionbox{Before\label{fig:before}}{\includegraphics{…}}). It spares you a multi-line minipage, so it suits simple subfigures that just hold one image.

Avoid the legacy packages (subfig, subfigure)

Packages for subfigures come in three historical generations, and they are easy to confuse. The oldest, subfigure, is obsolete and uses an old \subfigure command. Its successor, subfig (with \subfloat), is no longer actively maintained either. For new documents, the recommendation is the subcaption package described on this page.

Beware that you cannot load these together. Putting both subcaption and subfig (or the old subfigure) in your \usepackage list makes their internal definitions clash and produces a flood of errors. A journal template may already load an older package, so check what the template loads before adding a different one yourself.

  • subcaption — the current standard; the subfigure / subtable environments plus \subcaptionbox and \subref. Integrated with the caption package; recommended here.
  • subfig — the previous generation, with \subfloat. Maintenance has stalled; not recommended for new work.
  • subfigure — the oldest, with \subfigure. Obsolete; do not use it.