我是否可以使用包获取用 LaTeX 编写的算法并将其转换为要添加到图形列表中的图形(\listoffigures
)?
答案1
以下有两种可能性。
在这两种情况下,诀窍都是使用环境H
的浮点说明符algorithm
,以避免它浮动。这样您就可以将其放置在figure
环境中。
和algorithm2e
代码:
\documentclass{article}
\usepackage[ruled]{algorithm2e}
\begin{document}
\listoffigures
\bigskip
\begin{figure}[htbp]
\begin{algorithm}[H]
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\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\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\caption{My algorithm}
\end{figure}
\end{document}
输出:
和algorithmicx
代码:
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\begin{document}
\listoffigures
\bigskip
\begin{figure}[htbp]
\begin{algorithm}[H]
\caption{Euclid’s algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\caption{My algorithm}
\end{figure}
\end{document}
输出: