# customizing cleveref for pair of subfigure references: [1a and b] instead of [1a and 1b]

I am using the cleveref package and I am very happy with it. However, I encountered one problem I wasn't able to solve. I am probably just not combining the syntax correctly. This is highly connected to the question already asked, here: Crossreference in Ref Range [(1a-1e) to (1a-e)] I am sorry if this is already answered.

The above thread explains how to reference ranges of subequations (or floats) like 1a to e, rather than the default 1a to 1e. I would like to have the same feature for a pair of references combined by an "and", namely: 1a and b rather than the default 1a and 1b.

Here is a MWE:

\documentclass{book}

\usepackage{cleveref}
\usepackage{subfig}
\crefrangelabelformat{figure}{#3#1#4--#5\crefstripprefix{#1}{#2}#6}
\crefmultiformat{figure}{figs.~#2#1#3}{ and~#2#1#3}{, #2#1#3}{ and~#2#1#3}

\begin{document}
\begin{figure}
\centering
\subfloat[][]{\rule{2.cm}{2.cm}\label{subfig:1a}}
\subfloat[][]{\rule{2.cm}{2.cm}\label{subfig:1b}}\\
\subfloat[][]{\rule{2.cm}{2.cm}\label{subfig:1c}}
\subfloat[][]{\rule{2.cm}{2.cm}\label{subfig:1d}}\\
\caption{\label{fig:1}my figures}
\end{figure}

Referencing a range works as wanted and gives figs. 1a-b: \cref{subfig:1a,subfig:1b,subfig:1c,subfig:1d}.\\
But, referencing a pair gives \cref{subfig:1a,subfig:1b}, rather than the desired figs. 1a and b.\\

\end{document}


I think combining \crefstripprefix together with \crefmultiformat{figure} (which I use in the MWE to remove the "()") could give me the desired result. But I am not able to get the correct syntax to make this work, since I am pretty unfamiliar with the deep-down latex syntax.

In your definition of \crefmultiformat you want to use \crefrefstripprefix in the second argument using a value from the first argument. This can be achieved as follows:

\crefmultiformat{figure}{figs.~#2#1\xdef\mycreffirstarg{#1}#3}%
{ and~#2{\crefstripprefix{\mycreffirstarg}{#1}}#3}{, #2#1#3}{ and~#2#1#3}


\documentclass{book}

\usepackage{cleveref}
\usepackage{subfig}

\crefrangelabelformat{figure}{#3#1#4--#5\crefstripprefix{#1}{#2}#6}
\crefmultiformat{figure}{figs.~#2#1\xdef\mycreffirstarg{#1}#3}%
{ and~#2{\crefstripprefix{\mycreffirstarg}{#1}}#3}{, #2#1#3}{ and~#2#1#3}

\begin{document}
\begin{figure}
\centering
\subfloat[][]{\rule{2.cm}{2.cm}\label{subfig:1a}}
\subfloat[][]{\rule{2.cm}{2.cm}\label{subfig:1b}}\\
\subfloat[][]{\rule{2.cm}{2.cm}\label{subfig:1c}}
\subfloat[][]{\rule{2.cm}{2.cm}\label{subfig:1d}}\\
\caption{\label{fig:1}my figures}
\end{figure}

Referencing a range works as wanted and gives figs.~1a--b: \cref{subfig:1a,subfig:1b,subfig:1c,subfig:1d}.

Also referencing a pair gives \cref{subfig:1a,subfig:1b}, which is
than the desired figs.~1a and~b.

\end{document}


Using the subcaption package instead of subfig will get you most of the way. subcaption provides a \subref command, which gives you only the subfigure index without the figure number.

\documentclass{article}
\usepackage{graphicx}
\usepackage{cleveref}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}{0.2\textwidth}
\includegraphics[width=\linewidth]{example-image-a}
\caption{Image a}
\label{subfig:foo}
\end{subfigure}
\begin{subfigure}{0.2\textwidth}
\includegraphics[width=\linewidth]{example-image-b}
\caption{Image b}
\label{subfig:bar}
\end{subfigure}\\
\begin{subfigure}{0.2\textwidth}
\includegraphics[width=\linewidth]{example-image-c}
\caption{Image c}
\label{subfig:baz}
\end{subfigure}
\begin{subfigure}{0.2\textwidth}
\includegraphics[width=\linewidth]{example-image}
\caption{Image d}
\label{subfig:qux}
\end{subfigure}
\caption{\label{fig:1}my figures}
\end{figure}
Referencing a range works as wanted and gives figs.~\ref{subfig:foo}--\subref{subfig:bar}. There is also \cref{subfig:foo,subfig:bar,subfig:baz,subfig:qux}.
A pair can be referenced like figs.~\ref{subfig:foo} to \subref{subfig:baz}.
\end{document}


• Of course, this workaround works, but the whole point of using cleveref is to avoid such manual specification of the names and labels. Feb 20, 2018 at 22:04
• Surely hard-coding 1a-b is cheating, at least! Well, the rest undermines the point of cleveref, but this bit undermines the point of using the \label-\ref system to boot.
– cfr
Feb 21, 2018 at 1:07
• @HugoRaguet Yes, but I simply used the MWE provided by the OP and changed only the commands. I've edited the answer to remove the original hard-coded part as well.
– erik
Feb 21, 2018 at 22:01
• @erik: By manual specification, I was actually thinking of the "figs.", the "to" and the "\subref" parts. Otherwise, there is no use in using cleveref, as also mentioned by cfr. Feb 22, 2018 at 16:10