2

I have the following figure:

\begin{figure}[tb]
    \centering
    \subfloat[FS -- a]{%
        \includegraphics[width=0.5\textwidth]{Images/Chapter4/RQ2/updated/a.pdf}%
        \label{fig:fse}%
    }
    \subfloat[FS -- b]{%
        \includegraphics[width=0.5\textwidth]{Images/Chapter4/RQ2/updated/b.pdf}%
        \label{fig:fsp}%
    }\par
    \subfloat[FS -- c]{%
        \includegraphics[width=0.5\textwidth]{Images/Chapter4/RQ2/updated/c.pdf}%
        \label{fig:fsg}%
    }\par
    \subfloat[CL -- d]{%
        \includegraphics[width=0.5\textwidth]{Images/Chapter4/RQ2/updated/d.pdf}%
        \label{fig:cle}%
    }
    \subfloat[CL -- e]{%
        \includegraphics[width=0.5\textwidth]{Images/Chapter4/RQ2/updated/e.pdf}%
        \label{fig:clp}%
    }\par
    \subfloat[CL -- f]{%
        \includegraphics[width=0.5\textwidth]{Images/Chapter4/RQ2/updated/f.pdf}%
        \label{fig:clg}%
    }
    \label{fig:rub}
\end{figure}

I want to refer to the first three subfloats as 4.5(a-c). For that, I used ~\ref{fig:fse},\ref{fig:fsp},\ref{fig:fsg} but that gives me 4.5a,4.5b,4.5c

Do you have an idea how to fix that?

1 Answer 1

2

Here is an attempt using cleveref package.

  • A new macro \crefretainprefix is defined, emulating the \crefstripprefix.
  • The setting \crefrangelabelformat{subfigure}{...} is based on my previous answer.
  • Also I suppose \subfloat is defined by subcaption package.

In the following example, input

\Cref{fig:rub} contain \cref{fig:fse,fig:fsp,fig:fsg} and \cref{fig:cle,fig:clp,fig:clg}.

gives

Figure 1 contains figs. 1(a-c) and figs. 1(d-f).

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\usepackage{cleveref}

\makeatletter
\newcommand\crefretainprefix[2]{%
  \begingroup%
    \edef\@toksa{#1}%
    \edef\@toksb{#2}%
    \let\cref@acc\@empty%
    \@crefretainprefix%
    \cref@result%
  \endgroup}%

\def\@crefretainprefix{%
  \let\@iterate\relax%
  \def\accum@flag{0}%
  \let\@tempc\@tempb%
  \cref@poptok{\@toksa}{\@tempa}%
  \cref@poptok{\@toksb}{\@tempb}%
  \ifx\@tempa\@tempb\relax%
    \def\@iterate{\@crefretainprefix}%
    \ifx\cref@acc\@empty\relax%
      \let\cref@acc\@tempb%
    \else%
      \ifcat\@tempb\@tempc\relax%
        \ifcat\@tempb a\relax%
          \def\accum@flag{1}%
        \else%
          \expandafter\chardef\expandafter\@tempa%
            \expandafter=\expandafter`\@tempb\relax%
          \ifnum\@tempa>`/\relax%
            \expandafter\ifnum\@tempb<`:\relax%
              \def\accum@flag{1}%
            \fi%
          \fi%
        \fi%
      \fi%
      \def\@tempa{1}%
      \edef\cref@acc{\cref@acc\@tempb}%
    \fi%
  \else%
    \ifcat\@tempb\@tempc\relax\else%
      \edef\cref@acc{\cref@acc}%
    \fi%
    \edef\cref@result{\cref@acc}%
  \fi%
  \@iterate}%
\makeatother

\captionsetup[sub]{subrefformat=parens}
\crefrangelabelformat{subfigure}
  {\crefretainprefix{#1}{#2}(#3\crefstripprefix{#2}{#1}#4--#5\crefstripprefix{#1}{#2}#6)}

\begin{document}
\Cref{fig:rub} contain \cref{fig:fse,fig:fsp,fig:fsg} and \cref{fig:cle,fig:clp,fig:clg}.

\begin{figure}[tb]
    \centering
    \subfloat[FS -- a]{%
        \includegraphics[width=0.3\textwidth]{example-image-a}%
        \label{fig:fse}%
    }
    \subfloat[FS -- b]{%
        \includegraphics[width=0.3\textwidth]{example-image-b}%
        \label{fig:fsp}%
    }
    \subfloat[FS -- c]{%
        \includegraphics[width=0.3\textwidth]{example-image-c}%
        \label{fig:fsg}%
    }\par
    \subfloat[CL -- d]{%
        \includegraphics[width=0.3\textwidth]{example-image-a}%
        \label{fig:cle}%
    }
    \subfloat[CL -- e]{%
        \includegraphics[width=0.3\textwidth]{example-image-b}%
        \label{fig:clp}%
    }
    \subfloat[CL -- f]{%
        \includegraphics[width=0.3\textwidth]{example-image-c}%
        \label{fig:clg}%
    }
    \caption{text}\label{fig:rub}
\end{figure}

\end{document}

enter image description here

1
  • 1
    +1. Very nice indeed! Just to make \crefrangelabelformat{subfigure}{...} conform a bit more to cleveref's philosophy, you may want to replace the -- particle with \crefrangeconjunction (and run \providecommand\crefrangeconjunction{--} if you prefer -- over ` to `).
    – Mico
    Aug 16, 2020 at 7:23

You must log in to answer this question.

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