I am having trouble labeling lines of algorithms that are
inside the figure environment. For example, in the following code
the label "\lnl{loop}" gives compilation error. This works perfectly
in algorithms outside the figure environment.
\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}
This is the error I get with the latex command:
[1
! Undefined control sequence.
<write> \newlabel{sub@loop}{{\thesubalgocf
}{\thepage }}
l.33 \end{document}
Thanks for your help!
Best Answer
Instead of using a
figure
environment, you could useminipage
s and\captionof
, but now your object won't float: