3

I want to implement a if else block like that

if A
  if B
    if C
       else if D
    end
   if E
   end
  end
end

Where if C and if E has same indentation. if B under if A. if C and if E both are under if B.

I used \SetKwIF{If}{}{}{if}{}{ELSEIF}{else}{end if} to set the rule of if and else

I also write it on overleaf to implement the above portion. I was unable to implement else if portion. If I write else if* overleaf throws errors.

\documentclass{article}
\usepackage[linesnumbered]{algorithm2e}
\usepackage{xcolor}
\newcommand\mycommfont[1]{\small\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}

\SetKwInput{KwInput}{Input}   % Set the Input
\SetKwInput{KwOutput}{Output} %Set the Output
\SetKwIF{If}{}{}{if}{}{ELSEIF}{else}{end if}%
\begin{document}
\SetAlgoNoLine
\begin{algorithm}[!ht]
\KwInput{abc}
\KwOutput{abc}
\KwData{abc}
\If{(A)}
    {
    \If{B}{
    \If{C}{
       do this
    }
    \ELSEIF{D}{
    Do this
    }
    
     \If{(E)}{
       do this
    }
    
    }
   }
       
\end{algorithm}
\end{document}
      

Error that I received

enter image description here

The package that I used for the algorithm \usepackage[linesnumbered]{algorithm2e}

4
  • 4
    Please don't add arbitrary tags. You're not using latex3 and overleaf is just your editor. That said, I don't know what you're asking or what you're trying to do. My best guess is that you are trying to typeset an algorithm. If so, please complete your code to make it a minimal working example If the problem is an error, provide the text of the error and a minimal non-working example we can use to reproduce it. Use backticks for inline code rather than bold.
    – cfr
    Jan 25 at 2:16
  • 2
    Instead of text "the command that I used", just add the \usepackage{algorithm2e} to the top (as well as \documentclass and \begin/\end{document}). It's much easier to help you if you have what you think should be a complete document that everyone can copy and paste.
    – Teepeemm
    Jan 25 at 3:03
  • I edited the question.
    – Encipher
    Jan 25 at 16:22
  • Why does your error message include \ELSIF when your document uses \ELSEIF?
    – cfr
    Jan 25 at 18:33

1 Answer 1

5

You need to fill the second argument of \SetKwIF to create the macro:

\documentclass{article}
\usepackage[linesnumbered]{algorithm2e}

\SetKwIF{If}{ELSEIF}{Else}{if}{then}{else if}{else}{endif}

\begin{document}
\SetAlgoNoLine
\begin{algorithm}[!ht]
\uIf{C}{
   do this
}
\ELSEIF{D}{
Do this
}
\end{algorithm}
\end{document}

enter image description here

5
  • your else if displayed in the small letters. However, mine is in capital letter ELSEIF. How can I change the display like you?
    – Encipher
    Jan 25 at 19:55
  • 1
    @Encipher Adjust the 7th argument to however you want to display it. But if you use lowercase for the other keywords, why would you use all caps for elseif? Jan 25 at 19:59
  • Can you please explain little bit why we need to write 8 arguments? My structure does not have else if after the 1st if, then in macro why I need to define else if immediate after if? like that.
    – Encipher
    Jan 25 at 20:03
  • 1
    @Encipher With \SetKwIF{If}{ELSEIF}{}{if}{}{else if}{else}{end if} you define a whole set of macros. The complete syntax actually is \SetKwIF{If}{ElseIf}{Else}{if}{then}{else if}{else}{endif}. It will generate macros like \If, \ELSEIF etc., which you can later use in your algorithm. It has nothing to do with the structure of your code, the elseif is always the second argument in \SetKwIF, that's how \SetKwIF is defined. Jan 25 at 20:13
  • Thank you for the explanation.
    – Encipher
    Jan 25 at 20:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .