# How does one create, caption, label and refer to a subfigure?

So its a fairly simple question to a problem people normally experience. I have a rough idea of what to do, but I am looking for the correct way of doing things. So in the solution I will include what would be the bare minimum requirements I think would get this problem solved. I am looking for feedback and corrections though.

So please tell me, how does one go about creating subfigures within a figure. After that, caption it. Then label it for reference and then refer to it in the text?

-
 I am including this here since i can't answer my own question yet: \usepackage{graphicx} \usepackage{subfig} In the body the subfigure file is called a, b and so on. \begin{figure} \subfloat[caption for subfigure a \label{a}]{\includegraphics[options like trim, crop]{a.jpg}} \subfloat[caption for subfigure b \label{b}]{\includegraphics[options again]{b.jpg}} \caption{caption for the full figure} \label{whole} \end{figure} While I am in the body I refer to the full figure as \ref{whole} and then refer to the subfigures like \ref{a} and \ref{b} – Eshwar Oct 24 '11 at 8:16 You could edit your question for including code. – Stefan Kottwitz♦ Oct 24 '11 at 11:55

You can use the subfig package for this. For a brief guide on how to use it, including captioning, labeling and referencing, have a look at the excellent LaTeX WikiBook section covering it.

-

The subcaption package provides three ways to build your subfigures:

1) Using the \subcaption command inside, for example, minipages; the syntax:

\subcaption[<list entry>]{<document caption>}


2) Using the subfigure environment.

3) Using the \subcaptionbox command. The syntax:

\subcaptionbox[<list entry>]{<document caption>}[<width>][<inner-pos>]{<contents>}


where <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 (for \centering), l (for \raggedright), r (for \raggedleft), or s (for no special justification). The default is c.

Here's a little example, illustrating the three approaches, and the use of \ref and \subref to cross-reference the (sub)figures:

\documentclass{article}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\begin{minipage}[b]{.5\linewidth}
\centering
\rule{3cm}{1cm}% to simulate a figure
\subcaption{Test subfigure one}
\label{fig:sub11}
\end{minipage}%
\begin{minipage}[b]{.5\linewidth}
\centering
\rule{3cm}{1cm}% to simulate a figure
\subcaption{Test subfigure one}
\label{fig:sub12}
\end{minipage}
\caption{A figure with two subfigures}
\label{fig:testfig1}
\end{figure}

\begin{figure}
\begin{subfigure}[b]{.5\linewidth}
\centering
\rule{3cm}{1cm}% to simulate a figure
\subcaption{Test subfigure one}
\label{fig:sub21}
\end{subfigure}%
\begin{subfigure}[b]{.5\linewidth}
\centering
\rule{3cm}{1cm}% to simulate a figure
\subcaption{Test subfigure one}
\label{fig:sub22}
\end{subfigure}
\caption{Another figure with two subfigures}
\label{fig:testfig2}
\end{figure}

\begin{figure}
\centering
\subcaptionbox{Test subfigure one\label{fig:sub31}}[4cm]{\rule{3cm}{1cm}}%
\subcaptionbox{Test subfigure two\label{fig:sub32}}[4cm]{\rule{3cm}{1cm}}
\caption{Another figure with two subfigures}
\label{fig:testfig3}
\end{figure}

As we can see in Figure~\ref{fig:sub22}...

Subfigure~\subref{fig:sub32} of Figure~\ref{fig:testfig3} shows...

\end{document}


-
 thanks Gonzalo! Very thorough! Just missed how to do it with subfig package! – Eshwar Oct 25 '11 at 8:42 @Eshwar, strategically, subcaption and floatrow with its subfloatrow env. are superior to subfig (not maintained AFAIK) and to subfigure which is obsolete. – doctorate Apr 6 at 17:57

You can use the floatrow package like in Vertically align subfloats at the top while having subcaptions vertically aligned below the subfloats.

\documentclass{article}

\usepackage{tikz}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{floatrow}

\begin{document}

\begin{figure}
\ffigbox
{%
\begin{subfloatrow}
\ffigbox
{%
\begin{tikzpicture}
\draw circle (1.25cm) {};
\end{tikzpicture}%
}
{%
\subcaption{Small circle}%
\label{fig:c:sc}%
}
\ffigbox
{%
\begin{tikzpicture}
\draw circle (2cm) {};
\end{tikzpicture}%
}
{%
\subcaption{Big circle}%
\label{fig:c:bc}%
}
\end{subfloatrow}
}
{%
\caption{Circles}%
\label{fig:c}%
}
\end{figure}

See the circles in Figure~\ref{fig:c}. There is a small circle, \ref{fig:c:sc}, and a big circle, \ref{fig:c:bc}.

\end{document}


-