Skip to content

Algorithm2e Usage

This page demonstrates the usage of the algorithm2e package. The content comes from the official documentation.

Quick Example

Example 1

You must set \usepackage[options]{algorithm2e} before \begin{document} command. The available options are described in section 7.

The optional arguments [Hhtbp] works like those of figure environment. The H argument forces the algorithm to stay in place. If used, an algorithm is no more a floating object. Caution: algorithms cannot be cut, so if there is not enough place to put an algorithm with H option at a given spot, LaTeX will place a blank and put the algorithm on the following page.

Very Important: each line MUST end with \; only those with a macro beginning a block should not end with \;. Note then that you can always use the \; command in math mode to set a small space.

The caption works as in a figure environment, except that it should be located at the end of the algorithm.

tex
\documentclass{standalone}
\usepackage{xcharter-otf}
\usepackage[ruled]{algorithm2e}
\begin{document}
\begin{algorithm}[H]
  \SetAlgoLined
  \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}
\end{document}

Example 1

Example 2

tex
\documentclass{standalone}
\usepackage{xcharter-otf}
\usepackage[linesnumbered,lined,boxed,commentsnumbered]{algorithm2e}
\begin{document}
\begin{algorithm}[H]
  \SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
  \SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
  \SetKwInOut{Input}{input}\SetKwInOut{Output}{output}

  \Input{A bitmap $Im$ of size $w\times l$}
  \Output{A partition of the bitmap}
  \BlankLine
  \emph{special treatment of the first line}\;
  \For{$i\leftarrow 2$ \KwTo $l$}{
    \emph{special treatment of the first element of line $i$}\;
    \For{$j\leftarrow 2$ \KwTo $w$}{\label{forins}
      \Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}\;
      \Up$\leftarrow$ \FindCompress{$Im[i-1,]$}\;
      \This$\leftarrow$ \FindCompress{$Im[i,j]$}\;
      \If(\tcp*[h]{O(\Left,\This)==1}){\Left compatible with \This}{\label{lt}
        \lIf{\Left $<$ \This}{\Union{\Left,\This}}
        \lElse{\Union{\This,\Left}}
      }
      \If(\tcp*[f]{O(\Up,\This)==1}){\Up compatible with \This}{\label{ut}
        \lIf{\Up $<$ \This}{\Union{\Up,\This}}
        \tcp{\This is put under \Up to keep tree as flat as possible}\label{cmt}
        \lElse{\Union{\This,\Up}}\tcp*[h]{\This linked to \Up}\label{lelse}
      }
    }
    \lForEach{element $e$ of the line $i$}{\FindCompress{p}}
  }
  \caption{disjoint decomposition}\label{algo_disjdecomp}
\end{algorithm}
\end{document}

Example 2

Predefined Keywords

  • \KwIn{input}
  • \KwOut{output}
  • \KwData{input}
  • \KwResult{output}
  • \KwTo
  • \KwRet{[value]}
  • \Return{[value]}
  • \Begin{block inside}
  • \Begin(begin comment){block inside}
How default keywords are defined
latex
\SetKwInput{KwData}{Data}
\SetKwInput{KwResult}{Result}
\SetKwInput{KwIn}{Input}
\SetKwInput{KwOut}{Output}

\SetKw{KwTo}{to}
\SetKw{KwRet}{return}
\SetKw{Return}{return}

\SetKwBlock{Begin}{begin}{end}

\SetKwComment{tcc}{/*}{*/}
\SetKwComment{tcp}{//}{}

\SetKwIF{If}{ElseIf}{Else}{if}{then}{else if}{else}{endif}
\SetKwSwitch{Switch}{Case}{Other}{switch}{do}{case}{otherwise}{endcase}endsw
\SetKwFor{For}{for}{do}{endfor}
\SetKwFor{While}{while}{do}{endw}
\SetKwFor{ForEach}{foreach}{do}{endfch}
\SetKwAll{ForEach}{forall}{do}{endfall}
\SetKwRepeat{Repeat}{repeat}{until}

Comments

C Style (/* ... */)

  • \tcc*{right justified side comment}
  • \tcc{line(s) of comment}
  • \tcc*[r]{right justified side comment, ends the line (default)}
  • \tcc*[l]{left justified side comment, ends the line}
  • \tcc*[h]{left justified comment, without end line; useful with “if-then-else” macros for example}
  • \tcc*[f]{right justified comment, without end line; useful with “if-then-else” macros for example}

C++ Style (// ...)

  • \tcp{line(s) of comment}
  • \tcp*{right justified side comment}
  • \tcp*[r]{right justified side comment, ends the line (default)}
  • \tcp*[l]{left justified side comment, ends the line}
  • \tcp*[h]{left justified comment, without end line; useful with “if-then-else” macros for example}
  • \tcp*[f]{right justified comment, without end line; useful with “if-then-else” macros for example}