# Captioning a figure with multiple subfigures all at once

I want to create a figure with subfigures, and then reference them from the caption of the main figure:

\begin{figure}
\begin{subfigure}
A figure
\caption{}
\label{a}
\end{subfigure}
~
\begin{subfigure}
B figure
\caption{}
\label{b}
\end{subfigure}
\end{figure}


This doesn't work as written, how do I do something similar?

-
The environment subfigure is provided by the package subfigure which is obsolete. You should use subcation instead. However if I use your code snippet it works well. –  Marco Daniel Apr 7 '13 at 19:40
Marco meant subcaption. –  Jubobs Apr 7 '13 at 19:42
weirdly enough, I'm using the package subcaption... which also provides the subfigure environment –  Andrew Spott Apr 7 '13 at 19:48
@AndrewSpott: The environment subfigure of subcaption has an mandatory argument. –  Marco Daniel Apr 7 '13 at 19:51
@AndrewSpott such misunderstandings would not occur if your question contained a complete document, which is why we always ask for examples to start with \documentclass and end with \end{document} showing all packages used. –  David Carlisle Apr 7 '13 at 19:53

The package subfigure provides the environment subfigure. I guess you are using it. This package is obsolete and you should use the package subfig or subcaption (recommended) (provides nearly the same environment).

However the following example works well:

\documentclass{article}
\usepackage{subfigure}
\usepackage{caption}
\begin{document}
\begin{figure}
\begin{subfigure}
A figure
\caption{}
\label{a}
\end{subfigure}
~
\begin{subfigure}
B figure
\caption{}
\label{b}
\end{subfigure}
\label{fig:TheWholeFigure}
\end{figure}

\end{document}


Using subcaption:

\documentclass{article}
\usepackage{caption}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\begin{subfigure}{.49\linewidth}
A figure
\caption{}
\label{a}
\end{subfigure}
\hfill
\begin{subfigure}{.49\linewidth}
B figure
\caption{}
\label{b}
\end{subfigure}

If you want to reference the whole image, use \ref{fig:TheWholeFigure}.