1

I want to cross-reference a chain of subfigures. I use the package cleveref in the preamble as follow:

\usepackage[capitalize,nameinlink]{cleveref}
\captionsetup[subfigure]{subrefformat=simple,labelformat=simple}
\renewcommand\thesubfigure{(\alph{subfigure})}
\newcommand\crefrangeconjunction{--}

This is what I wrote in the body as an example \cref{fig:rain,fig:snow,fig:sunny,fig:thunderstorm}. The result I expected to be was

Figs 1(a)-(d) indicates that....

However, this is what I got

Figs 1(a)-1(d) indicates that....

So my question is when you chain multiple subfigures from the same figure, how do you make only the first subfigure of the order shown with the figure number (Fig 1(a)), and the last subfigure only shows the subfigure name (alphabet letter like (d) in the example) without the figure number? Thank you very much, I have no background in this, and reading the manual is too complex for me, your knowledge will help me a lot.

2
  • I doubt cleveref is quite that clever. You culd do it yourself using \ref and \subref. Nov 21, 2022 at 14:15
  • Thank you for your feedback!! If the answer meets your requirement, please accept it by checking the green tickmark on left side of the answer. Dec 21, 2022 at 16:33

1 Answer 1

1

It requires to define the \crefrangemultiformat and the \crefrangelabelformat commands using \crefstripprefix.

\crefstripprefix takes two strings as arguments, and returns the second one with any common prefix stripped off.

a

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{subcaption}

\usepackage[capitalize,nameinlink]{cleveref}

\newcommand\crefrangeconjunction{--}

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

%***************************************** added  <<<<<<<<<<<<<<<
\crefrangelabelformat{figure}{#3#1#4--(#5\crefstripprefix{#1}{#2}#6}
\crefmultiformat{figure}{Figs.~#2#1\xdef\crefstripprefixinfo{#1}#3}%
{ and~(#2{\crefstripprefix{\crefstripprefixinfo}{#1}}#3}{}{}
%****************************************

\begin{document}
    
    \begin{figure}[htbp!]
        \begin{subfigure}{0.2\textwidth}%
            \caption{Rain}  \label{fig:rain}
        \end{subfigure}
        \begin{subfigure}{0.2\textwidth}
            \caption{Snow}\label{fig:snow}
        \end{subfigure}
        \begin{subfigure}{0.2\textwidth}%
        \caption{Sunny} \label{fig:sunny}
        \end{subfigure}
        \begin{subfigure}{0.3\textwidth}
            \caption{Thunderstorm}\label{fig:thunderstorm}
        \end{subfigure}
        \caption{Forecast}\label{fig:forecast}
    \end{figure}

        
    \cref{fig:rain,fig:snow,fig:sunny,fig:thunderstorm}
    
    \cref{fig:rain,fig:snow,fig:sunny}
    
    \cref{fig:rain,fig:snow}
    
    \cref{fig:rain}
    
    \end{document
1
  • Thank you very much. It works....I really appreciate your help. Nov 22, 2022 at 2:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .