编译错误:\sf@@@subfloat 的参数有一个额外的 }。它运行良好

编译错误:\sf@@@subfloat 的参数有一个额外的 }。它运行良好

我想修复编译错误,但我不知道该怎么做。我希望仍然可以使用标签。

我添加了如下代码:

\documentclass{article}
\usepackage[utf8]{inputenc}


\usepackage{subfig}
\usepackage{graphicx}

\begin{document}
some text \ref{fig:label0}\\
some text \ref{fig:label1}\\
some text \ref{fig:label2}\\
some text \ref{fig:label3}\\


\begin{figure}[h]%
\centering
\subfloat[{\centering description A}]{\includegraphics[width=0.33\textwidth]{tmp_img.eps}
\label{fig:label1}}%
\subfloat[{\centering description B}]{\includegraphics[width=0.33\textwidth]{tmp_img.eps}
\label{fig:label2}}%
\subfloat[\centering description C]{\includegraphics[width=0.33\textwidth]{tmp_img.eps}
\label{fig:label3}}%
\caption{Main}
\label{fig:label0}%
\end{figure}

\end{document}

  Argument of \sf@@@subfloat has an extra }.

 <inserted text> 
                 \par  l.18 \label{fig:label1}}
 % I've run across a `}' that doesn't seem to match anything.
 For example, `\def\a#1{...}' and `\a}' would produce
 this error. If you simply proceed now, the `\par' that I've just
 inserted will cause me to report a runaway argument that might be the
 root of the problem. But if your `}' was spurious, just type `2' and
 it will go away.

 Runaway argument? {\centering description A}]{\@firstoftwo {\nobreak
 \@xcentercr }}\def \ETC. ! Paragraph ended before \sf@@@subfloat was
 complete. <to be read again> 
                    \par  l.18 \label{fig:label1}}
                         %
I suspect you've forgotten a `}', causing me to apply this control sequence to too much text.
How can we recover?
My plan is to forget the whole thing and hope for the best.

该代码的结果如下: 在此处输入图片描述

答案1

这不是 的问题。相反,您应该从子浮点描述中label删除。\centering

\documentclass{article}
\usepackage[utf8]{inputenc}


\usepackage{subfig}
\usepackage[demo]{graphicx}

\begin{document}
some text \ref{fig:label0}\\
some text \ref{fig:label1}\\
some text \ref{fig:label2}\\
some text \ref{fig:label3}\\


\begin{figure}[h]%
\centering
\subfloat[description A]{\includegraphics[width=0.33\textwidth]{tmp_img.eps}\label{fig:label1}}%
\hfill
\subfloat[description B]{\includegraphics[width=0.33\textwidth]{tmp_img.eps}\label{fig:label2}}%
\hfill
\subfloat[description C]{\includegraphics[width=0.33\textwidth]{tmp_img.eps}\label{fig:label3}}%
\caption{Main}
\label{fig:label0}%
\end{figure}

\end{document}

在此处输入图片描述

答案2

在当前的 LaTeX 版本中,问题中发布的文档运行时没有错误,但在旧版本中\centering很脆弱,因此您需要\protect按如下方式使用:

\documentclass{article}
\usepackage[utf8]{inputenc}


\usepackage{subfig}
\usepackage{graphicx}

\begin{document}
some text \ref{fig:label0}\\
some text \ref{fig:label1}\\
some text \ref{fig:label2}\\
some text \ref{fig:label3}% no\\

\begin{figure}[htp]%
\centering
\subfloat[{\protect\centering description A}]{\includegraphics[width=0.33\textwidth]{example-image}
\label{fig:label1}}%
\subfloat[{\protect\centering description B}]{\includegraphics[width=0.33\textwidth]{example-image}
\label{fig:label2}}%
\subfloat[\protect\centering description C]{\includegraphics[width=0.33\textwidth]{example-image}
\label{fig:label3}}%
\caption{Main}
\label{fig:label0}%
\end{figure}

\end{document}

相关内容