7.4 Sub-Floats
Some floats have sub-floats within them. For example, a figure may contain several sub-figures, each of which requires a caption. The simplest way to do this is to use the subcaption package that provides the subfigure and subtable environments:
Within these environments, you can use \caption
to create a
sub-caption. (In addition to the main \caption
for the
containing figure or table environment.)
Note:
The subcaption package requires the caption package, but doesn't automatically load it, so you'll need to load both:
Example:
This is very similar to the side-by-side figures example from §7.1.1. Side-By-Side Figures.
\begin
{figure}[htbp]
\begin
{subfigure}[b]{0.5\linewidth
}
\centering
\includegraphics
{rectangle}
\caption
{Rectangle}\label
{fig:rectangle}
\end
{subfigure}%
%
\begin
{subfigure}[b]{0.5\linewidth
}
\centering
\includegraphics
{circle}
\caption
{Circle}\label
{fig:circle}
\end
{subfigure}%
\caption
{Two Shapes}
\label
{fig:shape}
\end
{figure}
This produces Figure 7.5. Elsewhere in the document, the figure and its components can be referenced:
\ref
{fig:shapes2} shows some shapes.
Figure~\ref
{fig:rectangle} shows a rectangle and
Figure~\ref
{fig:circle} shows a circle.
which produces the following text:
You can also reference just the sub-float using
which is analogous to \ref
, but only displays the sub-float
number without the number associated with its containing float.
Example:
\ref
{fig:shapes2}
shows: (\subref
{fig:rectangle}) a rectangle and
(\subref
{fig:circle}) a circle.
produces
Note:
The subfigure labels (a, b, etc) should typically be in italic [17]. This can be achieved with the caption package using:
where <format-name> is the name for this new format and <code> is the code used to format the label where #2 gets replaced by the reference number.
Once you have defined a new format, you can then use
to switch to that new format. For subfloats, <type> needs to be
set to sub. The second argument <options> is a
<key>=<value> comma-separated list. The key that sets the format is
labelformat. (For further details about
both \DeclareCaptionLabelFormat
and \captionsetup
, see
the caption package documentation.)
For example, to create a format called em-noparens that displays the number in an emphasized font without parentheses:
Now switch to that new format:
Note that this only changes the caption label format. It doesn't affect the font
used by \ref
or \subref
. For \ref
, you can use the
fncylab package, which provides the command:
Within <defn>, use #1 to represent the subfigure value
and use \thefigure
for the encapsulating figure number. For
example:
Now
will produce
\subref
. Instead you will have
to do, for example, the following in the text:
If you want to add parentheses, the above can be modified to:
\DeclareCaptionLabelFormat
{em-parens}{(\emph
{#2})}
\captionsetup
[sub]{labelformat=em-parens}
\labelformat
{subfigure}{\thefigure
(\emph
{#1})}
For \subref
, you will have to do, for example, the following
in the text:
Exercise 18: Creating Sub-Figures
Download the image files rectangle.pdf and circle.pdf (or create your own images) and add Figure 7.5 to your document. You can download or view the solution.
This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-00-5).