# Is it possible to make a reference to a subfigure to appear Figure 2(a) with cleveref?

I am new here, and also new to LaTeX, so apologies if I have included too much or too little!

I have been using the subfig package, and just today added in cleveref.

The subfig package labels my subfigures (a), (b) etc, and I have been using the \subref command to make the same appear in the text.

However, using \cref (or even \ref) in the text to refer to the figure number as well as subfigure letter, there are no parentheses. I would like my output to appear

figure 2(a)

figure 2a

I have been searching a while online, but I can't see how to do this. I see here that someone has had the opposite problem and needed to get rid of unwanted parentheses. I can't work out what I have done relative to what he's doing other than that I am using subfig.

Here's my code (more or less)

\documentclass{article}

\usepackage{graphicx}      % needed for including graphics e.g. EPS, PS
\usepackage{epstopdf}      % automatic conversion
\usepackage{grffile}       % allow fullstops in graphics file names
\usepackage[format=hang,singlelinecheck=0,font={sf,small},labelfont=bf]{caption, subfig}
% allow subfigures
\usepackage[noabbrev]{cleveref}      % reference object types automatically

\graphicspath{{figures/}}

\begin{document}

Here is some text and a figure

\begin{figure}

\begin{minipage}[tbh]{\linewidth}
\centering
\captionsetup{justification=raggedright}

\end{minipage}

\caption{\subref{fig:1a} to \subref{fig:1b} are plotted against height, and \subref{fig:1c} to \subref{fig:1d} against pressure}
\label{fig:1}

\end{figure}

Now I'd like to refer to the figure \ref{fig:1d}. And use cref: \cref{fig:1d}

\end{document}

-

Yes, it is possible. You need to set

\captionsetup[subfigure]{subrefformat=simple,labelformat=simple,listofformat=subsimple}
\renewcommand\thesubfigure{(\alph{subfigure})}


and to use \subref*. A complete example:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage[format=hang,singlelinecheck=0,font={sf,small},labelfont=bf]{subfig}
\usepackage{caption}
\usepackage[noabbrev]{cleveref}

\graphicspath{{figures/}}

\captionsetup[subfigure]{subrefformat=simple,labelformat=simple,listofformat=subsimple}
\renewcommand\thesubfigure{(\alph{subfigure})}

\begin{document}

\begin{figure}
\centering
\captionsetup{justification=raggedright}
\subfloat[text]{\label{fig:1b}\includegraphics{file2.eps}}\\
\subfloat[text]{\label{fig:1d}\includegraphics{file4.eps}}
\caption{\protect\subref{fig:1a} to \protect\subref{fig:1b} are plotted against height, and \protect\subref{fig:1c} to \protect\subref{fig:1d} against pressure}
\label{fig:1}
\end{figure}

Now I refer to the figure \subref*{fig:1d}. And use cref: \cref{fig:1d}

\end{document}


The demo option for graphicx simply replaces actual figures with black rectangles; do not use that option in your actual document.

By the way, I made some modifications to your code: to prevent errors, I \protected the \subref commands inside \caption. Also I removed the inner minipage since its use was not required in this particular example. I didn't load some packages that were not relevant for the issue discussed in this question.

-
Thanks very much Gonzalo, not only did you answer my question but you have given me other some useful hints too, and I have even moved my justification commands to the \captionsetup at the top to save me doing it every figure. I am not sure why I was using minipage - I just copied from a previous document, but I can't imagine why I would have needed it in the first place! –  FionaSmith Oct 3 '12 at 8:48
@FionaSmith You're welcome! I'm glad I could help. –  Gonzalo Medina Oct 3 '12 at 13:26