6

I want to create an environment to easily list a bunch of subfigures with their explanations. But the explanations should appear only in the caption of the complete figure, instead of the subfigure's caption.

In order to solve this problem, I devised an environment that uses a command to append to a macro the subfigure captions. However, when I try to refer to the labels (that are created on the fly) for the subfigures in the resulting caption, they all refer to the last subfigure. I used \expandafter to append to my collecting macro, so I suspect this might be the problem.


The corresponding MWE:

\documentclass{article}

\usepackage{calc}
\usepackage{subcaption}
\usepackage[demo]{graphicx}

%
% ===================== %
% smallSubfigExplained  %
% ===================== %
\newlength\sseWidth
\def\sseExplanationOutput{}
\newcounter{sseTotalCounter}
\newcounter{sseCounter}[sseTotalCounter]

\newenvironment{smallSubfigExplained}{%
    \stepcounter{sseTotalCounter}
    \def\sseExplanationOutput{}
}{%
    \caption[cap]{\sseExplanationOutput}
}
\newcommand\sseItem[1]{%    
    \stepcounter{sseCounter}
    \def\lblname{sse:\thesseTotalCounter-\thesseCounter}
    \expandafter\def\expandafter\sseExplanationOutput\expandafter{%
        \sseExplanationOutput
        \textbf{\subref{\lblname}})
        #1
    }
    \begin{subfigure}[b]{0.4\textwidth}
        \centering
            \rule{2cm}{2cm}
            \caption{}
            \label{\lblname}
    \end{subfigure}     
}

\begin{document}
\begin{figure}
    \centering
        \begin{smallSubfigExplained}
            \sseItem{subfig expl 1}
            \sseItem{second subfig expl}
        \end{smallSubfigExplained}
\end{figure}
\end{document}

Screenshot:

Screenshot

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. I would say it is obvious if all subfigures have the same label - you should receive a warning like multiple definition. Do you? – strpeter Apr 24 '14 at 14:09
2

The use of \def on \sseExplanationOutput was self-referential, which doesn't work with \def. Normally, you would use an \edef, but in this case, the \subref{} doesn't work in an \edef, so you need a \protected@edef:

\documentclass{article}

\usepackage{calc}
\usepackage{subcaption}
\usepackage[demo]{graphicx}

%
% ===================== %
% smallSubfigExplained  %
% ===================== %
\newlength\sseWidth
\def\sseExplanationOutput{}
\newcounter{sseTotalCounter}
\newcounter{sseCounter}[sseTotalCounter]

\newenvironment{smallSubfigExplained}{%
    \stepcounter{sseTotalCounter}
    \def\sseExplanationOutput{}
}{%
    \caption[cap]{\sseExplanationOutput}
}
\makeatletter
\newcommand\sseItem[1]{%    
    \stepcounter{sseCounter}
    \def\lblname{sse:\thesseTotalCounter-\thesseCounter}
    \protected@edef\sseExplanationOutput{%
        \sseExplanationOutput
        \textbf{\subref{\lblname}})
        #1
    }
    \begin{subfigure}[b]{0.4\textwidth}
        \centering
            \rule{2cm}{2cm}
            \caption{}
            \label{\lblname}
    \end{subfigure}     
}
\makeatother
\begin{document}
\begin{figure}
    \centering
        \begin{smallSubfigExplained}
            \sseItem{subfig expl 1}
            \sseItem{second subfig expl}
        \end{smallSubfigExplained}
\end{figure}
\end{document}

enter image description here

| improve this answer | |

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.