# Subfigure labelling for a single figure

I have a single figure containing two plots. The two plots are already labelled within the figure using (a), (b).

So far I have been referencing the figures using the cleveref package as

\cref{fig:img1}(a)


However the (a) is not part of the reference link.

My attempt at a work around is to use the subfigure environment

\documentclass{article}
%\usepackage{jheppub}
\usepackage{subcaption}
\usepackage{cleveref}
\usepackage{graphicx}

\begin{document}

\begin{figure}
\centering
\begin{subfigure}[t]{0.47\textwidth} % contains the two plots in a single figure
\includegraphics[width=\textwidth]{./img.pdf}
\caption{}
\label{fig:imga}
\end{subfigure}
\begin{subfigure}[t]{0\textwidth} % the hidden unwanted image
\includegraphics[width=\textwidth]{./img.pdf}
\caption{}
\label{fig:imgb}
\end{subfigure}
\caption{Main caption here}
\label{fig:main}
\end{figure}

\end{document}


So now when I reference \cref{fig:imga} it returns figure 1a etc. The only problem other than the spacing between the figure and the caption is that I have the figure label (a) under the first figure and a floating (b) under the second unwanted image.

Using \caption*{} results in the labels not being counted, so the references just return the section number.

Ideally I would like to hide these labels, but not disable them as I want to use them in the references.

EDIT.

Using \phantomcaption in place of the two subfigure \caption{}'s has worked perfectly.

-
Instead use \phantomcaption command for the empty caption. –  percusse Mar 3 '13 at 15:10
Wow such an easy work around, yet this is the first time I have heard of the \phantomcaption command. I have used it for both as neither label was wanted. Thank you. –  Nick Rhodes Mar 3 '13 at 15:14
Not a direct answer to your question, but a suggestion: Could you break up the graphics file that currently contains the two subfigures into two separate graphics files? (While you're at it, you could get rid of the minuscule (a) and (b) labels in the plots, which are very easy to overlook. Assign "real" captions to each subfigure instead.) hat way, you'll gain a lot of flexibility, and assigning labels to the entire figure as well as to each subfigure (and creating cross-references to each entity) becomes almost trivially simple. –  Mico Mar 3 '13 at 15:15
I gave that a try, but I found it hard to get two images to sit perfectly inline with each other, even after I cropped the original into two separate images. –  Nick Rhodes Mar 3 '13 at 15:20
@NickRhodes Editing your answer into your question is not a good idea as this leaves this post unsolved. Can you write a self-answer separate from your question? This is perfectly fine here and you can also accept it two days afterwards. –  hpesoj626 Mar 9 '13 at 2:29
show 1 more comment

subcaption package offers a convenient \phantomcaption command to suppress the label generation on the subfloats.

\documentclass{article}
\usepackage{subcaption}
\usepackage{cleveref}
\usepackage{mwe} %<- For dummy images

\begin{document}

\begin{figure}
\centering
\begin{subfigure}[t]{0.47\textwidth} % contains the two plots in a single figure
\includegraphics[width=\textwidth]{example-image-a}
\phantomcaption
\label{fig:imga}
\end{subfigure}
\begin{subfigure}[t]{0\textwidth} % the hidden unwanted image
\includegraphics[width=\textwidth]{example-image-b}
\phantomcaption
\label{fig:imgb}
\end{subfigure}
\caption{Main caption here}
\label{fig:main}
\end{figure}

We can use \Cref{fig:imga} but we won't use \Cref{fig:imgb}

\end{document}


Then the result is as desired.

-

I found another solution. The key point is to change the format of \cref on the fly.

\documentclass{article}
\usepackage{cleveref}
\usepackage{mwe} %<- For dummy images

\begin{document}

\begin{figure}
\centering
\includegraphics[width=\textwidth]{example-image-a}
\caption{Main caption here}
\label{fig:main}
\end{figure}

The default format of \texttt{cref} looks like this \cref{fig:main}.
The format of the figure reference is now changed on the fly to be
\crefformat{figure}{fig.~#2#1{(a)}#3} % change the format to include (a)
\cref{fig:main} or
\crefformat{figure}{fig.~#2#1{(b)}#3} % change the format to include (b)
\cref{fig:main}
\crefformat{figure}{fig.~#2#1#3} % restore the default format

\end{document}


The result is:

As you can see (a) and (b) are included in the hyper links. To fix the spacing right before the 'fig.' string simply remove the breaks and comments, e.g.:

\crefformat{figure}{fig.~#2#1{(a)}#3}\cref{fig:main}\crefformat{figure}{fig.~#2#1#3}


The drawback of this solution is that the format is defined by hand. If decide to use

\usepackage[capitalise]{cleveref}


you will end up with a wrong format, e.g. 'fig.' instead of 'Fig.'. I have no idea how to solve this problem.

-