[Tex/LaTex] Combining cleveref and subref

cleverefsubcaption

I recently switched to using cleveref in my latex document. However, I have one case I still don't quite get right (or maybe it is just not covered in cleveref):

I use the subfigure-environment of the subcaptionpackage on a regular basis. In the caption of the main figure, I then use \subref{} to get only the label of the subfigure, e.g. (a) instead of 1a.
The main advantage of cleveref is that I can type \cref{fig} and get the result of fig.~\ref{fig}, but this does not work for a subreference, because \cref always produces the full label (1a).

Is there a way to reproduce fig.~\subref{fig:a} using cleveref as well? I.e. can I use \cref to create a reference than only uses the subfigure label without the mainfigure label?

Edit: here is a MWE illustrating the problem:

\documentclass{article}

\usepackage{mwe}
\usepackage{subcaption}
\usepackage{cleveref}

\begin{document}

\begin{figure}
    \centering
    \begin{subfigure}{.3\linewidth}
        \includegraphics[width=\linewidth]{image-a}
        \caption{Figure 1a}
        \label{fig:a}
    \end{subfigure}
    \begin{subfigure}{.3\linewidth}
        \includegraphics[width=\linewidth]{image-b}
        \caption{Figure 1b}
        \label{fig:b}
    \end{subfigure}
    \caption{Main figure caption}
    \label{fig}
\end{figure}

Using subcaption, I can link to \ref{fig}, \ref{fig:a} or to \subref{fig:a} alone.
Using cleverref, I can also link to \cref{fig} and to \cref{fig:a},
However, I cannot link to \subref{fig:a} alone.

\end{document}

This produces the following result:
enter image description here

Edit 2: To be extra clear: The point of using cleveref is to be able to write text references without the need to describe the nature of every label. But, if I can not create a subref version of the cref command, I am still stuck with manually typing those references, which, imho, makes not much sense. Either I use cleveref all the way, or not at all.
So the question is, can I create a \cref variant of \subref?

Best Answer

A work around is to define your own 'partially-clever' command by adding this to the preamble:

\newcommand{\csubref}[2]{\namecref{#1}~\subref{#1:#2}}

However this requires a strict labelling scheme of the type mainfigurelabel:subfigureindex so if the total float is labeled by fig as in your example subfigures must be labeled as fig:<your text here>. You then may reference the subfloat by \csubref{fig}{<your text here>}. This does not take into account other useful features of \cref like handling of multiple references. Also I would consider using the subfig package as cleverref supports it natively, it does however not solve your 'problem'. But see below for comments on that.

Your accordingly modified MWE:

\documentclass{article}

\usepackage{mwe}
\usepackage{subcaption}
\usepackage{cleveref}
\newcommand{\csubref}[2]{\namecref{#1}~\subref{#1:#2}}
\begin{document}

\begin{figure}
    \centering
    \begin{subfigure}{.3\linewidth}
        \includegraphics[width=\linewidth]{image-a}
        \caption{Figure 1a}
        \label{fig:a}
    \end{subfigure}
    \begin{subfigure}{.3\linewidth}
        \includegraphics[width=\linewidth]{image-b}
        \caption{Figure 1b}
        \label{fig:b}
    \end{subfigure}
    \caption{Main figure caption}
    \label{fig}
\end{figure}

Using subcaption, I can link to \ref{fig}, \ref{fig:a} or to \subref{fig:a} alone.
Using cleverref, I can also link to \cref{fig} and to \cref{fig:a},
However, I can link to \csubref{fig}{a} alone.

\end{document} 

However, in the MWE you descirbed I am still not 100% convinced of the use case, since either

  • In the captions it is redundant to refer to the figure again by using fig.
  • When the figure number is deduced from the context, so may be the talking of a figure, too, so reproducing fig. may not add substantially.

The fact that the cleverref package in combination with the subfig package do not do what you want i.e. having 'fig. a' somewhere in the text add to my notion that it is not really needed to do so.

I would consider simply styling the subref a way that makes it more naturally familiar to the reader what a or b mean by using \newcommand{\csubref}[1]{(\subref{#1})} which gives (a) or (b) and is directly associated with a subfigure caption (this is the normal behavior for the subfig package). I agree, however, that a single a appearing somewhere in the text, as is the case when you use \subref{fig:a}, is undesirable, as it is always prone to be confused with the indefinite article 'a'.