32

A misleading error message from the following code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subfig}

\begin{document}

\begin{figure}[!ht]
\centering%
\subfloat[]{
    \label{subfig1}
    \includegraphics[width=.4\linewidth]{fig1.pdf}%
}%
\qquad%
\subfloat[]{
    \label{subfig2}
    \includegraphics[width=.4\linewidth]{fig2.pdf}
}%
\label{myfig}
\caption{A caption for figures \subref{subfig1} and \subref{subfig2}.}
\end{figure}

\end{document}

gives

! Argument of \caption@ydblarg has an extra }.
<inserted text> 
                \par 
l.19 ...es \subref{subfig1} and \subref{subfig2}.}

?

What have I done wrong? I can't see any extra } anywhere.

2
  • 2
    You need to protect \subref, i.e. \protect\subref{subfig1} Feb 19, 2015 at 9:38
  • Aah. I have no idea what fragile commands and protect are, but now I know where to begin. Thanks very much @karlkoeller!
    – jozxyqk
    Feb 19, 2015 at 9:49

1 Answer 1

36

The easiest solution in these cases is to add \protect in front of the usual suspects; \subref is one of them because it has a *-variant.

\documentclass{article}
\usepackage{graphicx}
\usepackage{subfig}

\begin{document}

\begin{figure}[!ht]
\centering
\subfloat[]{%
    \label{subfig1}%
    \includegraphics[width=.4\linewidth]{fig1.pdf}%
}%
\qquad
\subfloat[]{%
    \label{subfig2}%
    \includegraphics[width=.4\linewidth]{fig2.pdf}%
}

\caption{A caption for figures \protect\subref{subfig1} and \protect\subref{subfig2}.}
\label{myfig}

\end{figure}

\end{document}

Note where I added/removed % at end of lines and the position of \label{myfig} after \caption.

This might be annoying if you have several appearances of \subref in moving arguments. In this case you can “robustify” the command:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{etoolbox} % for \robustify

\robustify{\subref}

\begin{document}

\begin{figure}[!ht]
\centering
\subfloat[]{%
    \label{subfig1}%
    \includegraphics[width=.4\linewidth]{fig1.pdf}%
}%
\qquad
\subfloat[]{%
    \label{subfig2}%
    \includegraphics[width=.4\linewidth]{fig2.pdf}%
}

\caption{A caption for figures \subref{subfig1} and \subref{subfig2}.}
\label{myfig}

\end{figure}

\end{document}

Another solution is to avoid subfig for subcaption, which is the path I'd recommend:

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

\begin{document}

\begin{figure}[!ht]
\centering
\subcaptionbox{\label{subfig1}}{%
    \includegraphics[width=.4\linewidth]{fig1.pdf}%
}%
\qquad
\subcaptionbox{\label{subfig2}}{%
    \includegraphics[width=.4\linewidth]{fig2.pdf}%
}

\caption{A caption for figures \subref{subfig1} and \subref{subfig2}.}
\label{myfig}

\end{figure}

\end{document}
7
  • Great answer, thanks! Can you explain why the \qquad line doesn't need a %?
    – jozxyqk
    Feb 19, 2015 at 10:25
  • 1
    @jozxyqk A space/end-of-line is ignored after a control sequence (whose name consists of letters).
    – egreg
    Feb 19, 2015 at 10:31
  • The \protect method also solved my problem with an equivalent error using inparaenum inside a caption - had to apply it to \begin, \end and each \item
    – Bamboo
    Jan 27, 2016 at 3:22
  • Another 'suspect' turned out to be the \linebreak. Using \protect\linebreak also helped.
    – hanna
    Jan 25, 2020 at 13:11
  • Using \protect also solves problems caused by hyperlink in captions.
    – Bromind
    Aug 13, 2020 at 16:05

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.