I'm using algorithm2e and I need to use the loop
keyword as it appears here:
[![enter image description here][1]][1]
MWE
documentclass{article}
\usepackage{xcolor}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
%%% Coloring the comment as blue
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}
\SetKwInput{KwInput}{Input} % Set the Input
\SetKwInput{KwOutput}{Output} % set the Output
\begin{document}
\maketitle
\begin{algorithm}[H]
\DontPrintSemicolon
\KwInput{Your Input}
\KwOutput{Your output}
\KwData{Testing set $x$}
$\sum_{i=1}^{\infty} := 0$ \tcp*{this is a comment}
\tcc{Now this is an if...else conditional loop}
\If{Condition 1}
{
Do something \tcp*{this is another comment}
\If{sub-Condition}
{Do a lot}
}
\ElseIf{Condition 2}
{
Do Otherwise \;
\tcc{Now this is a for loop}
\For{sequence}
{
loop instructions
}
}
\Else
{
Do the rest
}
\tcc{Now this is a While loop}
\While{Condition}
{
Do something\;
}
\caption{Example code}
\end{algorithm}
\end{document}
Best Answer
I am now discovering this package and in the documentation, there are macros that allow you to define your own keywords.
There are some that define loops, but they add the stop test either at the end or at the beginning of the loop.
So I opted for the macro that defines a block, since the stop test is given before the end of the loop.
\SetKwBlock{Loop}{Loop}{end}
The first parameter defines the name of the macro, so if you write it with a capital letter
{Loop}
, you must do the same when calling:\Loop
. If you write it with a lower case{loop}
, you must call\loop
. The second parameter is how the keyword will be displayed in the algorithm.I quote the manual on page 35: