IEEEE trans模板中怎么使用algorithm2e

IEEEE trans模板中怎么使用algorithm2e

    本文主要记录如何在IEEEE trans模板中使用algorithm2e,避免踩坑,找不到解决方案。

1.注释掉该注释的

    在导入algorithm2e包之前,将不必要的包进行注释,否则会一直报错,还找不到原因。需要注释的包包括下面两个:

\usepackage{
    
    algorithm}
\usepackage{
    
    algorithmic}

    如果注释掉上面这两个包,你再导入algorithm2e包时,会冲突,一直报错。

2.导入algorithm2e包

    用下面这几行语句导入algorithm2e包,并且要注意一定要将下面4行代码写完整,不能只写第一行导入包,还要加上下面3行才行,否则也是会报错。

% \usepackage[ruled,norelsize,vlined,linesnumbered]{
    
    algorithm2e}

% \makeatletter
% \newcommand{
    
    \removelatexerror}{
    
    \let\@latex@error\@gobble}
% \makeatother

3.使用algorithm2e包写算法

    用如下的代码写算法,是将算法放在了\begin{figure} \end{figure}中。附上一个简单的例子代码:


\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}

    上述算法运行之后的效果如下所示:
在这里插入图片描述

当然,也可以为算法加上行号,或者Input、Output等重要内容,可以参照如下代码:

\begin{
    
    figure}[!t]
 \removelatexerror
\begin{
    
    algorithm}[H]
  \KwIn{
    
    this text}
  \KwOut{
    
    how to write algorithm with \LaTeX2e }
  initialization\;
  \While{
    
    not at end of this document}{
    
    
    read current\;
    \Repeat{
    
    this end condition}{
    
    
      do these things\;
    }
    \eIf{
    
    understand}{
    
    
      go to next section\;
      current section becomes this one\;
    }{
    
    
      go back to the beginning of current section\;
    }
    \Do{
    
    this end condition}{
    
    
      do these things\;
    }
  }
  \caption{
    
    How to write algorithms}
\end{
    
    algorithm}
\end{
    
    figure}

参考文献

  1. algorithm2e笔记,记录一下常规操作
  2. algorithm2ePDF文档
    3.科研神器Latex:algorithm2e算法常用技巧小结

猜你喜欢

转载自blog.csdn.net/weixin_43981621/article/details/124824092