我在标记图形环境内的算法行时遇到了麻烦。例如,在下面的代码中,标签“\lnl{loop}”会出现编译错误。这在图形环境之外的算法中非常有效。
\documentclass{article}
\usepackage{algorithm2e}
\usepackage{caption}
\usepackage{subcaption}
\newcommand{\myalgorithm}{%
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\lnl{loop} \While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\end{algorithm}}
\begin{document}
\begin{figure}
\begin{subfigure}{.5\textwidth}
\myalgorithm
\caption{How to write algorithms}
\end{subfigure}% need this comment symbol to avoid overfull hbox
\caption{Main caption}
\end{figure}
\end{document}
这是我使用 latex 命令时遇到的错误:
[1
! Undefined control sequence.
<write> \newlabel{sub@loop}{{\thesubalgocf
}{\thepage }}
l.33 \end{document}
感谢您的帮助!
答案1
figure
您可以使用minipage
s 和来代替使用环境\captionof
,但现在您的对象不会浮动:
\documentclass{article}
\usepackage{algorithm2e}
\usepackage{caption}
\usepackage{subcaption}
\newcommand{\myalgorithm}{%
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\lnl{loop} \While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\end{algorithm}}
\begin{document}
\begin{center}
\begin{minipage}{.5\textwidth}
\centering
\myalgorithm
\captionof{subfigure}{How to write algorithms}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\centering
\myalgorithm
\captionof{subfigure}{How to write algorithms}
\end{minipage}%
\captionof{figure}{Main caption}
\end{center}
\end{document}