论文模版不建议使用algorithm的浮动环境,但是把算法嵌入到图片里面效果就不好看了,比如:
```
\begin{figure}
\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}
\caption{Euclid's algorithm}\label{euclid}
\end{figure}
```
出来效果是:
![](https://pics.latexstudio.net/data/images/202003/cf2a20f6fd79047.png)
还有一种效果倒是想要的,但是看起来比较麻烦:
```
\usepackage[ruled,norelsize]{algorithm2e}
\makeatletter
\newcommand{\removelatexerror}{\let\@latex@error\@gobble}
\makeatother
\begin{document}
\begin{figure}[!t]
\removelatexerror
\begin{algorithm}[H]
\caption{multiobjective DE}
initialize population $P = \left \{ X_{1}, ... , X_{N} \right \} $\;
\For( \emph{Evolutionary loop}){$g := 1$ to $G_{max}$}
{
Do things \;
Trim the population to size $N$ using nondominated sorting and diversity estimation \;
}
\end{algorithm}
\end{figure}
\end{document}
```
效果如下:
![](https://pics.latexstudio.net/data/images/202003/fd2f29cfc38ca7a.png)
如果直接在algorithm后面指定位置h
```
\begin{algorithm}[h]
\caption{An example for format For \& While Loop in Algorithm}
\begin{algorithmic}[1]
```
可以得到想要的结果,**但是这样可以去掉algorithm的浮动环境吗?**
我还尝试了直接在algorithm后面用H,但是算法的位置并没有紧跟文字,想知道是什么原因?
一周热门 更多>