TransWikia.com

How to reference subfloats in a specific way

TeX - LaTeX Asked on December 18, 2020

I have the following figure:

begin{figure}[tb]
    centering
    subfloat[FS -- a]{%
        includegraphics[width=0.5textwidth]{Images/Chapter4/RQ2/updated/a.pdf}%
        label{fig:fse}%
    }
    subfloat[FS -- b]{%
        includegraphics[width=0.5textwidth]{Images/Chapter4/RQ2/updated/b.pdf}%
        label{fig:fsp}%
    }par
    subfloat[FS -- c]{%
        includegraphics[width=0.5textwidth]{Images/Chapter4/RQ2/updated/c.pdf}%
        label{fig:fsg}%
    }par
    subfloat[CL -- d]{%
        includegraphics[width=0.5textwidth]{Images/Chapter4/RQ2/updated/d.pdf}%
        label{fig:cle}%
    }
    subfloat[CL -- e]{%
        includegraphics[width=0.5textwidth]{Images/Chapter4/RQ2/updated/e.pdf}%
        label{fig:clp}%
    }par
    subfloat[CL -- f]{%
        includegraphics[width=0.5textwidth]{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?

One Answer

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
newcommandcrefretainprefix[2]{%
  begingroup%
    edef@toksa{#1}%
    edef@toksb{#2}%
    letcref@acc@empty%
    @crefretainprefix%
    cref@result%
  endgroup}%

def@crefretainprefix{%
  let@iteraterelax%
  defaccum@flag{0}%
  let@tempc@tempb%
  cref@poptok{@toksa}{@tempa}%
  cref@poptok{@toksb}{@tempb}%
  ifx@tempa@tempbrelax%
    def@iterate{@crefretainprefix}%
    ifxcref@acc@emptyrelax%
      letcref@acc@tempb%
    else%
      ifcat@tempb@tempcrelax%
        ifcat@tempb arelax%
          defaccum@flag{1}%
        else%
          expandafterchardefexpandafter@tempa%
            expandafter=expandafter`@tempbrelax%
          ifnum@tempa>`/relax%
            expandafterifnum@tempb<`:relax%
              defaccum@flag{1}%
            fi%
          fi%
        fi%
      fi%
      def@tempa{1}%
      edefcref@acc{cref@acc@tempb}%
    fi%
  else%
    ifcat@tempb@tempcrelaxelse%
      edefcref@acc{cref@acc}%
    fi%
    edefcref@result{cref@acc}%
  fi%
  @iterate}%
makeatother

captionsetup[sub]{subrefformat=parens}
crefrangelabelformat{subfigure}
  {crefretainprefix{#1}{#2}(#3crefstripprefix{#2}{#1}#4--#5crefstripprefix{#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.3textwidth]{example-image-a}%
        label{fig:fse}%
    }
    subfloat[FS -- b]{%
        includegraphics[width=0.3textwidth]{example-image-b}%
        label{fig:fsp}%
    }
    subfloat[FS -- c]{%
        includegraphics[width=0.3textwidth]{example-image-c}%
        label{fig:fsg}%
    }par
    subfloat[CL -- d]{%
        includegraphics[width=0.3textwidth]{example-image-a}%
        label{fig:cle}%
    }
    subfloat[CL -- e]{%
        includegraphics[width=0.3textwidth]{example-image-b}%
        label{fig:clp}%
    }
    subfloat[CL -- f]{%
        includegraphics[width=0.3textwidth]{example-image-c}%
        label{fig:clg}%
    }
    caption{text}label{fig:rub}
end{figure}

end{document}

enter image description here

Correct answer by muzimuzhi Z on December 18, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP