56

Is there a command in algorithmic package which is similar to \ElsIf in algorithmicx? ( I do not want each nested if to be indented).

1 Answer 1

78

Yes there is: \ELSIF. This is one of the first examples taken from the algorithms bundle documentation (section 3.2 The if-then-else Statement, p 3):

enter image description here

\documentclass{article}

\usepackage{algorithmic}

\begin{document}

\begin{algorithmic}
  \IF{some condition is true}
    \STATE do some processing
  \ELSIF{some other condition is true}
    \STATE do some different processing
  \ELSIF{some even more bizarre condition is met}
    \STATE do something else
  \ELSE
    \STATE do the default actions
  \ENDIF
\end{algorithmic}

\end{document}

If you are using algorithmicx you have to use \ElsIf. Here is a mirror of the above pseudocode using algpseudocode:

\documentclass{article}

\usepackage{algpseudocode,algorithm}

\begin{document}

\begin{algorithmic}
  \If{some condition is true}
    \State do some processing
  \ElsIf{some other condition is true}
    \State do some different processing
  \ElsIf{some even more bizarre condition is met}
    \State do something else
  \Else
    \State do the default actions
  \EndIf
\end{algorithmic}

\end{document}
5
  • 1
    Could I also use ElsIf ?
    – alper
    Commented Nov 22, 2021 at 19:04
  • 4
    @alper: The algorithms bundle (which provides the algorithmic package) only supplies capitalized keywords (like \ELSIF). The algorithmicx bundle (which provides the algpseudocode and other packages) provides \ElsIf and the like; algcompatible would provide \ELSIF.
    – Werner
    Commented Nov 22, 2021 at 20:15
  • If I use \usepackage{algorithmic} instead of \usepackage{algorithmicx} could it be also done?
    – alper
    Commented Aug 20, 2022 at 17:45
  • @alper: Sure; in fact, that's what I show in my example.
    – Werner
    Commented Aug 21, 2022 at 8:48
  • Ah sorry I meant using algorithmicx instead of algorithmic ; but \ElsIf does do job when I use algorithmicx
    – alper
    Commented Aug 21, 2022 at 10:05

You must log in to answer this question.

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