2

Consider the following MWE:

\documentclass{article}

\usepackage[demo]{graphicx}
\usepackage{subcaption}
\usepackage{caption}
\begin{document}
\begin{figure}

\subcaptionbox{\label{fig:img1}}{%
 \includegraphics[width=0.5\columnwidth]{image.jpg}}%
\subcaptionbox{\label{fig:img2}}{%
 \includegraphics[width=0.5\columnwidth]{image.jpg}%
}
\caption{Caption}
\end{figure}

\end{document}

This works all fine, but what I wont to achieve is that the main caption automatically gives me the range of subfigures (never mind the subcaptions here). So instead of Figure 1 in this case, I want LaTeX to print Figure 1a–b (and if there are more subfigures, adapt accordingly).

So far, I redefined the caption by hand with \renewcommand{\thefigure}{\arabic{figure}a--b}, but I'd like to have this automated.

3

Patching into \subcaptionbox this can be achieved with:

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{caption}

\usepackage{etoolbox}
\newcommand*\MySubcaptionFirst{}
\newcommand*\MySubcaptionLast{}
\makeatletter
\newcommand*\MySubcaptionRange
  {%
    \MySubcaptionFirst
    \ifx\MySubcaptionLast\@empty
    \else
      --\MySubcaptionLast
    \fi
  }
\newcommand*\SubCaptionBoxPatch
  {%
    \ifx\MySubcaptionFirst\@empty
      \expandafter\def\csname the\@captype\endcsname
        {\arabic{\@captype}\MySubcaptionRange}%
      \edef\MySubcaptionFirst{\csname thesub\@captype\endcsname}%
    \else
      \edef\MySubcaptionLast{\csname thesub\@captype\endcsname}%
    \fi
  }
\apptocmd \caption@iiibox 
  {\SubCaptionBoxPatch} 
  {}
  {\GenericError{}{Patching failed}{}{}}
\makeatother
\begin{document}
\begin{figure}
  \subcaptionbox{\label{fig:img1}}
    {\includegraphics[width=0.45\columnwidth]{example-image-duck}}
  \subcaptionbox{\label{fig:img2}}
    {\includegraphics[width=0.45\columnwidth]{example-image-duck}}
  \subcaptionbox{\label{fig:img3}}
    {\includegraphics[width=0.45\columnwidth]{example-image-duck}}
  \caption{Caption}
\end{figure}

\begin{figure}
  \subcaptionbox{\label{fig:img1}}
    {\includegraphics[width=0.5\columnwidth]{example-image-duck}}%
  \subcaptionbox{\label{fig:img3}}
    {\includegraphics[width=0.5\columnwidth]{example-image-duck}}%
  \caption{Caption}
\end{figure}
\begin{figure}
  \subcaptionbox{\label{fig:img3}}
    {\includegraphics[width=0.5\columnwidth]{example-image-duck}}%
  \caption{Caption}
\end{figure}
\begin{figure}
  \caption{Caption}
\end{figure}
\end{document}

enter image description here enter image description here enter image description here enter image description here

  • Thanks, that does the trick as described in my question. After playing a bit with it, I realised that this gives me all kind of problems with my cleveref references in the text and with koma-scrip's besidecaption. I guess, an automated solution which would take care of all thins things would get too complex. – Simifilm Sep 18 '18 at 6:35
  • @Simifilm yes it changes the references. One could change that by redefining how the caption label is printed instead of the numbering format of the counter. Don't know the internals of besidecaption, so I would need some time to make out how to make it compatible with it (if possible -- well with a two run solution almost anything is possible). I don't think I'll invest that much time though. – Skillmon Sep 18 '18 at 7:03
  • @Simifilm can you provide an example with cleveref where it's messing up and an example wtih besidecaption in a follow up question? This way you might get answers which are automated and compatible with those two constructs. And I (might) have more spare time this evening, so you'd provide me with something to do :) – Skillmon Sep 20 '18 at 7:55
  • I posted a follow-up: tex.stackexchange.com/questions/451710/… – Simifilm Sep 20 '18 at 15:08
2

Here's one option which uses cleveref to format the label range. You only have to specify the first and last label you assigned to the subfigures.

subfigures

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{caption}
\usepackage{cleveref}

\crefrangeformat{figure}{#3#1#4--#5\crefstripprefix{#1}{#2}#6}
\newcommand{\subfigurecaption}[3]{%
    \begingroup
        \renewcommand{\thefigure}{\crefrange{#1}{#2}}
        \caption{#3}
    \endgroup
}

\begin{document}
\begin{figure}

\subcaptionbox{\label{fig:img1}}{%
 \includegraphics[width=0.5\columnwidth]{example-image-duck}}%
\subcaptionbox{\label{fig:img2}}{%
 \includegraphics[width=0.5\columnwidth]{example-image-duck}%
}
\subfigurecaption{fig:img1}{fig:img2}{Caption}
\end{figure}

\begin{figure}

\subcaptionbox{\label{fig:img3}}{%
 \includegraphics[width=0.3\columnwidth]{example-image-duck}}%
\subcaptionbox{}{%
 \includegraphics[width=0.3\columnwidth]{example-image-duck}}%
\subcaptionbox{\label{fig:img4}}{%
 \includegraphics[width=0.3\columnwidth]{example-image-duck}%
}
\subfigurecaption{fig:img3}{fig:img4}{Caption}
\end{figure}
\end{document}
  • Thanks, I knew that something along this line was possible, but can't this be even more automated? That\caption checks whether there are any subfigures and automatically gets redefined? – Simifilm Sep 17 '18 at 6:40
  • @Simifilm Maybe it could, but then you would end up with a completely custom way to specify figures because I do not know of any way to derive that from the standard input format. But maybe someone else will have an idea that would be a bit less invasive. – TeXnician Sep 17 '18 at 6:47
  • @TeXnician Idea? yes! Less invasive? no. – Skillmon Sep 17 '18 at 7:35
  • @Skillmon +1 nonetheless, because automation with TeX is always nice :) – TeXnician Sep 17 '18 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.