1

Imagine I have the following figure

\begin{figure}[H]
\begin{minipage}{0.45\linewidth}
\begin{subfigure}[b]{\textwidth}
\centering
        \includegraphics[width=.7\linewidth]{example-image}
        \caption{}
        \label{fig1a}
    \end{subfigure}
    \quad
    \begin{subfigure}[b]{\textwidth}
    \centering
        \includegraphics[width=.7\linewidth]{example-image}
        \caption{}
        \label{fig1b}
    \end{subfigure}
\end{minipage}%
\begin{minipage}{0.55\linewidth}
    \begin{subfigure}[b]{0.5\textwidth}
    \centering
        \includegraphics[width=\linewidth]{example-image}
        \caption{}
        \label{fig1c}
    \end{subfigure}%
    \begin{subfigure}[b]{0.5\textwidth}
    \centering
        \includegraphics[width=\linewidth]{example-image}
        \caption{}
        \label{fig1d}
    \end{subfigure}
    \quad
    \begin{subfigure}[b]{0.5\textwidth}
    \centering
        \includegraphics[width=\linewidth]{example-image}
        \caption{}
        \label{fig1e}
    \end{subfigure}%
    \begin{subfigure}[b]{0.5\textwidth}
    \centering
        \includegraphics[width=\linewidth]{example-image}
        \caption{}
        \label{fig1f}
    \end{subfigure}
\end{minipage}
\caption{(a) A caption. (b) Another caption. (c) Yet another caption. (d) So many captions. (e) Really. (f) I'm done with captions.} 
\label{fig1}
\end{figure}

which gives

enter image description here

My question: is it possible to automatically label the subfigures so that I don't have to write (a), (b), ... (f) by hand on the figure caption, and keep their order (as set by the mini pages)?

In some sense, it would be nice to simply write the caption under each subfigure but make these captions appear, automatically labelled in the correct ordering, in the figure's main caption. Any ideas?

Edit: I saw this question, but it still doesn't fix the automatic ordering problem.

2
  • You already use \label, so one could use \ref or possibly \subref. although that seems like more work, not less. I take it you want something like \footnote to create the caption as you go. Commented Mar 11, 2022 at 15:27
  • @JohnKormylo Those are nice options, though I still have to manually write them in order. Ideally, a subcaption would be associated with a certain subfigure and automatically displayed (in the correct order) in the main caption.
    – sam wolfe
    Commented Mar 11, 2022 at 15:34

2 Answers 2

1

The creates a global macro (\thecaption) containing the accumulated subcaptions.

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

\newcommand{\thecaption}{}% reserve global name

\newcommand{\mysubcaption}[1]{% #1 = text
  \caption{}%
  \ifnum\value{subfigure}=1 
    \xdef\thecaption{(\thesubfigure)~#1}%
  \else
    \xdef\thecaption{\thecaption, (\thesubfigure)~#1}%
  \fi}

\begin{document}
\begin{figure}[ht]% friends don't let friends use H
\begin{minipage}{0.45\linewidth}
\begin{subfigure}{\textwidth}
\centering
        \includegraphics[width=.7\linewidth]{example-image}
        \mysubcaption{A caption}
    \end{subfigure}
    \vskip\floatsep
    \begin{subfigure}{\textwidth}
    \centering
        \includegraphics[width=.7\linewidth]{example-image}
        \mysubcaption{Another caption}
    \end{subfigure}
\end{minipage}%
\begin{minipage}{0.55\linewidth}
    \begin{subfigure}[b]{0.5\textwidth}
    \centering
        \includegraphics[width=\linewidth]{example-image}
        \mysubcaption{Yet another caption}
    \end{subfigure}%
    \begin{subfigure}[b]{0.5\textwidth}
    \centering
        \includegraphics[width=\linewidth]{example-image}
        \mysubcaption{So many captions}
    \end{subfigure}
    \vskip\floatsep
    \begin{subfigure}[b]{0.5\textwidth}
    \centering
        \includegraphics[width=\linewidth]{example-image}
        \mysubcaption{Really}
    \end{subfigure}%
    \begin{subfigure}[b]{0.5\textwidth}
    \centering
        \includegraphics[width=\linewidth]{example-image}
        \mysubcaption{I'm done with captions}
    \end{subfigure}
\end{minipage}
\caption{\thecaption.}% Note the period.
\end{figure}
\end{document}
1

You can do it with expl3.

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

\ExplSyntaxOn

\AddToHook{env/figure/begin}
  {
    \seq_gclear:N \g_samwolfe_ac_captions_seq
  }

\NewDocumentCommand{\delayedsubcaption}{m}
  {
    \int_gincr:N \g_samwolfe_ac_label_int
    \caption{ \label{ac@\int_to_arabic:n { \g_samwolfe_ac_label_int}} }
    \seq_gput_right:Nx \g_samwolfe_ac_captions_seq
      {
        \exp_not:N \protect
        (\exp_not:N \subref {ac@\int_to_arabic:n { \g_samwolfe_ac_label_int}})
        \exp_not:n { \nobreakspace #1 }
      }
  }

\NewExpandableDocumentCommand{\printsubcaptions}{}
  {
    \seq_use:Nn \g_samwolfe_ac_captions_seq { .~ }.
  }

\int_new:N \g_samwolfe_ac_label_int
\seq_new:N \g_samwolfe_ac_captions_seq

\ExplSyntaxOff

\begin{document}

\listoffigures

\section{Main}

\begin{figure}[htp]

\begin{minipage}{0.45\linewidth}
\begin{subfigure}[b]{\textwidth}
\centering
        \includegraphics[width=.7\linewidth]{example-image}
        \delayedsubcaption{A caption}
        \label{fig1a}
    \end{subfigure}

    \begin{subfigure}[b]{\textwidth}
    \centering
        \includegraphics[width=.7\linewidth]{example-image}
        \delayedsubcaption{Another caption}
        \label{fig1b}
    \end{subfigure}
\end{minipage}\hfill
\begin{minipage}{0.55\linewidth}
    \begin{subfigure}[b]{0.5\textwidth}
    \centering
        \includegraphics[width=\linewidth]{example-image}
        \delayedsubcaption{Yet another caption}
        \label{fig1c}
    \end{subfigure}%
    \begin{subfigure}[b]{0.5\textwidth}
    \centering
        \includegraphics[width=\linewidth]{example-image}
        \delayedsubcaption{So many captions}
        \label{fig1d}
    \end{subfigure}

    \begin{subfigure}[b]{0.5\textwidth}
    \centering
        \includegraphics[width=\linewidth]{example-image}
        \delayedsubcaption{Really}
        \label{fig1e}
    \end{subfigure}%
    \begin{subfigure}[b]{0.5\textwidth}
    \centering
        \includegraphics[width=\linewidth]{example-image}
        \delayedsubcaption{I'm done with captions}
        \label{fig1f}
    \end{subfigure}
\end{minipage}
\caption{\printsubcaptions}
\label{fig1}

\end{figure}

\end{document}

You can add whatever you want to the main caption. Disposable labels are created, but you can add your own for reference.

enter image description here

I changed your \quad to a blank line, which is “the right thing to do”.

You must log in to answer this question.

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