为伪代码添加行数和行号

为伪代码添加行数和行号

我正在尝试编写一些伪代码。如何在算法顶部和底部添加两行以及添加行号。

代码:

\documentclass{article}
%\usepackage{algorithmic}% http://ctan.org/pkg/algorithms
\usepackage[]{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{latitude, longitude, mac, route, direction}
%\KwResult{how to write algorithm with \LaTeX2e }
 initialize $list$ as an empty  list of integers. \\
%\While{not at end of this document}{
%read current\;
$list$ = Get stop Order from behaviour; \\
\eIf{list size > 0}{
  find the last stop name in the behaviour table of this mac;
}{
  find the closet stop of the producer current location; \\
  insert it into next table; \\
  find initializer; \\
  \eIf{initializer is available}{
     get the initializer stop name;  \\
     restore all stop name for this mac to the behaviour table;

  }

}
\caption{Detection algorithm.}
\end{algorithm}

\end{document}

答案1

您可以\nl在行中添加可引用的行号。此外,如果您想在环境之前/之后添加空格algorithm,您可以在algorithm环境中使用以下支持进行修补:etoolbox

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm2e,lipsum,etoolbox}

\AtBeginEnvironment{algorithm}{\addvspace{2\baselineskip}}
\AfterEndEnvironment{algorithm}{\addvspace{2\baselineskip}}

\begin{document}

\lipsum[1]

\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{latitude, longitude, mac, route, direction}
  %\KwResult{how to write algorithm with \LaTeX2e }
  initialize \texttt{list} as an empty  list of integers \;
  %\While{not at end of this document}{
  %read current\;
  \texttt{list} = Get stop Order from behaviour \;
  \eIf{\texttt{list} size $>$ 0}{
    find the last stop name in the behaviour table of this mac \;
  }{
    \nl find the closet stop of the producer current location \;
    insert it into next table \;
    find initializer \;
    \If{initializer is available}{
       get the initializer stop name \;
       restore all stop name for this mac to the behaviour table \;
    }
  }
  \caption{Detection algorithm.}
\end{algorithm}

\lipsum[2]

\end{document}

注意使用来\;结束该行;algorithm2e使用此语法。它还会打印;,但您可以根据需要更改此符号(问题\DontPrintSemicolon)。

答案2

由于提供的“MWE”不是 WE (;-)),我使用了algorithm2e手册中的一些虚拟代码:

算法行前面的命令\nl将打印行号,可以使用以下方式设置样式

\SetNlSty{font}{txt before}{txt after}命令。默认值为\SetNlSty{textbf}{}{}

\documentclass{article}
\usepackage[]{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\SetNlSty{texttt}{(}{)}
%\KwData{latitude, longitude, mac, route, direction}
% initialize $list$ as an empty  list of integers. \\
%$list$ = Get stop Order from behaviour; \\
%\eIf{list size > 0}{
%  find the last stop name in the behaviour table of this mac;
%}{
%  find the closet stop of the producer current location; \\
%  insert it into next table; \\
%  find initializer; \\
%  \eIf{initializer is available}{
%     get the initializer stop name;  \\
%     restore all stop name for this mac to the behaviour table;    
%  }
%} 
%}
\nl\KwIn{input data}
\nl\KwOut{output data}
\nl\tcc{a comment line in C-style}
\caption{Detection algorithm.}
\end{algorithm}

\end{document}

在此处输入图片描述

相关内容