2

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}
5
  • 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.
    – Delan
    Aug 11, 2019 at 11:55
  • 1
    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.
    – AndréC
    Aug 11, 2019 at 12:11
  • @AndréC Updated. Check it
    – Delan
    Aug 11, 2019 at 12:17

1 Answer 1

3

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}
4
  • 1
    Flawless! Thank you very much, Andre!!!
    – Delan
    Aug 11, 2019 at 12:51
  • The Loop keyword should be in Upper or Lower case?
    – Delan
    Aug 11, 2019 at 12:54
  • 1
    I just add more explnations, is it clear now?
    – AndréC
    Aug 11, 2019 at 15:01
  • 1
    Perfect! Thank you very much for such a great answer
    – Delan
    Aug 11, 2019 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.