Subcaption
Subcaption
Axel Sommerfeldt
https://gitlab.com/axelsommerfeldt/caption
2023/08/13
Abstract
This package offers an user interface to typeset sub-captions.
At the end of each section, text marked with the mountain symbol will contain background knowl-
edge on how the particular command or environment is actually implemented. If you just want to
use this package as it is, you don’t have to read or understand them.
Since version 3.1 the caption package offers a low-level interface to typeset sub-captions:
\DeclareCaptionSubType defines the required counters and internal commands, \set-
captionsubtype switches to the sub-caption mode, and \caption@subtypehook could
be extended to apply own code when a switch to the sub-caption mode is in progress.
This package demonstrates its usage by offering a high-level user interface additionally.
1
Contents
1 Loading the package 3
2 Setting options 3
7 Cross Referencing 12
7.1 The \subref command . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7.2 The subrefformat= option . . . . . . . . . . . . . . . . . . . . . . . 13
7.3 Referencing sub-figures without sub-captions . . . . . . . . . . . . . . . 14
7.4 Typesetting sub-captions without generating a (new) reference . . . . . . 15
7.5 Where to place the \label command? . . . . . . . . . . . . . . . . . . 16
7.6 Where do hyperlinks jump? . . . . . . . . . . . . . . . . . . . . . . . . . 17
9 Numbering 22
9.1 Pittfall #1: Using multiple main captions . . . . . . . . . . . . . . . . . . 22
9.2 Pittfall #2: Expecting \caption to increment the counter . . . . . . . . 24
10 Abbreviatory commands 25
12 Required packages 26
15 Thanks 28
2
1 Loading the package
Load this package using
\usepackage[⟨options⟩]{subcaption} .
The options for the subcaption package are the same ones as for the caption package, but
specify settings which are used for sub-captions additionally. In fact
\usepackage[⟨options⟩]{subcaption}
is identical to
\usepackage{subcaption}
\subcaptionsetup{⟨options⟩} .
margin=0pt,font+=smaller,labelformat=parens,labelsep=space,
skip=6pt,list=false,hypcap=false 1
2 Setting options
\subcaptionsetup The \subcaptionsetup command sets options specifically for sub-captions.
New feature
v1.6 \subcaptionsetup{⟨options⟩}
\subcaptionsetup{font=it}
\subcaptionsetup[⟨environment⟩]{⟨options⟩}
is an alternative syntax to
\captionsetup[sub⟨environment⟩]{⟨options⟩} .
1 This means that sub-captions are not listed in the List of Figures or Tables by default, but you can enable
3
An example:
\subcaptionsetup[table]{labelformat=simple}
is identical to
\captionsetup[subtable]{labelformat=simple}
and sets the labelformat to “simple” for all sub-captions inside tables.
An example:
\usepackage[labelsep=quad,indention=10pt]{caption}
\usepackage[labelfont=bf,list=true]{subcaption}
\captionsetup[table]{textfont=it,position=top}
\subcaptionsetup[table]{textfont=sf}
indention=10pt,position=top,margin=0pt,font=small,
labelformat=parens,labelsep=space,skip=6pt,hypcap=false,
labelfont=bf,list=true,textfont=sf .
4
Its syntax is:
\subcaptionbox[⟨list entry⟩]{⟨heading⟩}[⟨width⟩][⟨inner-pos⟩]{⟨contents⟩}
\subcaptionbox*{⟨heading⟩}[⟨width⟩][⟨inner-pos⟩]{⟨contents⟩}
The arguments ⟨list entry⟩ & ⟨heading⟩ will be used for typesetting the \caption.
⟨width⟩ is the width of the resulting \parbox; the default value is the width of the contents.
⟨inner-pos⟩ specifies how the contents will be justified inside the resulting \parbox; it can be
either ‘c’ (centered, using \centering), ‘l’ (left-justified, using \raggedright), ‘r’ (right-
justified, using \raggedleft), or ‘s’ (for no special justification). The default is ‘c’. (But
you can use any justification defined with \DeclareCaptionJustification as well, e.g.:
‘centerlast’)
When using \subcaptionbox, the baseline of the resulting box will be placed right
between contents and heading. So usually you don’t have to care about the vertical align-
ment of the sub-figures for yourself. Also the hyperlink anchor is placed properly with
respect to the hypcap= setting.
An example:
\begin{figure}
\centering
\subcaptionbox{A cat\label{cat}}
{\includegraphics{cat}}
\subcaptionbox{An elephant\label{elephant}}
{\includegraphics{elephant}}
\caption{Two animals}\label{animals}
\end{figure}
As you see the result is not satisfying; the caption below the cat looks ugly because of
the small width of the graphic. This can be solved by using the optional arguments of
\subcaptionbox, increasing the width of the resulting box:
2 The pictures were taken with permission from the LATEX Companion[1] examples.
5
...
\subcaptionbox{A cat\label{cat}}
[2.5cm]{\includegraphics{cat}}
...
Furthermore the main caption, which is centered with respect to the \textwidth, looks
mis-aligned with respect to the sub-captions. This can (again) be solved by using the op-
tional arguments of \subcaptionbox, giving both boxes the same width, for example:
...
\subcaptionbox{A cat\label{cat}}
[.4\textwidth]{\includegraphics{cat}}%
\subcaptionbox{An elephant\label{elephant}}
[.4\textwidth]{\includegraphics{elephant}}
...
6
3.1 Comparison with \captionbox
Both, \captionbox (offered by the caption package) and \subcaptionbox, put its
contents and caption into a \parbox of either natural or given width and share the same
(mandatory and optional) arguments, but while \captionbox uses a regular caption,
\subcaptionbox uses a sub-caption instead, like “(a)” or “2.1”.
So for example the last example would look like this when using \captionbox instead
of \subcaptionbox:
\begin{figure}
\centering
\captionbox{A cat\label{cat}}
[.4\textwidth]{\includegraphics{cat}}%
\captionbox{An elephant\label{elephant}}
[.4\textwidth]{\includegraphics{elephant}}
\end{figure}
\begin{subcaptionblock}[⟨outer-pos⟩][⟨height⟩][⟨inner-pos⟩]{⟨width⟩}
...
\end{subcaptionblock}
The default value for ⟨outer-pos⟩ is either ‘b’ (if no main caption was typeset so far) or
‘t’ (if a main caption was already typeset), and the default value for ⟨inner-pos⟩ is ‘s’.
New feature Beside the ⟨outer-pos⟩ values of ‘c’, ‘t’, and ‘b’, the subcaption package also offers the
v1.2 values ‘T’ and ‘B’ additionally which align the contents at the very top or bottom. (In
contrast ‘t’ and ‘b’ align the contents at the top or bottom baseline.)
7
The same example as Figure 3, but this time using the subcaptionblock environment
instead of \subcaptionbox:
\begin{figure}
\centering
\begin{subcaptionblock}{.4\textwidth}
\centering
\includegraphics{cat}
\caption{A cat}\label{cat}
\end{subcaptionblock}%
\begin{subcaptionblock}{.4\textwidth}
\centering
\includegraphics{elephant}
\caption{An elephant}\label{elephant}
\end{subcaptionblock}%
\caption{Two animals}\label{animals}
\end{figure}
• You can override the setttings for a specific subcaption with a \captionsetup
inside the subcaptionblock, e.g.:
\begin{subcaptionblock}{.4\textwidth}
\centering
\includegraphics{owl}
\captionsetup{skip=3pt}
\caption{An owl}\label{owl}
\end{subcaptionblock}
• Just like figure or table, a subcaptionblock could have multiple captions,
e.g.:
\begin{subcaptionblock}{.4\textwidth}
\centering
\includegraphics{cat}
\caption{A cat}\label{cat}
8
\medskip
\includegraphics{elephant}
\caption{An elephant}\label{elephant}
\end{subcaptionblock}
• Hyperlinks targeted to this sub-figure will jump to the beginning of the sub-
captionblock, and not to the \caption inside it (if hypcap=true is set
for sub-captions). (See section 7.6: Where do hyperlinks jump?)
\begin{subcaptiongroup}
...
\end{subcaptiongroup}
\begin{subcaptiongroup*}
...
\end{subcaptiongroup*}
While this gives you great flexibility, it also offers you no help formatting its contents.
9
The same example as Figure 6, but this time using the subcaptiongroup environment
instead of \subcaptionblock:
\begin{figure}
\centering
\begin{subcaptiongroup}
\centering
\parbox[b]{.4\textwidth}{%
\centering
\includegraphics{cat}
\caption{A cat}\label{cat}}%
\parbox[b]{.4\textwidth}{%
\centering
\includegraphics{elephant}
\caption{An elephant}\label{elephant}}%
\end{subcaptiongroup}
\caption{Two animals}\label{animals}
\end{figure}
–or–
\begin{figure}
\centering
\begin{subcaptiongroup}
\centering
\begin{minipage}[b]{.4\textwidth}
\centering
\includegraphics{cat}
\caption{A cat}\label{cat}
\end{minipage}%
\begin{minipage}[b]{.4\textwidth}
\centering
\includegraphics{elephant}
\caption{An elephant}\label{elephant}
\end{minipage}
\end{subcaptiongroup}
\caption{Two animals}\label{animals}
\end{figure}
10
(a) A cat (b) An elephant
\DeclareCaptionSubType[⟨numbering scheme⟩]{⟨type⟩}
\DeclareCaptionSubType*[⟨numbering scheme⟩]{⟨type⟩}
For the environments figure & table, and all the ones defined with \Declare-
FloatingEnvironment offered by the newfloat package, this will be done automati-
cally, but for other environments (e.g. the ones defined with \newfloat offered by the
float package or \DeclareNewFloatType offered by the floatrow package) this has
to be done manually.
The starred variant provides the sub-caption numbering format ⟨type⟩.⟨subtype⟩ (for ex-
ample ‘1.2’) while the non-starred variant simply uses ⟨subtype⟩ (for example ‘a’).
Own numbering formats can be created by redefining \thesub⟨type⟩, e.g.:
\DeclareCaptionSubType*{figure}
\renewcommand\thesubfigure{\thefigure\alph{subfigure}}
\DeclareCaptionSubType*[arabic]{table}
\subcaptionsetup[table]{labelformat=simple,labelsep=colon}
11
will give you sub-captions in tables like these ones:
7 Cross Referencing
The macro \the⟨counter⟩ is not only responsible for the look of the ⟨counter⟩, but for
the look of the references typeset with \ref, too. References will be prefixed by LATEX
with the internal macro \p@⟨counter⟩.
\DeclareCaptionSubType will define both of them for sub-captions (e.g. sub-
figure and subtable), and as you have seen in the last section \Declare-
CaptionSubType will give you some options to control the internal (re-)definition
of \the⟨counter⟩ and \p@⟨counter⟩.
\thesubfigure For example \thesubfigure and \p@subfigure are (as default) internally defined
\p@subfigure as
\newcommand\thesubfigure{\alph{subfigure}}
\newcommand\p@subfigure{\thefigure}
so the label of sub-captions will look like ‘a’ (decorated by the selected label format),
while references will look like ‘1a’ since they are prefixed by \p@subfigure = \the-
figure.
After \DeclareCaptionSubType*[arabic]{figure}, \thesubfigure and
\p@subfigure will look like
\renewcommand\thesubfigure{\thefigure.\arabic{subfigure}}
\renewcommand\p@subfigure{}
But if you want detailed control on how the references will look like, the options of
\DeclareCaptionSubType are potentially not sufficient. In this case one need to
redefine these two macros on his/her own. Some examples:
If you want parentheses around the sub-figure part of the reference, so they will look like
‘1(a)’, you may get them this way:
\usepackage[labelformat=simple]{subcaption}
\renewcommand\thesubfigure{(\alph{subfigure})}
(Note: Since parens is the default label format you will get double parentheses in sub-captions
when not specifiying a different label format, e.g. simple.)
But if you want only a closing parenthesis, so references will look like ‘1a)’, but the
sub-captions itself should still look like ‘(a)’, this would be a possible solution:
12
\usepackage{subcaption}
\renewcommand\thesubfigure{\alph{subfigure})}
\DeclareCaptionLabelFormat{opening}{(#2}
\subcaptionsetup[figure]{labelformat=opening}
i Please note that you need to surround redefinitions of \p@⟨counter⟩ with \makeatletter and
\makeatother. See http://tex.stackexchange.com/questions/8351/ for de-
tails.
Since October 2019 \p@⟨counter⟩ could be redefined using \labelformat instead, dropping
the need to use \makeatletter and \makeatother. Furthermore not only prefixes could be
specified this way, instead the counter representation could be used at any position in the text. So
for example
\makeatletter
\renewcommand\p@subfigure{\thefigure}
\makeatother
(which prefixes the subfigure counter representation with the figure counter representation) could
be replaced by
\labelformat{subfigure}{\thefigure #1}
\subref{⟨key⟩}
\subref*{⟨key⟩} 3 .
So for example \ref{cat} gives the result ‘1a’ but \subref{cat} gives ‘a’.
Note: If the sub-caption was (re-)defined with the starred variant \DeclareCaptionSub-
Type*, both \ref and \subref usually gives the same result.
13
This is possible, too, by using the option subrefformat:
\captionsetup{subrefformat=⟨label format⟩}
This one will choose a label format (either a pre-defined one, or a one defined with
\DeclareCaptionLabelFormat) as decorative element to sub-references. The de-
fault one is simple which has no decorative elements but simply typeset the reference
as it is.
For example
\captionsetup{subrefformat=parens}
will result in references (typeset with \ref) like ‘1a’ but sub-references (typeset with
\subref) like ‘(a)’.
\phantomcaption
instead of \caption.
\phantomcaption is offered by the caption package since version 3.2 and does not
generate any output but increases the sub-figure or sub-table counter and gives you an
anchor for a \label command which can be placed after it.
An example:
\begin{figure}
\centering
\begin{subcaptiongroup}
\includegraphics{cat_with_a}
\phantomcaption\label{cat}
\includegraphics{elephant_with_b}
\phantomcaption\label{elephant}
\end{subcaptiongroup}
\captionsetup{subrefformat=parens}
\caption{Two animals: \subref{cat} a cat,
and \subref{elephant} an elephant}
\label{animals}
\end{figure}
14
(b)
(a)
\captionlistentry If you don’t want to give a sub-figure a caption (yet), because the picture itself already
contains the caption, or for some other reason, you could also use the command
\captionlistentry{⟨list entry⟩}
instead of \caption.
\captionlistentry is offered by the caption package since version 3.3 and (just
like \phantomcaption) does not generate any output but increases the sub-figure or
sub-table counter and gives you an anchor for a \label command which can be placed
after it. Additionally to \phantomcaption this command puts an entry into the list of
figures or tables.
An example:
\begin{figure}
\centering
\begin{subcaptiongroup}
\includegraphics{cat_with_a}
\captionlistentry{A cat}
\label{cat}
\includegraphics{elephant_with_b}
\captionlistentry{An elephant}
\label{elephant}
\end{subcaptiongroup}
\captionsetup{subrefformat=parens}
\caption{Two animals: \subref{cat} a cat,
and \subref{elephant} an elephant}
\label{animals}
\end{figure}
15
2. It puts an entry into the list of figures or tables
3. It finally typesets a caption
When put into a command or into an environment which either evaluates its content more
than once or does not like one of the first two actions (for whatever reason), the result
could be either an error message or an incorrect result, for example a sub-figure or sub-
table counter which was incremented more than once.
\captiontext In these cases the \caption command could be split into \phantomcaption which
performs step one only (or \captionlistentry which performs steps one and two),
and \captiontext which performs step three only. This way critical steps could
be out-sourced from the target command or environment, for example by prepending
\phantomcaption and using \captiontext inside.
The syntax of captiontext is
\captiontext[⟨number⟩]{⟨text of sub-caption⟩}
\captiontext*[⟨number⟩]{⟨text of sub-caption⟩}
\captiontext is offered by the caption package since version 3.6 and as opposite to
\caption it does not increase the sub-figure or sub-table counter and does not give you
an anchor for a \label command. It typesets the caption only, using existing counter
values unless a ⟨number⟩ is given explicitly.
For example code please take a look at section 8: Captions inside sub-figures.
\phantomcaption\label{this}
...
\phantomcaption
\label{that}
But when using the \subcaptionbox command, the \label should be placed inside
the caption text, e.g.:
16
\subcaptionbox{A description here\label{todo1}}
{Some content here}
...
\subcaptionbox[List-of-Figures entry]
{A description here\label{todo2}}
{Some content here}
caption
hypcap=false hypcap=true
subcaption
sub-caption figure or table
hypcap=false
(default setting)
sub-figure or sub-figure or
hypcap=true
sub-table sub-table
Remember: If you use the hypcap package[5], it controls the placement of the hyperlink
anchors, making the rules above invalid.
(See also the documentation of the caption package, sections about hyperref & hypcap.)
• The caption could already be part of the image. (This case is already handled in
section 7.3: Referencing sub-figures without sub-captions.)
• LATEX packages like overpic, stackengine, or tikz could be used. Note that the
commands or environments offered by these packages usually evaluate their con-
tent more than once, resulting in either errors or wrong reference counters. For
this reason \caption should not be used here, instead \phantomcaption or
17
\captionlistentry should be used outside the command or environment and
\captiontext should be used inside it. See also section 7.4: Typesetting sub-
captions without generating a (new) reference
• The options skip= and margin= could be used to place the caption onto the im-
age.
\usepackage{overpic}
...
\begin{figure}
\centering
\begin{subcaptiongroup}
\subcaptionlistentry{A cat}
\label{cat}
\begin{overpic}[width=60pt]{cat}
\put(40,34){\captiontext*{}}
\end{overpic}
\subcaptionlistentry{An elephant}
\label{elephant}
\begin{overpic}[width=140pt]{elephant}
\put(58,40){\captiontext*{}}
\end{overpic}
\end{subcaptiongroup}
\captionsetup{subrefformat=parens}
\caption{Two animals: \subref{cat} a huge cat,
and \subref{elephant} an elephant}
\end{figure}
(b)
(a)
If neither a reference to the sub-figures nor an entry in the List of Figures is needed, the
usage of \phantomcaption or \captionlistentry could be dropped. Since this
18
leaves \captiontext without a valid sub-figure number value it must be given to it
explicitly as optional argument.
Furthermore the usage of the subcaptiongroup environment could be dropped here,
instead it would be sufficient to replace \captiontext with \subcaptiontext.
(See section 10: Abbreviatory commands)
\usepackage{overpic}
...
\begin{figure}
\centering
\begin{overpic}[width=60pt]{cat}
\put(40,34){\subcaptiontext*[1]{}}
\end{overpic}
\begin{overpic}[width=140pt]{elephant}
\put(58,40){\subcaptiontext*[2]{}}
\end{overpic}
\caption{Two animals: A huge cat and an elephant}
\end{figure}
\usepackage{stackengine}
...
\begin{figure}
\centering
\begin{subcaptiongroup}
\subcaptionlistentry{A cat}
\label{cat}
\stackinset{l}{25pt}{b}{20pt}{\captiontext*{}}
{\includegraphics[width=60pt]{cat}}
\subcaptionlistentry{An elephant}
\label{elephant}
\stackinset{l}{80pt}{b}{60pt}{\captiontext*{}}
{\includegraphics[width=140pt]{elephant}}
\end{subcaptiongroup}
\captionsetup{subrefformat=parens}
\caption{Two animals: \subref{cat} a huge cat,
and \subref{elephant} an elephant}
\end{figure}
19
(b)
(a)
Figure 10: Two animals: (a) a huge cat, and (b) an elephant
If neither a reference to the sub-figures nor an entry in the List of Figures is needed, this
code could be simpified to:
\usepackage{stackengine}
...
\begin{figure}
\centering
\stackinset{l}{25pt}{b}{20pt}{\subcaptiontext*[1]{}}
{\includegraphics[width=60pt]{cat}}
\stackinset{l}{80pt}{b}{60pt}{\subcaptiontext*[2]{}}
{\includegraphics[width=140pt]{elephant}}
\caption{Two animals: A huge cat and an elephant}
\end{figure}
\usepackage{tikz}
...
\begin{figure}
\centering
\begin{subcaptiongroup}
\subcaptionlistentry{A cat}
\label{cat}
\begin{tikzpicture}
\node (cat) at (0,0)
{\includegraphics[width=60pt]{cat}};
\node at (0.1,-0.1) {\captiontext*{}};
\end{tikzpicture}
\subcaptionlistentry{An elephant}
\label{elephant}
\begin{tikzpicture}
\node (elephant) at (0,0)
20
{\includegraphics[width=140pt]{elephant}};
\node at (0.5,-0.1) {\captiontext*{}};
\end{tikzpicture}
\end{subcaptiongroup}
\captionsetup{subrefformat=parens}
\caption{Two animals: \subref{cat} a huge cat,
and \subref{elephant} an elephant}
\end{figure}
(b)
(a)
Figure 11: Two animals: (a) a huge cat, and (b) an elephant
If neither a reference to the sub-figures nor an entry in the List of Figures is needed, this
code could be simpified to:
\usepackage{tikz}
...
\begin{figure}
\centering
\begin{tikzpicture}
\node (cat) at (0,0)
{\includegraphics[width=60pt]{cat}};
\node at (0.1,-0.1) {\subcaptiontext*[1]{}};
\end{tikzpicture}
\begin{tikzpicture}
\node (elephant) at (0,0)
{\includegraphics[width=140pt]{elephant}};
\node at (0.5,-0.1) {\subcaptiontext*[2]{}};
\end{tikzpicture}
\caption{Two animals: A huge cat and an elephant}
\end{figure}
21
The same example as in section section 7.3: Referencing sub-figures without sub-
captions, but using the skip=. . . and margin=. . . options:
\begin{figure}
\centering
\subcaptionsetup[figure]
{skip=-28pt,slc=off,margin={25pt,0pt}}
\subcaptionbox{\label{cat}}
{\includegraphics[width=60pt]{cat}}
\subcaptionsetup[figure]
{skip=-60pt,slc=off,margin={80pt,0pt}}
\subcaptionbox{\label{elephant}}
{\includegraphics[width=140pt]{elephant}}
\captionsetup{subrefformat=parens}
\caption{Two animals: \subref{cat} a huge cat,
and \subref{elephant} an elephant}
\end{figure}
(b)
(a)
Figure 12: Two animals: (a) a huge cat, and (b) an elephant
9 Numbering
9.1 Pittfall #1: Using multiple main captions
When multiple main captions are used within a figure or table, and sub-captions are used
as well, how does the subcaption package know which sub-captions belong to which
caption, i.e. what is the main counter value for the sub-captions?
Let’s illustrate this problem with an example document:
\documentclass{article}
\usepackage{graphicx,subcaption}
\begin{document}
\begin{figure}
\centering
\includegraphics[width=4cm]{example-image-c}
\caption{Caption no. 1}
\bigskip
\subcaptionbox
22
{\label{fig:2a}}
{\includegraphics[width=2cm]{example-image-a}}
\subcaptionbox
{\label{fig:2b}}
{\includegraphics[width=2cm]{example-image-b}}
\caption{Caption no. 2}
\end{figure}
Look at sub-figures \ref{fig:2a} and \ref{fig:2b}.
\end{document}
It’s obvious that the sub-captions belong to 2nd caption, and therefore \ref{fig:2a}
will become “2a”, isn’t it? But since the subcaption package is only involved when using
LATEX commands either defined or patched by the caption or subcaption package, this is
what the caption package is aware of:
\usepackage{subcaption}
\begin{document}
\begin{figure}
\caption{Caption no. 1}
\subcaptionbox
{\label{fig:2a}}
{⟨unknown content⟩}
\subcaptionbox
{\label{fig:2b}}
{⟨unknown content⟩}
\caption{Caption no. 2}
\end{figure}
\end{document}
So from captions point of view it’s not easy to decide if the sub-captions belong to the 1st
or 2nd main caption since they are placed between them. (Note: The subcaption package
is only offering an user interface to the sub-caption feature of the caption package, and
therefore this decision is the responsibility of the caption package.)
But how does the caption package makes a decision? If in doubt, it rather clings to the
past than to the future, i.e. in this case it decides that the sub-captions belong to the 1st
caption and therefore the result of \ref{fig:2a} is not “2a” but “1a”. If the caption
package is unsure about its decision (like in this case), a warning will be issued:
How to fix it? Use \nextfloat to tell the caption package where the 2nd figure within
the figure environment starts:
\documentclass{article}
\usepackage{graphicx,subcaption}
\begin{document}
\begin{figure}
\centering
23
\includegraphics[width=4cm]{example-image-c}
\caption{Caption no. 1}
\bigskip
\nextfloat
\subcaptionbox
{\label{fig:2a}}
{\includegraphics[width=2cm]{example-image-a}}
\subcaptionbox
{\label{fig:2b}}
{\includegraphics[width=2cm]{example-image-b}}
\caption{Caption no. 2}
\end{figure}
Look at sub-figures \ref{fig:2a} and \ref{fig:2b}.
\end{document}
i Note that the \nextfloat command was introduced in caption package v3.6. Previ-
ous versions of the caption package have bound the decision to the position= setting
of the floating environment instead, i.e. sub-captions belonged to the caption above if
position=top was set and they belonged to the caption below if position=below
was set. While this would be beneficial in this case, there were several cases were it was
not and especially it was not always comprehensible to the user why his references got an
incorrect numbering.
Furthermore the new decision algorithm always succeeds if there is only one caption
within the figure or table (which is the case most of the time) while the old one did not.
If you still prefer the old decision algorithm (for example because you want to process an
already existing document), you need to specify the caption package version explicitly,
for example:
\documentclass{article}
\usepackage{caption}[=v3.5]
\usepackage{graphicx,subcaption}
...
\documentclass{article}
\begin{document}
\begin{figure}
Figure counter before caption: \thefigure
\caption{Some text}
Figure counter after caption: \thefigure
\end{figure}
\end{document}
24
But this happens if we use the subcaption package:
\documentclass{article}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\subcaptionbox{}{some content}
\subcaptionbox{}{some content}
Figure counter before caption: \thefigure
\caption{Some text}
Figure counter after caption: \thefigure
\end{figure}
\end{document}
Here the results are 1 and 1 for the counter values. Why?
Since the caption package assumes that there will be a \caption following \sub-
captionbox it decides that the sub-captions belong to the upcoming main caption and
therefore share the same main counter value which needs to be incremented before its
use. Therefore the first \subcaptionbox in the figure increments the figure counter
while the second one and especially the \caption does not.
Usually this is no problem at all but keep this in mind if you are doing tricky stuff with
the figure or table counter within figures or tables.
10 Abbreviatory commands
\subcaption As we have seen in sections section 8.1: Using the overpic package, section 8.2: Using
\phantomsubcaption the stackengine package, and section 8.3: Using the tikz package it’s sometimes inconve-
\subcaptionlistentry nient to use the commands and environments described so far.
\subcaptiontext For example it would be inconvenient to use captiongroup just for a single \caption-
text in this particular case:
...
\begin{overpic}[width=60pt]{cat}
\put(40,34)
{\begin{captiongroup*}
\captiontext*[1]{}
\end{captiongroup*}}
\end{overpic}
...
...
\begin{overpic}[width=60pt]{cat}
\put(40,34)
{\setcaptionsubtype*
25
\captiontext*[1]{}}
\end{overpic}
...
But this is still inconvenient when used many times, and therefore the subcaption package
defines several extra commands which are prefixed with \setcaptionsubtype*:
...
\begin{overpic}[width=60pt]{cat}
\put(40,34){\subcaptiontext*[1]{}}
\end{overpic}
...
12 Required packages
New feature Starting with version 1.4 the subcaption package requires at least version 3.1 of the cap-
v1.4 tion package and loads it automatically. (Older versions of the subcaption package have
required exactly the version of the caption package which was released with it.)
If you need to use a specific version of the caption package you need to load it before the
subcaption package, e.g.:
\usepackage[ . . . ]{caption}[=v3.5]
\usepackage[ . . . ]{subcaption}
26
Note that there are limitations if an older version of the caption package is used:
New feature Prior subcaption version 1.6 an error message was issued if the ‘subfigure’ or ‘subtable’
v1.6 counter was already defined by any of them. Since subcaption version 1.6 the ‘subfigure’
and ‘subtable’ counters will only be defined by the subcaption package if they are not
defined yet, but the usage of any of the commands or environments described in this
document will result in an error message “The counter sub. . . was defined by . . . ” if the
corresponding counter was not defined by the subcaption package. Equally the commands
or environments offered by the above document classes or packages should not be used
with counters defined by the subcaption package, this will result in either LATEX errors or
unwanted behaviour.
So the rule of thumb is: If the counter ‘subfigure’ or ‘subtable’ is defined by document
class or package xyz, then the corresponding commands and environments offered by the
document class or package xyz should be used.
An example document:
\documentclass{memoir}
\newsubfloat{table} % defines the ‘subtable’ counter
\usepackage{subcaption} % defines the ‘subfigure’ counter
\begin{document}
\begin{figure}
\subcaptionbox{...}{...} % <- This is fine
\caption{...}
\end{figure}
\begin{table}
\caption{...}
\subcaptionbox{...}{...} % <- This results in an error,
\end{table} \subtop should be used instead
\end{document}
27
14 Beyond this package
For a more advanced usage of the sub-caption feature of the caption package, please take
a look at the excellent keyfloat package[6] which provides the environments keysub-
figs, keysubtabs, and keysubfloats for typesetting sub-figures and sub-tables.
Furthermore the floatrow package[3] provides the subfloatrow environment for type-
setting sub-figures.
15 Thanks
I would like to thank Stephen Dalton who helped to make this package a better one.
References
[1] Frank Mittelbach and Michel Goossens:
The LATEX Companion (2nd. Ed.),
Addison-Wesley, 2004.
[2] Axel Sommerfeldt:
Customizing captions of floating environments,
2022/01/07
[3] Olga Lapko:
The floatrow package documentation,
2007/12/24
28
[10] Steven D. Cochran:
The subfig package,
2005/07/05
29