1

I am trying to write some pseudocode. How can I add two line to the algorithm at the top and the bottom as well as adding line number.

Code:

\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}
  • According to the manual: \SetNlsty{texttt}{}{} with \nl command before relevant algorithm line, but your example does not compile – user31729 Oct 2 '15 at 19:22
3

You can add \nl to a line to insert a line number that can be referenced. Additionally, if you want to add space before/after the algorithm environment, you can patch into the algorithm environment with some support from etoolbox:

enter image description here

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

Note the use of \; to end the line; algorithm2e uses this syntax. It also prints ;, but you can change this symbol if needed (issue \DontPrintSemicolon).

4

Since the provided 'MWE' is no WE (;-)) I used some dummy code from the algorithm2e manual:

The \nl command in front of the algorithm line will print the line number, the the style can be set with

\SetNlSty{font}{txt before}{txt after} command. The default is \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}

enter image description here

  • Please check my new code. Is it Possible to add one line at the top of the algorithm and one at the bottom? – Mr Asker Oct 2 '15 at 19:31
  • @MrAsker: I still can't compile your example. The \nl commands can be omitted when needed – user31729 Oct 2 '15 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.