Algorithm 2 e Latex
Algorithm 2 e Latex
Contents
1 Introduction
4
4
7
5 Compatibility issues
16
17
7 The
7.1
7.2
7.3
7.4
The
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
18
18
18
19
19
19
19
19
19
20
20
20
20
author is very grateful to David Carlisle, one of the authors of the LaTeX Companion book, for his advices
Blais for his suggestions
David A. Bader for his new option noend
Gilles Geeraerts for his new command SetKwIfElseIf
Ricardo Fukasawa for the portuguese keywords
k Christian Icking for the german translation of keywords
Arnaud Giersch for his suggestions and corrections on SetKwComments
and the many users as Jean-Baptiste Rouquier for their remarks
Martin
8 Typesetting
21
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
21
21
22
22
22
23
23
24
24
24
24
25
27
27
28
30
10 The
10.1
10.2
10.3
10.4
10.5
10.6
10.7
10.8
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
30
30
30
31
31
32
32
33
33
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
at the
at the
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
beginning
end . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
34
34
34
35
35
35
38
39
39
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
40
40
41
43
44
46
.
.
.
.
.
.
.
.
.
.
13 Known bugs
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
46
Introduction
which gives
Data: this text
Result: how to write algorithm with LATEX2e
initialization;
while not at end of this document do
read current section;
if understand then
go to next section;
current section becomes this one;
else
go back to the beginning of current section;
end
end
Algorithm 1: How to write algorithms
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. It is used by \listofalgorithms as a reference name for the list of algorithms.
You can also use the title macro given with the package, but this macro doesnt insert an entry
in the list of algorithms.
3.1
\IncMargin{1em}
\begin{algorithm}
\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*[r]{\This linked to \Up}\label{lelse}
}
}
\lForEach{element $e$ of the line $i$}{\FindCompress{p}}
}
\caption{disjoint decomposition}\label{algo_disjdecomp}
\end{algorithm}\DecMargin{1em}
3.2
Algorithm: IntervalRestriction
while S 6= do
remove x from the list of T of maximal index
while |S ImSucc(x)| =
6 |S| do
for y S ImSucc(x) do
{ remove from V all the arcs zy : }
for z ImP red(y) M in do
remove the arc zy from V
N bSuccInS(z) N bSuccInS(z) 1
move z in T to the list preceding its present list
{i.e. If z T [k], move z from T [k] to T [k 1]}
N bP redInM in(y) 0
N bP redN otInM in(y) 0
S S {y}
AppendT oM in(y)
RemoveF romM in(x)
\begin{algorithm}
\DontPrintSemicolon
\KwData{$G=(X,U)$ such that $G^{tc}$ is an order.}
\KwResult{$G=(X,V)$ with $V\subseteq U$ such that $G^{tc}$ is an
interval order.}
\Begin{
$V \longleftarrow U$\;
$S \longleftarrow \emptyset$\;
\For{$x\in X$}{
$NbSuccInS(x) \longleftarrow 0$\;
$NbPredInMin(x) \longleftarrow 0$\;
$NbPredNotInMin(x) \longleftarrow |ImPred(x)|$\;
}
\For{$x \in X$}{
\If{$NbPredInMin(x) = 0$ {\bf and} $NbPredNotInMin(x) = 0$}{
$AppendToMin(x)$}
}
\nl\While{$S \neq \emptyset$}{\label{InRes1}
\nlset{REM} remove $x$ from the list of $T$ of maximal index\;\label{InResR}
\lnl{InRes2}\While{$|S \cap ImSucc(x)| \neq |S|$}{
\For{$ y \in S-ImSucc(x)$}{
\{ remove from $V$ all the arcs $zy$ : \}\;
\For{$z \in ImPred(y) \cap Min$}{
remove the arc $zy$ from $V$\;
$NbSuccInS(z) \longleftarrow NbSuccInS(z) - 1$\;
move $z$ in $T$ to the list preceding its present list\;
\{i.e. If $z \in T[k]$, move $z$ from $T[k]$ to
$T[k-1]$\}\;
}
$NbPredInMin(y) \longleftarrow 0$\;
$NbPredNotInMin(y) \longleftarrow 0$\;
$S \longleftarrow S - \{y\}$\;
$AppendToMin(y)$\;
}
}
$RemoveFromMin(x)$\;
}
}
\caption{IntervalRestriction\label{IR}}
\end{algorithm}
In this section, we will try to show you main macros and how you can use this package to suit
your need. Based on one example using most popular algorithms expressions, we will show you
how it can be configured to be display in pseudo-code, in python or in C.
The following code shows how is typeset the generic example well use in this section:
\Fn(\tcc*[h]{algorithm as a recursive function}){\FRecurs{some args}}{
\KwData{Some input data\\these inputs can be displayed on several lines and one
input can be wider than lines width.}
\KwResult{Same for output data}
\tcc{this is a comment to tell you that we will now really start code}
\If(\tcc*[h]{a simple if but with a comment on the same line}){this is true}{
we do that, else nothing\;
\tcc{we will include other if so you can see this is possible}
\eIf{we agree that}{
we do that\;
}{
else we will do a more complicated if using else if\;
\uIf{this first condition is true}{
we do that\;
}
\uElseIf{this other condition is true}{
this is done\tcc*[r]{else if}
}
\Else{
in other case, we do this\tcc*[r]{else}
}
}
}
\tcc{now loops}
\For{\forcond}{
a for loop\;
}
\While{$i<n$}{
a while loop including a repeat--until loop\;
\Repeat{this end condition}{
do this things\;
}
}
They are many other possibilities and customization possible that you have to
discover by reading the documentation.
}
To handle if condition, use a macro to be abble to change it according to language syntax,
in particular we will change it for python-style and c-style. We also define a function to write
algorithm as a recursive function. These macros are defined as:
\newcommand{\forcond}{$i=0$ \KwTo $n$}
\SetKwFunction{FRecurs}{FnRecursive}%
The algorithm 4 shows how algorithm is displayed in pseudo-code with default behaviour and
options boxed, commentsnumbered and longend set. Note that by default, lines are used to show
block of code. Note also that longend option makes package use special end keyword for each
command2 .
2 Default
10
The algorithm 5 shows how algorithm is displayed using automatic block display (new feature
since relase 5.0). To achieve this display, we only add following macros at start of the algorithm:
\AlgoDisplayBlockMarkers\SetAlgoBlockMarkers{begin}{end}%
\SetAlgoNoEnd
First one tells package to display blocks with keyword markers. Note that the definition of block
markers are the one by default. Last macro remove end keywords of commands to avoid a double
end (the one of block marker and the one of command).
The algorithm 6 shows how algorithm looks like with a python-style syntax. To achieve this
display, we need to make following changes before the algorithm:
\SetStartEndCondition{ }{}{}%
\SetKwProg{Fn}{def}{\string:}{}
\SetKwFunction{Range}{range}%%
\SetKw{KwTo}{in}\SetKwFor{For}{for}{\string:}{}%
\SetKwIF{If}{ElseIf}{Else}{if}{:}{elif}{else:}{}%
\SetKwFor{While}{while}{:}{fintq}%
\renewcommand{\forcond}{$i$ \KwTo\Range{$n$}}
\AlgoDontDisplayBlockMarkers\SetAlgoNoEnd\SetAlgoNoLine%
SetStartEndCondition is used to display alternatives and loops conditions according to python
syntax: it means a space before condition, and no space after since : marks end of condition.
Functions are defined with def in python, so we redefine \Fn macro. Range is a new macro for
range python function. Next are redefined For, If and While commands accordingly to python
syntax. Note that we do nothing for repeat-until command since it doesnt exist in python. For
condition is redefined to match python behaviour. At last we tell package to not display block, to
not display end keyword and to not print line according to python syntax.
The algorithm 7 shows how algorithm looks like with a c-style syntax. To achieve this display,
we need to make following changes before the algorithm:
\SetStartEndCondition{ (}{)}{)}\SetAlgoBlockMarkers{\{}{\}}%
\SetKwProg{Fn}{}{}{}\SetKwFunction{FRecurs}{void FnRecursive}%
\SetKwFor{For}{for}{}{}%
\SetKwIF{If}{ElseIf}{Else}{if}{}{elif}{else}{}%
\SetKwFor{While}{while}{}{}%
\SetKwRepeat{Repeat}{repeat}{until}%
\AlgoDisplayBlockMarkers\SetAlgoNoLine%
SetStartEndCondition set braces around conditions like in C. We want that each block is marked
with { at start and } at end, so we set it thanks to \SetAlgoBlockMarkers macro. \Fn is redefined
with no keyword since in C, name of function defines it. Then we redefin FnRecursive with its
type. Next, For, If, While and Repeat are redefined accordingly to C syntax. At last, we tell the
package to display block markers.
The algorithm 8 shows how algorithm looks like with a c-style syntax and a more compact way
to mark blocks. To achieve this display, we need to make following changes before the algorithm:
\SetStartEndCondition{ (}{)}{)}\SetAlgoBlockMarkers{}{\}}%
\SetKwProg{Fn}{}{\{}{}\SetKwFunction{FRecurs}{void FnRecursive}%
\SetKwFor{For}{for}{\{}{}%
\SetKwIF{If}{ElseIf}{Else}{if}{\{}{elif}{else\{}{}%
\SetKwFor{While}{while}{\{}{}%
\SetKwRepeat{Repeat}{repeat\{}{until}%
\AlgoDisplayBlockMarkers\SetAlgoNoLine%
If you look at algorithm 4, you can see that some command doesnt put a end. For example, it is
a case for a if followed by an else, same thing for a else if. In C, there is always an end marker.
So, to achieve our goal we need to use end marker of blocks. But we dont want displaying begin
marker as in algorithm 5 or algorithm 7. If begin block marker is set to empty, then nothing
is written (especially not a blank line). So we tell package to use block markers with an empty
marker for begin and a } for end. Now we have to tell package to write a { on the same line as
commands. This is achieve by redefining If, For, While and Repeat command.
11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
12
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
14
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
15
Compatibility issues
Compatibily with other packages improven by changing name of internal macros. Algorithm2e
can now be used with almost all package, as elsart, hermes, arabtex for example, if this last
is loaded after algorithm2e package. So, at this time, release 5.0has few known compatibility
problem with other packages. The packages or classes that are known to be not compatible with
algorithm2e package is:
ascelike
pstcol
Nevertheless, when use with some packages, some of their options cannot be used, or you need to
specify some particular options (as algo2e to change name of environment if algorithm is already
defined by the class), either from algorithm2e package or from the other packages.
hyperref if you want to compile in PdfLATEX, you must not use naturalnames option. Beware
this has changed from release 3 where you should use it!
article-hermes is not compatible with relsize used by algorithm2e package, so you have to use
norelsize option to get algorithm works with article-hermes class.
Note also that, if you use packages changing the way references are printed, you must define labels
of algorithm after the caption to ensure a correct printing. You cannot use \label inside a caption
without errors.
From release 4.0, some commands have been renamed to have consistent naming (CamlCase
syntax) and old commands are no more available. If you doesnt want to change your mind or use
old latex files, you have to use oldcommands option to enable old commands back. Here are these
commands:
\SetNoLine becomes \SetAlgoNoLine
\SetVline becomes \SetAlgoVlined
\Setvlineskip becomes \SetVlineSkip
\SetLine becomes \SetAlgoLined
\dontprintsemicolon becomes \DontPrintSemicolon
\printsemicolon becomes \PrintSemicolon
\incmargin becomes \IncMargin
\decmargin becomes \DecMargin
\setnlskip becomes \SetNlSkip
\Setnlskip becomes \SetNlSkip
\setalcapskip becomes \SetAlCapSkip
\setalcaphskip becomes \SetAlCapHSkip
\nlSty becomes \NlSty
\Setnlsty becomes \SetNlSty
\linesnumbered becomes \LinesNumbered
\linesnotnumbered becomes \LinesNotNumbered
16
7
7.1
7.2
compatibility issue
algo2e: changes the name of environment algorithm into algorithm2e and so allows to use the
package with some journal style which already define an algorithm environment. Changes
also the command name for the list of algorithms, it becomes \listofalgorithmes
endfloat: endfloat packages doesnt allow float environment inside other environment. So using
it with figure option of algorithm2e makes error. This option enables a new environment
algoendfloat to be used instead of algorithm environment that put algorithm at the end. algoendfloat environment make algorithm acting as endfloat figures. This option load endfloat
package, so it is required to have it.
18
norelsize: starting from this release (v4.00), algorithm2e package uses relsize package in order to
get relative size for lines numbers; but it seems that some rare classes (such as inform1.cls)
are not compatible with relsize; to have algorithm2e working, this option makes algorithm2e
not to load relsize package and go back to previous definition by using \scriptsize font for
lines numbers.
slide: require package color. To be used with slide class in order to have correct margins.
nokwfunc disable the setting in \KwFunction of procedures or functions name (see section 6) of
function and procedure environment. Useful if you use name of procedure or function that
cannot be a command name as a math display for example.
7.3
7.3.1
algorithm numbering
figure: algorithms are put in classical figures and so are numbered as figures and putted in the
\listoffigures.
dotocloa adds an entry in the toc for the list of algorithms. This option loads package tocbibind
if not already done and so list of figures and list of tables are also added in the toc. If you want
to control which ones of the lists will be added in the toc, please load package tocbibind
before package algorithm and give it the options you want.
7.4
7.4.1
code typesetting
blocks display
lines
lined: \SetAlgoLined becomes the default, see section 9.6 for explanations about the \SetAlgoLined
macros.
vlined: \SetAlgoVlined becomes the default, see section 9.6 for explanations about the \SetAlgoVlined
macros.
19
noline: \SetNoline becomes the default, see section 9.6 for explanations about the \SetNoline
macros.
block markers
displayblockmarkers \AlgoDisplayBlockMarkers becomes the default, see section 9.6 for explanations about the \AlgoDisplayBlockMarkers macro.
7.4.2
end keywords
longend the end keyword are longer and different for each macro. For example endif for a
if-then-else macro.
shortend the end keyword of the macros is just end (default).
noend the end keyword of the macros is not printed.
7.4.3
comments
scright (default) right justified side comments (side comments are flushed to the right)
scleft left justified side comments (side comments are put right after the code line)
fillcomment (default) end mark of comment is flushed to the right so comments fill all the width
of text
nofillcomment end mark of comment is put right after the comment
7.4.4
lines numbers
linesnumbered: lines of the algorithms are numbered except for comments and input/output
(KwInput and KwInOut). You must use \nllabel{label} to label thoses lines.
linesnumberedhidden: lines of the algorithms are numbered as linesnumbered but numbers are
not shown. \ShowLn and \ShowLnLabel{label} show the number on line they are put.
commentsnumbered: makes comments be numbered if numbering is active.
inoutnumbered: makes data input/output be numbered if numbering is active.
rightnl: put lines numbers to the right of the algorithm instead of left.
resetcount the line numbers are reset to 0 at the beginning of each algorithm (by default).
noresetcount the contreverse of the precedent. To reset the line counter to 0 do:
\setcounter{AlgoLine}{0}
algonl the line numbers will be prefixed with the number of the current algorithm. Take care
to set the caption of the algorithm at the beginning of the environnement, else you will have
the precedent algorithm number as the current one.
7.4.5
title of algorithms
Typesetting
Note that if you define macros outside the algorithm environment they are available in all the
document and, in particular, you can use them inside all algorithms without redefining them. Be
careful you cant use macros beginning a block outside an algorithm environment.
9.1
\; marks the end of a line. Dont forget it !. By default, it prints a ;. You can change this
with \DontPrintSemicolon.
\DontPrintSemicolon the ; are no more printed at the end of each line.
\PrintSemicolon prints a ; at the end of each line (by default)
\BlankLine prints a blank line. In fact puts a vertical space of one ex.
\Indp indents plus the text is shifted to the right.
\Indm indents minus the text is shifted to the left.
21
9.2
9.2.1
Algorithm environment are float environment. So you can use classical \caption, \listofalgorithms{,}
\label. If you want a title but not a caption (for example to not add an enter in the list of algorithm) you have \TitleOfAlgo{.} And if you want to name your algorithm and not number it,
you can change the reference of it by using \SetAlgoRefName{ref}:
\caption{thetitle} works as classical caption of figures. It inserts an entry in the list of algorithms. Should be the standard way to put title of algorithms.
\TitleOfAlgo{thetitle} prints: Algorithm n : thetitle in the typography and size defined by
\SetTitleSty. Puts a vertical space below.
Beware: \TitleOfAlgo doesnt insert an entry in the list of algorithms. So dont use
\TitleOfAlgo with \caption. Both increment the counter of the algorithms.
note:with the french option prints Algorithme n :
The following commands help you to define the style and the layout of the caption:
\SetAlgoCaptionSeparator{sep} which sets the separator between title of algorithms (Algorithm
1) and the name of the algorithm. By default its : and caption looks like Algorithm 2:
name but now you can change it by using for example which will give Algorithm 3.
name.
\AlCapSkip is the dimension of the distance between algorithm body and caption in plain and
boxed mode. You can change by hands or by using \SetAlCapSkip{0ex}.
\SetAlCapSkip{length} sets the lenght of \AlCapSkip) dimension between algorithm body and
caption.
\SetAlCapHSkip{length} sets the horizontal skip before Algorithm: in caption when used in
ruled algorithm.
22
\SetTitleSty{type style}{type size} sets the typography and size of the titles defined with
the macro \TitleOfAlgo{} (not with \caption).
\NoCaptionOfAlgo doesnt print Algorithm and its number in the caption. This macros is ONLY
active for algoruled or ruled algorithms and for the next algorithm. For example, it is
useful when the algorithm just describes a function and you only want to display the name
of the function in the caption.
\RestoreCaptionOfAlgo restores correct captions that was corrupted by a \NoCaptionOfAlgo
macro.
\SetAlgoCaptionLayout{style} sets global style of the caption; style must be the name of a
macro taking one argument (the text of the caption). Examples below show how to use it:
\SetAlgoCaptionLayout{centerline} to have centered caption;
\SetAlgoCaptionLayout{textbf} to have bold caption.
If you want to apply two styles in the same time, such as centered bold, you have to define you own macro and then use \SetAlgoCaptionLayout with its name. \AlCapFnt and
\AlCapNameFnt can change the font used in caption, beware of interactions between this
three commands.
Note that two length control the layout of ruled, algoruled, boxruled algorithms caption.
\interspacetitleruled and \interspacetitleboxruled are described section 9.6.
9.3
9.3.1
line numbering
labelling and numbering lines
AlgoLine is the counter used to number the lines. Its a standard counter, so LATEXcommands
works with it.
linesnumbered, linesnumberedhidden and commentsnumbered (see above section 7) are the options controlling auto-numbering of lines. You can also control this feature manually and precisely
with the following commands:
\LinesNumbered makes lines of the following algorithms be auto-numbered. This command corresponds to linesnumbered option.
\LinesNumberedHidden makes lines of the following algorithms be auto-numbered, but numbers
stay hidden. You have to use \ShowLn and \ShowLnLabel to see them. This command
corresponds to linesnumberedhidden option.
\LinesNotNumbered makes lines of the following algorithms no be auto-numbered.
\nllabel{label} macro for labelling lines when auto-numbering is active.
\nl numbers the line: must begin the line. You can use \label to label the line and reference it
further.
\lnl{label} numbers and labels the line : must begin the line. Do a Beware this has changed
from release 3\nl\label{label} in one time. Prefer to use a classical \label as it is more
readable.
\nlset{text} works as \nl except that the additional argument is the text to put at the beginning
of the line. This text becomes the reference if you label it and \ref will print it instead of
classical number.
\lnlset{text}{label} works for \nlset as \lnl for \nl. Prefer to use a classical \label as it
is more readable.
23
The following command allows you to change the way line numbers are printed:
\SetNlSty{<font>}{<txt before>}{<txt after>} defines how to print line numbers:
will print {<font> <txt bef> thelinenumber <txt aft>}.
By default \SetNlSty{textbf}{}{}.
\SetNlSkip{length} sets the value of the space between the line numbers and the text, by default
1em.
\SetAlgoNLRelativeSize{number} sets the relative size of line numbers. By default, line numbers are two size smaller than algorithm text. Use this macro to change this behavior. For example, \SetAlgoNlRelativeSize{0} sets it to the same size, \SetAlgoNlRelativeSize{1}
to one size smaller and \SetAlgoNlRelativeSize{1} to one size bigger.
Example below shows use of these macros:
\SetNlSty{texttt}{[}{]}
\SetAlgoNlRelativeSize{0}
[2]Input: input data
[2]
\SetNlSkip{0em}
[4]Output: output data
[4]
\nl\KwIn{input data}
[6]/* a comment line in C-style
[6]
\nl\KwOut{output data}
[8]repeat
[8]
\nl\tcc{a comment line in C-style}
[10]
fn Y1 ;
=
\nl\Repeat{\nl$e<\tau$}{
[12]
fn+1 fn fn1 ;
\nl$f_n\leftarrow Y_1$\;
[14]
e f2n ;
\nl$f_{n+1}\leftarrow f_n\times f_{n-1}$\;
[16]
[15]until
e < ;
\nl$e\leftarrow \frac{f_n}{2}$\;
[18]return
[18]
e
}
\nl\KwRet{$e$}
9.4
*/
math display
If you need to use math display to handle complex mathematics as matrix, using standard \[ \]
or $$ will not allow correct numbering and end line management. If you dont need line numbers,
there is no problem. If you want line numbers, please use algomathdisplay environment instead
of \[ \] or $$. It will work as standard math display but line spacing, line numbers, end line will
be managed correcly
9.5
9.5.1
standard styles
standard font shapes and styles
Almost every text in algorithm has his own style that can be customized. The following commands correspond to the different styles used by the package. They can be customized by using
corresponding \Set commands (see section 9.5.3)
\AlFnt is used at the beginning of the body of algorithm in order to define the fonts used for
typesetting algorithms. You can use it elsewhere you want to typeset text as algorithm
For example you can do to have algorithms typeset in small sf font. Default is nothing so
algorithm is typeset as the text of the document.
\KwSty{<text>} sets <text> in keyword type style.
24
25
\AlCapNameSty{<text>} sets <text> in caption name typography, that is the same used, together with \AlCapNameFnt to print the name of caption you set by calling \caption{name}.
More precisely it is printed as follow:
\AlCapNameSty{\AlCapNameFnt name}
which gives name. By default \AlCapNameSty is textnormal which means print in standard text.
\AlCapFnt{<text>} sets <text> in font shape of caption title, that is the same used, together
with \AlCapSty, to print Algorithm #:, more precisely it is printed as follow:
\AlCapSty{\AlCapFnt Algorithm #:}
which gives actually Algorithm #:. By default \AlCapFnt is \relax which means keep
text as it is.
\AlCapNameFnt{<text>} sets <text> in caption name typography, that is the same used, together with \AlCapNameSty to print the name of caption you set by calling \caption{name}.
More precisely it is printed as follow:
\AlCapNameSty{\AlCapNameFnt name}
which gives name. By default \AlCapNameFnt is \relax which means keep text as it is.
\ProcSty{<text>} sets <text> in procedure/function caption title typography, that is the same
used, together with \ProcFnt, to print Procedure, more precisely it is printed as follow:
\ProcSty{\ProcFnt Procedure}
which gives actually Procedure. By default \ProcSty is \AlCapSty.
\ProcNameSty{<text>} sets <text> in procedure name typography, that is the same used, together with \ProcNameFnt to print the name of caption you set by calling \caption{Proc(int
i)}. More precisely it is printed as follow:
\ProcNameSty{\ProcNameFnt Proc(}
which gives Proc(. By default \ProcNameSty is \AlCapNameSty which means print in
standard text.
\ProcArgSty{<text>} sets <text> in argument of procedure/function typography, that is the
same used, together with \ProcArgFnt, to print int i if \caption{Proc(int i)} was
called. More precisely it is printed as follow:
\ProcArgSty{\ProcArgFnt int i}
which gives actually int i. By default \ProcArgSty is \AlCapNameSty;
\ProcFnt{<text>} sets <text> in font shape of caption title, that is the same used, together
with \ProcSty, to print Procedure, more precisely it is printed as follow:
\ProcSty{\ProcFnt Procedure}
which gives actually Procedure. By default \ProcFnt is \relax which means keep text
as it is.
\ProcNameFnt{<text>} sets <text> in procedure/function name typography, that is the same
used, together with \ProcNameSty to print the name of caption you set by calling \caption{Proc(int
i)}. More precisely it is printed as follow:
\ProcNameSty{\ProcNameFnt Proc(}
which gives Proc(. By default \ProcNameFnt is \relax which means keep text as it is.
\ProcArgFnt{<text>} sets <text> in font shape of argument of procedure/environment caption,
that is the same used, together with \ProcArgSty, to print int i if \caption{int i} was
called. More precisely it is printed as follow:
\ProcArgSty{\ProcFnt int i}
which gives actually int i. By default \ProcArgFnt do nothing.
26
With the following commands you can customize the style and have the look you want for your
algorithms:
\SetAlFnt{<font>} define the fonts used for typesetting algorithms.
You have to give commands to set the font in argument. You can use it elsewhere you want to
typeset text as algorithm. For example you can do \SetAlFnt{\small\sf} to have algorithms
typeset in small sf font.
The next ones require to give in parameter name of a macro (whithout \) which takes one
argument. For example, \SetAlCapFnt{textbf} (see section 9.2.2) defines the default behaviour
of \AlCapFnt. If you want to do more complicated thing, you should define your own macro and
give it to \SetAlCapFnt or \SetAlCapNameFnt. Here are two examples:
\newcommand{\mycapfn}[1]{\tiny #1}\SetAlCapNameFnt{mycapfnt}
\newcommand{\mycapfn}[1]{\textsl{\small #1}}\SetAlCapNameFnt{mycapfnt}
Here is the complete list of these macros:
\SetKwSty{<font>} sets the Kw typography to <font> (by default: textbf ).
\SetFuncSty{<font>} sets the function typography (by default: texttt).
\SetArgSty{<font>} sets the argument typography (by default: emph).
\SetDataSty{<font>} sets the data typography (by default: textsf ).
\SetCommentSty{<font>} sets the comment text typography (by default: texttt).
\SetNlSty{<font>} sets the number line typography (by default: \relsize{-2})
\SetProcNameSty{<font>} sets caption typography of procedure and function environment (by
default the same as \AlCapSty{)}.
\SetProcArgSty{<font>} sets argument typography of procedure and function environment (by
default the same as \AlCapNameSty{)}.
\SetBlockMarkersSty{<font>} sets block markers typography (by default the same as \KwSty{)}.
9.5.4
The following commands allow to redefine Fnt macros. This ones requires to give directly commands that define the font shape you want. They works as \SetAlFnt{d}escribed above. For example you can do \SetAlCapFnt{\large\color{red}} to have Algorithm #: in caption printed
in large red font.
\SetAlCapFnt{<font>} sets the font used for {algorithm: } in caption of algorithm (default is
set to \relax).
27
\SetAlCapNameFnt{<font>} sets the font used by caption text. Default is \relax and text is
kept as it is.
\SetAlTitleFnt{<font>} sets the font used in \TitleOfAlgo command (default is set to \relax,
so text is kept as it is).
The next commands allow to redefine Sty macros for caption or title. As \Set commands of
basic font style (see section 9.5.3), they require a name of a command in argument, this command
have to take one argument, the text to be typeset. They should be combined with previous
commands to redefine display of caption or title. Examples of use:
\newcommand{\mycapsty}[1]{\textbf{\emph{#1}}}\SetAlCapNameSty{mycapsty}
caption will be print emphased and in bold face.
\SetAlCapNameFnt{\tiny} set font to tiny size.
if you combine \SetAlCapNameSty{mycapsty} and \SetAlCapNameFnt{\tiny} will give tiny
bold empased caption.
Now the commands:
\SetAlCapSty{<commandname>}: sets the command that will be used by \AlCapSty to define
style of Algorithm #: in caption. The argument is a name of a command (without \).
This command have to take one argument, the text to be formatted. Default is set to:
\SetAlCapSty{textbf}.
\SetAlCapNameSty{<commandname>}: sets the command that will be used by \AlCapNameSty
to define style of caption text. The argument is a name of a command (without \).
This command have to take one argument, the text to be formatted. Default is set to:
\SetAlCapSty{textnormal}.
\SetAlTitleSty{<commandname>} sets the command that will be used by \AlTitleSty to define
style of algorithm title given by \TitleOfAlgo (default is set to \SetAlTitleSty{textbf}).
Note that by combining Fnt and Sty macros you can define almost all styles easily. For
example, the last example above can be define in a simplier way that previously presented by
doing:
\SetAlCapNameSty{textsl}\SetAlCapNameFnt{\small}
9.6
\RestyleAlgo{style} change the layout of the algorithms as do options boxed, boxruled, ruled
and algoruled.
\RestyleAlgo{style} sets the style of the following algorithms to that given by this macro (plain,
boxed, ruled, algoruled) unlike those indicated in the options of the package (see options of
the package).
\SetAlgoVlined prints a vertical line followed by a little horizontal line between the start and
the end of each block. Looks like that :
\SetNoline Doesnt print vertical lines (by default). The block is marked with keywords such as
begin, end.
\SetAlgoLined prints vertical lines between bloc start-end keywords as begin, end.
\SetAlgoLongEnd acts like longend option.
\SetAlgoShortEnd acts like shortend option.
28
9.7
comments
10
Here are the english keywords predefined in the package. There are other language predefined
macros provided, such as french keywords, see section 12 for a list of other language keywords.
All these keywords are defined using macros provided by the package and described in section 11.
10.1
\KwIn{input}
\KwOut{output}
\KwData{input}
\KwResult{output}
10.2
10.3
comments
10.4
if-then-else macros
\If{condition}{then block}
\If(then comment){condition}{then block}
\uIf{condition}{then block without end}
\uIf(then comment){condition}{then block without end}
\lIf{condition}{thens line text}
\lIf(if comment){condition}{thens line text}
\ElseIf{elseif block}
\ElseIf(elseif comment){elseif block}
\uElseIf{elseif block without end}
\uElseIf(elseif comment){elseif block without end}
\lElseIf{elseifs line text}
\lElseIf(elseif comment){elseifs line text}
\Else{else block}
\Else(else comment){else block}
\uElse{else block without end}
31
10.5
10.6
\For{condition}{text loop}
\For(for comment){condition}{text loop}
\lFor{condition}{line text loop}
\lFor(for comment){condition}{line text loop}
\While{condition}{text loop}
\While(while comment){condition}{text loop}
\lWhile{condition}{line text loop}
\lWhile(while comment){condition}{line text loop}
\ForEach{condition}{text loop}
32
10.7
10.8
1. \SetKwInput{KwData}{Data}
\SetKwInput{KwResult}{Result}
\SetKwInput{KwIn}{Input}
\SetKwInput{KwOut}{Output}
2. \SetKw{KwTo}{to}
3. \SetKw{KwRet}{return}
\SetKw{Return}{return}
4. \SetKwBlock{Begin}{begin}{end}
5. \SetKwComment{tcc}{/*}{*/}
\SetKwComment{tcp}{//}{}
6. \SetKwIF{If}{ElseIf}{Else}{if}{then}{else if}{else}{endif}
7. \SetKwSwitch{Switch}{Case}{Other}{switch}{do}{case}{otherwise}{endcase}endsw
8. \SetKwFor{For}{for}{do}{endfor}
\SetKwFor{While}{while}{do}{endw}
\SetKwFor{ForEach}{foreach}{do}{endfch}
\SetKwAll{ForEach}{forall}{do}{endfall}
9. \SetKwRepeat{Repeat}{repeat}{until}
33
11
Note that all these macros verify if the keywords are already defined and do a renewcommand if
they are. So you can overload the default definitions of this package with your own.
11.1
\SetKwInput{Kw}{input} defines the macro \Kw{arg} which prints input followed by : in key
word typography, and behind the argument arg. Typically used to define macros such as
\Input{data} or \Output{result}. Note that arg will be shifted so that all the text is
vertically aligned and to the right of the :.
\SetKwInOut{Kw}{input} works as \SetKwInput{Kw}{input}. But the position of the : is fixed
and set by the longest keyword defined by this macro.
\ResetInOut{input} resets the position of the : for all macros defined previously by
\SetKwInOut{Kw}{input}. The new position is fixed depending on the size of the text
input given in argument.
11.2
\SetKw{Kw}{thetext} defines the macro \Kw which defines a keyword thetext and prints it in
keyword typography. It can take one argument: \Kw{arg}. If so, arg is printed in argument
typography. For example \Kw{thetext} could give: Kw thetext
\SetKwHangingKw{Kw}{thetext} defines a hanging keyword that should act like a combination
of \SetKwInput and \SetKw. In comparison with \SetKwInput, it doesnt print : at end
of keyword and line is numbering if linesnumbered is set.
For example \SetKwHangingKw{HData}{Data$\rightarrow$} could gives:
1
Data a list of data and a long description of this data to be sure that text requires
several lines to be printed;
\SetKwData{Kw}{thetext} defines the macro \Kw{w}hich defines a data text. Prints thetext in
data typography. Note that this macros can takes one argument as function macros.
\SetKwArray{Kw}{array} which defines an array keywords Kw called array and printed in DataSty
style when call with \Kw. It can be used with one argument which denotes the element index:
\Kw{n} prints array[n] with array in \DataSty and n in \ArgSty.
\SetKwBlock{Begin}{begin}{end} defines a macro \Begin{txt} which denotes a block. The
text is surrounded by the words begin and end in keyword typography and shifted to the
right (indented). In \Vline or \Line mode a straight vertical line is added.
\Begin(side text){text} gives also text in a block surrounded by begin and end, but side
text if put after the begin keyword. Combined with \tcc*[f] macro, it allows you to put
comments on the same line as begin.
You can also use alternativ \uBegin{txt} which acts as \Begin{txt} but without end.
Useful for example as a part separator that doesnt necessary need an end keyword.
\SetKwProg{Prog}{Title}{is}{end} Env is a block with Title (in CapSty style) at the beginning followed by args followed by is then text inside a block ended by end. If no end
is specified, nothing is written (no blank line is inserted). Useful to typeset function or prog.
For example:
\SetAlgoLined
1 Function afunc(i: int) : int is
\SetKwProg{Fn}{Function}{ is}{end}
2
return 0;
\Fn{afunc(i: int) : int}{return 0\;}
3 end
=
4 def afunc(i: int):
\SetKwProg{Def}{def}{:}{}
5
return 0;
\Def{afunc(i: int)}{return 0\;}
34
11.3
If you want describe the function by an algorithm, use instead function or procedure environment.
\SetKwFunction{KwFn}{Fn} defines a macro \KwFn{arg} which prints Fn in Function typography
and its argument arg in argument typography, surrounded by a pair of parentheses.
\SetKwFunction{Dothat}{Do that} defines the macro \DoThat{this}, which is equivalent
to \FuncSty{Do that(}\ArgSty{this}\FuncSty{)} which gives: Do that(this).
Note that you can also use it without arguments, it will be printed without (), example:
\SetKwFunction{Fn}{TheFunction} use as \Fn gives TheFunction.
Keywords (with or without arguments) and functions defined previously in normal text (not
in an algorithm environment) can be used outside an algorithm environment. You can use it
by typing \DoThat{toto} (for a function defined by \SetKwFunction{Dothat}{Do that}),
you will obtain Do That(toto).
11.4
to define comments
11.5
35
\lIf(comment){cond}{Thens text}
the same with comment.
\lElseIf{ElseIfs text}
ElseIfs text is written on the same line as else if. No endif is printed.
\lElseIf(comment){ElseIfs text}
the same with comment.
\lElse{Elses text}
Elses text is written on the same line as else. No endif is printed.
\lElse(comment){Elses text}
the same with comment.
\uIf{cond}{Thens text} (for uncomplete if)
defines a If block unterminated like in a \eIf block, i.e. dont print the endif or dont
put the little horizontal line in Vline mode (see examples below).
\uIf(comment){cond}{Thens text}
the same with comment.
\uElseIf{ElseIfs text} (for uncomplete elseif)
Same explanation as for \uIf but with else if.
\uElseIf(comment){ElseIfs text}
the same with comment.
\uElse{Elses text} (for uncomplete else)
Same explanation as for \uElseIf but with else.
\uElse{Elses text}
the same with comment.
\eIf{cond}{Thens text}{Elses text}
equivalent to the use of \uIf followed by \Else.
The macros which begin with a l (l as line) denote that the text passed in argument will be
printed on the same line while with the others the text is printed in a block and shifted. You
should put \; at the end of l macros.
The macros which begin with a u (u as uncomplete) denote that the text passed in argument
will be printed in a block not terminated by endif. They are useful to chain different alternatives.
The keywords then and else are automatically printed. cond is always printed in argument
typography just behind the keyword if.
All this macros can be combined with () and \Comment* macros to put comments after main
keywords as If, Else or ElseIf (see list of predefined keywords above and example below).
Some examples with \SetKwIF{If}{ElseIf}{Else}{if}{then}{else if}{else}{endif} the
default definition given in the package:
\SetAlgoVlined
1 if cond1 then
\eIf{cond1}{
2
a line;
a line\;
3
a line;
a line\;
=
4 else
}{
5
another line;
another line\;
6
another line;
another line\;
}
\SetAlgoNoLine
1 if cond2 then
\If{cond2}{
2
second if;
=
second if\;
3
second if;
second if\;
4 end
}
36
\lIf{cond4}{ok} \lElse{wrong}
\leIf{cond4}{ok}{wrong}
\SetAlgoVlined
\lIf{cond5}{cond5 true}
\uElseIf{cond51}{
cond 5 false\;
but cond51 true\;
}
\ElseIf{}{
all is wrong\;
\Return result52\;
}
\SetAlgoLined
\uIf{cond6}{
cond6 is ok\;
always ok\;
}
\uElseIf{cond62}{
choose result62\;
\Return result62\;
}
\Else{
all is wrong\;
do something else\;
}
2
3
1
2
3
4
5
6
7
1
2
3
4
5
6
7
8
9
10
37
if cond6 then
cond6 is ok;
always ok;
else if cond62 then
choose result62;
return result62;
else
all is wrong;
do something else;
end
11.6
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
\SetKwSwitch{Switch}{Case}{Other}{switch}{do}{case}{otherwise}{endcase}endsw defines
several macros to give a complete Switch-do-case-otherwise environment:
\Switch{iden}{switchs block}
\Switch(comment){iden}{switchs block}
\Case{cond}{Cases block}
\Case(comment){cond}{Cases block}
\uCase{cond}{Cases block}
\uCase(comment){cond}{Cases block}
\lCase{cond}{Cases text}
\lCase(comment){cond}{Cases text}
\Other{Otherwises block}
\Other(comment){Otherwises block}
\lOther{Otherwises text}
38
\lOther(comment){Otherwises text}
The keywords do and endsw are automatically printed. iden and cond are always printed in
argument typography just behind the keywords Switch, Case and Otherwise. Here is an example
with the default keywords:
\Switch{the value of T}{
1 switch the value of T do
\uCase{a value}{
2
case a value
do this\;
3
do this;
do that\;
4
do that;
}
5
case another value one line;
\lCase{another value}{one line}
6
case last value
\Case{last value}{
7
do this;
=
do this\;
8
break;
break\;
9
end
}
10
otherwise
\Other{
11
for the other values;
for the other values\;
12
do that;
do that\;
13
end
}
14 endsw
}
As for If-then-elseif-else-endif macro, you can use () to put comments after main keywords.
11.7
\SetKwFor{For}{for}{do}{endfor} defines a loop environment with stop-test done at the beginning of the loop.
\For{loops condition}{Fors text}
\For(comment){loops condition}{Fors text}
\lFor{loops condition}{Fors text}
\lFor(comment){loops condition}{Fors text}
The keywords do and endfor are automatically printed. The loop condition is printed in
argument typography. For example:
\SetAlgoLined
\ForAll{elements of $S_1$}{
remove an element e from $S_1$\;
put e in the set $S_2$\;
}
=
\lFor{i=1 \emph{\KwTo}max}{mark i}\;
\ForEach{$e$ in the set}{
put $e$ in ${\cal E}$\;
mark $e$\;
}
1
2
3
4
5
6
7
8
9
10
As for If-then-elseif-else-endif macro, you can use () to put comments after main keywords.
11.8
1
2
3
4
5
6
repeat
the text of the loop;
another line;
always in the loop;
until this stop condition;
repeat a one line loop until stop;
As for If-then-elseif-else-endif macro, you can use () to put comments after main keywords.
12
12.1
6. \Pour{condition}{bloc de la boucle}
\lPour{condition}{ligne de la boucle}
7. \Tq{condition}{bloc de la boucle}
\lTq{condition}{ligne de la boucle}
8. \PourCh{condition}{bloc de la boucle}
\lPourCh{condition}{ligne de la boucle}
9. \PourTous{condition}{bloc de la boucle}
\lPourTous{condition}{ligne de la boucle}
10. \Repeter{condition darret}{bloc de la boucle}
\lRepeter{condition darret}{ligne de la boucle}
Here we describe how they are obtained:
1. \SetKwInput{Donnes}{Donn
ees}
\SetKwInput{Res}{R
esultat}
\SetKwInput{Entree}{Entr
ees}
\SetKwInput{Sortie}{Sorties}
2. \SetKw{KwA}{`
a}
\SetKw{Retour}{retourner}
3. \SetKwBlock{Deb}{d
ebut}{fin}
4. \SetKwIF{Si}{SinonSi}{Sinon}{si}{alors}{sinon si}{alors}{finsi}
5. \SetKwSwitch{Suivant}{Cas}{Autre}{suivant}{faire}{cas o`u}{autres cas}{fin cas}fin dalternative
6. \SetKwFor{Pour}{pour}{faire}{finpour}
7. \SetKwFor{Tq}{tant que}{faire}{fintq}
8. \SetKwFor{PourCh}{pour chaque}{faire}{finprch}
9. \SetKwFor{PourTous}{pour tous}{faire}{finprts}
10. \SetKwRepeat{Repeter}{r
ep
eter}{jusqu`
a}
12.2
German keywords
\Ein{Eingabe}
\Aus{Ausgabe}
\Daten{Daten}
\Ergebnis{Ergebnis}
\Bis{bis}
\KwZurueck{zur
uck}
\Zurueck{zur
uck}
\Beginn{Beginn}
41
\Wiederh{stop condition}{loop}
\lWiederh{stop condition}{line loop}
\eWenn{condition}{then text}{else text}
\Wenn{condition}{then text}
\uWenn{condition}{then text without end}
\lWenn{condition}{then line}
\SonstWenn{condition}{elseif text}
\uSonstWenn{condition}{elseif text without end}
\lSonstWenn{condition}{elseif line}
\Sonst{else text}
\uSonst{else text without end}
\lSonst{else line}
\Unterscheide{conditions}switch-case-default text\Fall{case of}{text}
\uFall{case of}{text}
\lFall{case of}{line text}
\Anderes{default text}
\lAnderes{default line}
\Fuer{condition}{loop}
\lFuer{condition}{line loop}
\FuerPar{condition}{loop}
\lFuerPar{condition}{line}
\FuerJedes{condition}{loop}
\lFuerJedes{condition}{line}
\FuerAlle{condition}{loop}
\lFuerAlle{condition}{line}Ende
\Solange{condition}{loop}Ende
\lSolange{condition}{line}
Here we describe how they are obtained:
\SetKwInput{Ein}{Eingabe}
\SetKwInput{Aus}{Ausgabe}
\SetKwInput{Daten}{Daten}
\SetKwInput{Ergebnis}{Ergebnis}
\SetKw{Bis}{bis}
\SetKw{KwZurueck}{zur
uck}
\SetKw{Zurueck}{zur
uck}
\SetKwBlock{Beginn}{Beginn}{Ende}
\SetKwRepeat{Wiederh}{wiederhole}{bis}
42
\SetKwIF{Wenn}{SonstWenn}{Sonst}{wenn}{dann}{sonst wenn}{sonst}{Ende}
\SetKwSwitch{Unterscheide}{Fall}{Anderes}{unterscheide}{tue}{Fall}{sonst}{Ende
Fall}Ende.
\SetKwFor{Fuer}{f
ur}{tue}{Ende}
\SetKwFor{FuerPar}{f
ur}{tue gleichzeitig}{Ende}
\SetKwFor{FuerJedes}{f
ur jedes}{tue}{Ende}
\SetKwFor{FuerAlle}{f
ur alle}{tue}{Ende}
\SetKwFor{Solange}{solange}{tue}{Ende}
12.3
Portuguese keywords
\Entrada{Entrada}
\Saida{Sada}
\Dados{Dados}
\Resultado{Resultado}
\Ate
\KwRetorna{[val]}
\Retorna{[val]}
\Iniciob{inside block}
\eSe{condition}{then block}{else block}
\Se{condition}{then block}
\uSe{condition}{then block without end}
\lSe{condition}{thens line text}
\Senao{else block}
\uSenao{else block without else}
\lSenao{elses line text}
\SenaoSe{condition}{elseif block}
\uSenaoSe{condition}{elseif block without end}
\lSenaoSe{condition}{elseifs line text}
\Selec{condition}{Switch block}
\Caso{a case}{case block}
\uCaso{a case}{case block without end}
\lCaso{a case}{cases line}
\Outro{otherwise block}
\lOutro{otherwises line}
\Para{condition}{text loop}
\lPara{condition}{line text loop}
\ParaPar{condition}{text loop}
\lParaPar{condition}{line text loop}
43
\ParaCada{condition}{text loop}
\lParaCada{condition}{line text loop}
\ParaTodo{condition}{text loop}
\lParaTodo{condition}{line text loop}
\Enqto{stop condition}{text loop}
\lEnqto{stop condition}{text loop}
\Repita{stop condition}{text loop}
\lRepita{stop condition}{line of the loop}
Here we describe how they are obtained:
1. \SetKwInput{Entrada}{Entrada}
\SetKwInput{Saida}{Sa
da}
\SetKwInput{Dados}{Dados}
\SetKwInput{Resultado}{Resultado}
2. \SetKw{Ate}{at
e} \SetKw{KwRetorna}{retorna}
\SetKw{Retorna}{retorna}
3. \SetKwBlock{Inicio}{in
cio}{fim}
4. \SetKwIF{Se}{SenaoSe}{Senao}{se}{ent~
ao}{sen~
ao se}{sen~
ao}{fim se}
5. \SetKwSwitch{Selec}{Caso}{Outro}{selecione}{fa
ca}{caso}{sen~
ao}{fim caso}fim selec
6. \SetKwFor{Para}{para}{fa
ca}{fim para}
7. \SetKwFor{ParaPar}{para}{fa
ca em paralelo}{fim para}
8. \SetKwFor{ParaCada}{para cada}{fa
ca}{fim para cada}
9. \SetKwFor{ParaTodo}{para todo}{fa
ca}{fim para todo}
10. \SetKwFor{Enqto}{enquanto}{fa
ca}{fim enqto}
11. \SetKwRepeat{Repita}{repita}{at
e}
12.4
Italian keywords
\KwIng{Ingresso}
\KwUsc{Uscita}
\KwDati{Dati}
\KwRisult{Risultato}
\KwA
\KwRitorna{ritorna}
\Ritorna{ritorna}
\Inizio{inside block}
\Ripeti{stop condition}{text loop}
\lRipeti{stop condition}{line of the loop}
44
45
12.5
Here are some czech keywords, please feel free to send me the others.
\Vst
\Vyst
\Vysl
How they are obtained:
1. \SetKwInput{Vst}Vstup
2. \SetKwInput{Vyst}V
ystup
3. \SetKwInput{Vysl}V
ysledek
13
Known bugs
no more known bugs actually; if you find one, please send it to me.
46
Release notes
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
* ADD:
* ADD:
* ADD:
* ADD:
* FIX:
* FIX:
* FIX:
* FIX:
*
*
*
*
*
*
FIX:
FIX:
FIX:
FIX:
FIX:
FIX:
| return 0
Tip: combine it with \SetKwFunction to write recursive function algorithm. With
example above, you could define \SetKwFunction{\Afunc}{afunc} and then write:
Def{\Afunc{i:int}{\eIf{i>0}{\KwRet \Afunc{i-1}}{\KwRet 0\;}} that writes:
def afunc(i: int):
| if(i>0):
|
return afunc(i-1)
| else:
|
return 0
with appropriate typo.
option croatian: croation keywords (thanks to Ivan Gavran)
option ngerman: same as german option but so can be used with global option ngerman
of babel
option spanish: Spanish support (thanks to Mario Abarca)
unterminated block: useful to add part separator that doesnt necessary need an end
keyword.
Designed on the pattern of unterminated if (see \uIf macro) allowing to
add a block that is not terminated by a keyword. Such block are defined in the same
time as a block is defined by adding a macro beginning with u. So, for example,
predefined \SetKwBlock{Begin}{begin}{end} defines now two commands:
- \Begin{} as previously which print a begin - end block
- \uBegin{} that defines a begin only block
dotocloa option which was broken
uIf and uCase didnt have same behavior when used with
noline, vlined or lined option. This is fixed. Side effect: no empty
line after an uIf or uCase when used with options lined or vlined
a bug with Repeat Until command when use with side comment on Until
a bug with side text -- text put into () -- of command macro (SetKwIf and so on)
which was always setting a ; even after a \DontPrintSemicolon
a bug with hyperref and chapter definition (thanks to Hubert Meier)
bugs with l macro and side comment
revision number
fix non ascii character (utf8 not yet recognized by all latex engine)
fnum@algocf had an useless parameter which sometimes broke expansion and output an error
works now with multicol package
48
List of Algorithms
1
2
3
4
5
6
7
8
49
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
4
5
7
10
12
13
14
15