[Tex/LaTex] Trying to write `loop`keyword in algorithm2e

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

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

screenshot

\documentclass{article}
\usepackage{xcolor}
\usepackage[linesnumbered,boxruled,ruled,noline]{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
\SetKwBlock{Loop}{Loop}{end}
\begin{document}
%\maketitle

\begin{algorithm}[H]
\DontPrintSemicolon

  \KwInput{Your Input}
  \KwOutput{Your output}
  \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}