The algorithm 5 shows how you can have each block tagged with begin-end keywords. This is
done by using automatic block display (new feature since release 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).
Not that since release 5.1, you can also have block tagged for one line display. The algo-
rithm 6 is based on algorithm 5 but uses “l macros” as e.g. \lIf to display command on one
line ; \AlgoDisplayGroupMarkers is added to have also block tagged with markers defined by
\SetAlgoBlockMarkers. To achieve this display, we replace preamble of algorithm 5 by:
\AlgoDisplayBlockMarkers\AlgoDisplayGroupMarkers\SetAlgoBlockMarkers{ \{}{ \}\ }%
\SetAlgoNoEnd\SetAlgoNoLine
The algorithm 7 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 doesn’t 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 8 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 9 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%
10