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.
Best Answer
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.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:Another solution is to avoid
subfig
forsubcaption
, which is the path I'd recommend: