# 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.

-

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:1a}
\end{subfigure}
\begin{subfigure}{0.2\textwidth}
\includegraphics[width=\linewidth]{example-image-b}
\caption{Image b}
\label{subfig:1b}
\end{subfigure}\\
\begin{subfigure}{0.2\textwidth}
\includegraphics[width=\linewidth]{example-image-c}
\caption{Image c}
\label{subfig:1c}
\end{subfigure}
\begin{subfigure}{0.2\textwidth}
\includegraphics[width=\linewidth]{example-image}
\caption{Image d}
\label{subfig:1d}
\end{subfigure}
\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}.\\
A pair can be referenced like figs.~\ref{subfig:1a} and \subref{subfig:1b}.
\end{document}


-