\caption@ydblarg 的参数有一个额外的 }

\caption@ydblarg 的参数有一个额外的 }

以下代码中的误导性错误消息:

\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}

给出

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

?

我做错了什么?我没看到任何额外内容}

答案1

\protect在这些情况下,最简单的解决方案是在常见的嫌疑人前面添加;\subref是其中之一,因为它有一个 *-变体。

\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}

请注意我%在行尾添加/删除的位置以及\label{myfig} \caption

如果您多次出现\subref移动参数,这可能会很烦人。在这种情况下,您可以“强化”命令:

\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}

另一个解决方案是避免subfigfor subcaption,这是我推荐的路径:

\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}

相关内容