Three packages with the same name and one you should use
There are three historical ways to build a panel figure, and they are mutually incompatible at the level of error messages. The original subfigure package (the \subfigure[...]{...} command) is long unmaintained and must not be loaded with anything else. subfig by Steven D. Cochran replaced it, using \subfloat as its interface and its own option syntax; it works, and it is the package most older answers and templates assume. subcaption, from the caption bundle, is the current one: it provides a subfigure environment and a \subcaptionbox command, and — the decisive advantage — it inherits every caption setting, so panel captions take the font, spacing and label format you already configured for the document.
The conflicts are loud and worth knowing by sight. subcaption refuses to coexist with subfig and with the old subfigure package, and the error text says exactly that, but the confusing part is that the older package is sometimes loaded for you: a document class, a journal class file or subfig pulled in by \usepackage{subfigure} in a template line nobody remembers. If you see Package subcaption Error: this package can not be used together with the subfig package, grep the whole project — including the class file — for subfig.
What the packages actually give you is a second-level counter. The parent float keeps its own series (Figure 3) and each panel gets a letter series tied to it, so a panel is referred to as 3(a) rather than as a new figure. That matters for how you write: a reviewer asks for "Figure 3(a)", the list of figures gets one entry for the group, and the panels stay inside one placement decision, so they cannot drift apart — which is the whole point.
Decide once per project. Mixing \subfloat from subfig with subcaption syntax in the same document is the most common way a paper that compiled for its author fails for the journal.
The subfigure environment, and what its arguments mean
\begin{subfigure}[⟨pos⟩]{⟨width⟩} opens a box of the given width, with ⟨pos⟩ deciding which line of that box sits on the surrounding baseline: t, c or b — the first baseline, the vertical centre, or the last baseline. Omit it and you get c, which is why two panels of different heights look centred against each other rather than top-aligned, which is almost never what a figure wants.
Inside the box: \centering first, then \includegraphics[width=\linewidth]{...} — note \linewidth and not \textwidth, because inside a sub-figure box the current line width is the panel width — then \caption{...} and, after it, \label{...}. The label order rule is the same as for any float: the sub-caption steps the panel counter, and a label placed before it picks up whatever number was current at that moment.
Panel numbering is a counter like any other. \thesubfigure holds the format (default \alph{subfigure}), and the prefix that makes a panel read "3(a)" rather than "a" comes from \p@subfigure: the \ref machinery concatenates the parent prefix with the panel format, because \label inside a sub-figure is written with the parent counter in effect. If you want 3.1 instead of 3(a), redefine \thesubfigure to \arabic{subfigure} and adjust \p@subfigure to end with a period; if you want a bracketed style, do it in the caption label format rather than by hand.
Formatting is caption syntax scoped to the sub-type: \captionsetup[subfigure]{labelformat=parens, font=small, skip=4pt, list=true}. labelformat chooses how the panel label is printed (simple, parens, subparens, empty), labels=break lets a long caption break away from its label instead of hanging, skip is the gap between graphic and panel caption, and list controls whether the panel is written to the list of figures at all — set list=false document-wide when only the group should appear there, or give one panel its own \caption[LOF entry]{...} override.
\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage{subcaption} % loads caption automatically
% Panel counters and their punctuation are ordinary LaTeX counters.
\renewcommand{\thesubfigure}{\alph{subfigure}}
\captionsetup[subfigure]{labelformat=parens,labels=break,skip=4pt}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{subfigure}[b]{0.48\textwidth}
\centering
\includegraphics[width=\linewidth]{voltage-decay}
\caption{Capacitor voltage.}
\label{fig:v}
\end{subfigure}%
\hfill
\begin{subfigure}[b]{0.48\textwidth}
\centering
\includegraphics[width=\linewidth]{current-decay}
\caption{Discharge current.}
\label{fig:i}
\end{subfigure}
\caption{RC discharge for the same run, measured on the two channels.}
\label{fig:rc}
\end{figure}
Compare Figure~\ref{fig:rc} with its panels Figure~\ref{fig:v} and
Figure~\ref{fig:i}: the group has one number, the panels have letters.
\end{document}
subcaptionbox, which avoids the baseline problem entirely
\subcaptionbox[⟨LOF entry⟩]{⟨caption⟩}[⟨width⟩]{⟨content⟩} is a command rather than an environment, and the difference is not cosmetic: the content is typeset as a box and the caption is placed beneath it, so the baseline of the whole construct is the baseline of the content. Two \subcaptionboxes on the same line align by their graphics rather than by their middles, which is what you want when one panel is a plot and the other is a plot with a legend sticking up.
The optional [⟨width⟩] after the caption sets the width of the caption block, not of the graphic: giving it lets a caption wrap to a narrower measure than its panel, and omitting it makes the caption take the natural width of the content. The first optional argument is the list-of-figures text, so a short panel title in the LOF and a long explanatory caption on the page is one argument each.
A label belongs inside the caption: \subcaptionbox{Voltage.\label{fig:v2}}[0.46\textwidth]{...}. That is the one syntactic difference from the environment form that catches people, because a label written after the closing brace refers to the parent figure and every panel then resolves to the same number.
Use the environment when a panel has several objects or its own internal layout; use \subcaptionbox when a panel is one graphic and the only question is alignment. Both produce the same counters and the same LOF behaviour, so you can switch per figure without touching the document setup.
\documentclass[a4paper]{article}
\usepackage{graphicx,subcaption}
\begin{document}
% Syntax: \subcaptionbox[LOF entry]{caption}[width]{content}
% The content is typeset first, so its baseline is the baseline of the
% whole box -- no minipage trap at all.
\begin{figure}[htbp]
\centering
\subcaptionbox{Voltage channel.\label{fig:v2}}[0.46\textwidth]{%
\includegraphics[width=\linewidth]{voltage-decay}%
}\hfill
\subcaptionbox{Current channel.\label{fig:i2}}[0.46\textwidth]{%
\includegraphics[width=\linewidth]{current-decay}%
}
\caption{The same run on both channels.}
\label{fig:rc2}
\end{figure}
% Unequal panels: give the widths yourself, they need not be equal.
\begin{figure}[htbp]
\centering
\subcaptionbox{Spectrum.\label{fig:spec}}[0.62\textwidth]{%
\includegraphics[width=\linewidth]{spectrum}%
}\hfill
\subcaptionbox{Residuals.\label{fig:res}}[0.34\textwidth]{%
\includegraphics[width=\linewidth]{residuals}%
}
\caption{Fit and residuals, in a 2:1 ratio.}
\end{figure}
\end{document}
The minipage baseline trap
Every sub-figure is a box in a line, and a line is aligned by baselines, not by top edges. An ordinary minipage (and a subfigure with no positional argument) puts its centre on the surrounding baseline; [t] puts the baseline of its first line there; [b] puts the last line's baseline there. So two boxes of heights h1 and h2 aligned by c have their tops displaced by (h1-h2)/2, which is precisely the "why are these two figures not level" complaint.
The second half of the trap is that a graphic has no baseline of its own: an \includegraphics box sits on the baseline with zero depth, so in a [t] box whose first line is an image, the "first baseline" is the bottom edge of the image. Text, by contrast, has a real first baseline one ascent down. That is why [t] alone does not level a text panel against a graphic panel.
The standard fix is to give the box a first line of its own: \vspace{0pt} as the first material inside a [t] minipage or sub-figure creates a zero-height first line whose baseline is the top of the content, so all tops then really do line up. The same trick fixes the classic "two minipages, one with a table and one with a paragraph, look wrong" problem, and it costs nothing to leave in.
The practical choice for a row of plots is [b]: last baselines equal means the bottoms of the graphics and the tops of their captions sit consistently, and it degrades gracefully when one panel has a two-line caption. [t] is right when the panels are text blocks or tables you want level at the header. [c] is right for nothing except very even panels, which is why it should not have been the default.
Centering and stray spaces inside the boxes
\centering inside a minipage or sub-figure is local in the useful direction: it does not leak out and it does not affect the next box, but it must be the first thing in the box, because it is a switch that governs the paragraph it is in. The parent float's own \centering centres the row of panels; it does nothing for the content of each panel, which is why every listing in this page writes \centering inside each box as well as in the float.
The stray-space problem is a width problem masquerading as a bug. Source lines break after \end{subfigure}, and TeX reads a newline as a space, so a row of two 0.49\textwidth boxes plus an \hfill plus an invisible inter-word space can exceed the measure and produce Overfull \hbox. The habit that removes it is a % at the end of the line before the separator, as in the first listing: \end{subfigure}% then newline then \hfill. The same applies after \end{minipage}.
Budget the row as arithmetic rather than by trial. With a total measure W, n equal panels and a separation g, each panel may be (W - (n-1)g)/n wide, and anything above that is a promise you cannot keep. The tempting 0.5\textwidth for two panels is wrong by exactly one separation plus one space, which is why 0.48 with \hfill is the idiom: \hfill takes whatever is left and stretches, so the pair stays centred and the sum never exceeds the measure.
A blank line inside a panel ends a paragraph, and two blank lines inside a box is legal but changes its height by one \parskip plus a baseline — enough to move the whole row to the next page. If a panel is producing a vertical gap you cannot explain, look for a blank line before its closing \end{subfigure} or immediately after \caption.
2 \times 0.52\,W + g \;>\; W \quad\Longleftrightarrow\quad \text{an overfull hbox in the log}
Numbering, references and the list of figures
A group has one number and the panels have a letter series tied to it. The parent float's caption and label come last inside the float — after the panel row — so \ref{fig:rc} gives the group and \ref{fig:v} gives 3(a). Nothing in the source says which is which, so the reader-facing convention matters: write "Figure 3(a)" in prose, never "figure 3a" typed by hand, and never \ref{fig:rc}(a), which breaks the moment a panel is added.
Each panel writes its own list-of-figures entry by default, indented under the group, with the format controlled by \captionsetup[subfigure]{list=true,listformat=simple} (simple, parens, subsimple, subparens). Most journals want only the group in the list: list=false is the setting, not a commented-out caption.
With hyperref, panel links work out of the box because subcaption registers the parent/child anchor names itself; the ordering rule that still applies is the usual one — hyperref last among the packages that matter, and cleveref after it. cleveref then produces "figures 3(a) and 3(b)" from \cref{fig:v,fig:i}, and if it does not, the cause is that subcaption was loaded after cleveref rather than before.
Counters are per-type, so panels of different kinds in one row do not share a series: a subfigure and a subtable in the same float number independently within the same parent number. If a float contains only tables and you write subfigure for them, the panels are still numbered in the figure series and the caption says "Figure" — the environment chooses the type, not the content.
h_{\mathrm{cell}} = h_{\mathrm{graphic}} + g_{\mathrm{cap}} + h_{\mathrm{caption}}
Alternatives, and when a group is the wrong model
Two independent floats on one row is a different request from a group of panels, and floatrow — also from the caption author — is the tool for it: \begin{floatrow} with \ffigbox and \fcapside places two separately numbered floats side by side, with one width ratio (\floatsetup[figure]{setlayout={capbeside,capside},capwidth=...}) and the ability to put the caption beside rather than under the graphic. Choose it for a figure and its data table printed together but cited separately. It is not compatible with subcaption, which is the same design decision expressed in the other direction.
Plain minipages with \captionof are the lightest escape: two minipage boxes side by side, each with its own \captionof{figure}{...}, gives you independent numbers without a float at all and without loading anything beyond caption or capt-of. The cost is that nothing floats — the row sits exactly where you wrote it and cannot break across pages, so it belongs at the top of a page or between paragraphs.
For a grid more complicated than one row, a tabular of sub-figures is often easier to reason about than nested minipages: one row per band, each cell a 0.48\linewidth box, with @{} specifications controlling the gutter exactly. It also keeps the baseline behaviour predictable, since a tabular row aligns on its last baselines by default. Wrap that tabular in a single figure float with one parent caption and you have the same numbering model as subcaption, but with grid control.
Finally, know when the group is a presentation choice rather than an argument. Panels that are read one at a time, cited separately, or printed on different pages of a supplement are better as separate figures: a group forces one placement decision on the page breaker, and a group that does not fit becomes the biggest float on the list, which is how a three-panel figure ends up alone on its own page.
\documentclass[a4paper]{article}
\usepackage{graphicx,subcaption,booktabs,array}
\begin{document}
\begin{figure}[htbp]
\centering
% A subtable next to a subfigure: both are boxes, both are numbered
% in their own counter series (a, b for tables is usually 1a, 1b too).
\begin{subfigure}[t]{0.44\textwidth}
\vspace{0pt} % gives the box a first baseline
\centering
\includegraphics[width=\linewidth]{micrograph}
\caption{500x detail.}
\label{fig:micro}
\end{subfigure}\hfill
\begin{subtable}[t]{0.52\textwidth}
\centering
\begin{tabular}{@{}lrr@{}}
\toprule
Region & grain & s.d. \\ \midrule
A & 12.4 & 1.1 \\
B & 9.8 & 0.7 \\
\bottomrule
\end{tabular}
\caption{Grain counts.}
\label{tab:grain}
\end{subtable}
\caption{Micrograph and the counts taken from it.}
\label{fig:sample}
\end{figure}
% Three panels, two over one: a minipage inside the float, then a row
% of subfigures inside that minipage.
\begin{figure}[htbp]
\centering
\begin{minipage}{0.98\textwidth}
\centering
\begin{subfigure}[b]{0.48\textwidth}
\centering
\includegraphics[width=\linewidth]{panel-a}
\caption{Panel A.}
\end{subfigure}\hfill
\begin{subfigure}[b]{0.48\textwidth}
\centering
\includegraphics[width=\linewidth]{panel-b}
\caption{Panel B.}
\end{subfigure}
\medskip
\begin{subfigure}[b]{0.7\textwidth}
\centering
\includegraphics[width=\linewidth]{panel-c}
\caption{Panel C, full width of the group.}
\end{subfigure}
\end{minipage}
\caption{Two panels above a third.}
\label{fig:three}
\end{figure}
\end{document}
Lesson cheat sheet
One key line of code from each section — jump to it or copy it straight from here.
\documentclass[a4paper]{article}
2 \times 0.52\,W + g \;>\; W \quad\Longleftrightarrow\quad \text{an overfull hbox in the log}
h_{\mathrm{cell}} = h_{\mathrm{graphic}} + g_{\mathrm{cap}} + h_{\mathrm{caption}}
Common mistakes
- Loading
subcaptionin a document that already loadssubfigor the oldsubfigurepackage, often from a class file. The build dies at the preamble; grep the project before editing your own\usepackageline. - Writing
\includegraphics[width=\textwidth]{...}inside a panel. Inside a box the reference length is\linewidth, so the graphic is a size or a space too wide and the row overflows. - Relying on the default
[c]alignment for panels of different heights: their tops end up displaced by half the height difference. Use[b]for rows of graphics,[t]\vspace{0pt}for rows of text or tables. - Putting
\labelafter the closing brace of\subcaptionboxinstead of inside its caption: every panel then resolves to the parent figure number. - Letting
0.5\textwidth + 0.5\textwidthplus a separator near-exceed the measure, then adding\vspaceto hide the resulting overfull box. Do the width arithmetic:(W-(n-1)g)/n. - A blank line before
\end{subfigure}adds a paragraph gap to the box height, which can move the whole row to the next page. - Naming panels by hand in the prose ("Fig. 2a") instead of
Figure~\ref{fig:sub-a}, so the text contradicts the caption after one insertion.
Questions about this lesson
How do I put two images side by side in LaTeX with separate captions?
\textwidth inside one figure float, each with its own \caption and \label, separated by \hfill. The float gets one parent caption at the end, so the pair is cited as Figure 3(a) and Figure 3(b).What is the difference between subcaption and subfig?
\subcaptionbox. subfig uses \subfloat and its own option list. They cannot both be loaded, and the old subfigure package cannot be loaded with either.Why are my two minipages not aligned at the top?
\vspace{0pt} so the first baseline exists; for graphics-only panels [b] is usually the better choice.How do I make sub-figure labels read 1a, 1b instead of (a), (b)?
\thesubfigure and the parent prefix from \p@subfigure. Redefine \thesubfigure to whatever you want printed, and set \p@subfigure to the parent number plus the separator you prefer. With hyperref and cleveref loaded in the right order, \cref then spells them out consistently.Can I put a table and a figure side by side as panels?
How do I keep panel captions out of the list of figures?
\captionsetup[subfigure]{list=false} in the preamble. The panels keep their captions on the page and their letter numbers; only the parent float writes an entry. The opposite setting, listformat=simple or subparens, controls how panel entries look when you do want them.Live playground: edit the LaTeX, see it render
The preview updates as you type — break it on purpose to see how errors look.
Keep reading
Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.