# Trying to write loopkeyword in algorithm2e

I'm using algorithm2e and I need to use the loopkeyword 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}}

\SetKwInput{KwInput}{Input}                % Set the Input
\SetKwInput{KwOutput}{Output}              % set the Output

\begin{document}
\maketitle

\begin{algorithm}[H]
\DontPrintSemicolon

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

• Maybe help: tex.stackexchange.com/q/213502/31034
– user31034
Aug 11, 2019 at 11:53
• Thank you. I tried what's on that page before but it actually takes the new keyword as a word and not as a loop with its block. Aug 11, 2019 at 11:55
• If you use algorithm and algpseudocode packages, you can use \Loop <text> \EndLoop as a loop block.
– user31034
Aug 11, 2019 at 12:10
• Your screenshot is not complete, it does not show how the loop ends. Aug 11, 2019 at 12:11
• @AndréC Updated. Check it Aug 11, 2019 at 12:17

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:

\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 doesn't necessary need an end keyword.

\documentclass{article}
\usepackage{xcolor}
\usepackage[linesnumbered,boxruled,ruled,noline]{algorithm2e}

%%% Coloring the comment as blue
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}

\SetKwInput{KwInput}{Input}                % Set the Input
\SetKwInput{KwOutput}{Output}              % set the Output
\SetKwBlock{Loop}{Loop}{end}
\begin{document}
%\maketitle

\begin{algorithm}[H]
\DontPrintSemicolon

\KwData{Testing set $x$}
\Loop ( until the terminal condition is met. One epoch:)
{n=n+1

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

terminal condition: RMSE on \dots
}
\caption{Example code}
\end{algorithm}

\end{document}

• Flawless! Thank you very much, Andre!!! Aug 11, 2019 at 12:51
• The Loop keyword should be in Upper or Lower case? Aug 11, 2019 at 12:54
• I just add more explnations, is it clear now? Aug 11, 2019 at 15:01
• Perfect! Thank you very much for such a great answer Aug 11, 2019 at 15:42