In algpseudocode, \algrenewcommand doesn’t work for \algorithmicelsif

algorithmicxalgpseudocode

I would like to customize keywords in algpseudocode, including the rendering of \ElsIf. But when I compile the file

\documentclass{article}
\usepackage{algpseudocode}
\algrenewcommand\algorithmicthen{\relax}
\algrenewcommand\algorithmicif{If}
\algrenewcommand\algorithmicelse{Else}
\algrenewcommand\algorithmicelsif{Elif}
\begin{document}
\end{document}

I get a syntactic error:

! LaTeX Error: Command \ALG@cmd@2@algorithmicelsif undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.7 \algrenewcommand\algorithmicelsif
                                     {Elif}
?

whereas the first three algrenewcommands work fine. What am I doing wrong?

I can see that the style file algorithmic.sty contains

\newcommand{\algorithmicif}{\textbf{if}}
\newcommand{\algorithmicthen}{\textbf{then}}
\newcommand{\algorithmicelse}{\textbf{else}}
\newcommand{\algorithmicelsif}{\algorithmicelse\ \algorithmicif}

and so I thought that it should be possible to redefine any of these commands.

Best Answer

algorithmic.sty forms part of the algorithms bundle, while algpseudocode.sty forms part of the algorithmicx bundle, so you're comparing code that isn't used at all. The latter defines \ElsIf to use \algorithmicelse and \algorithmicif, so redefining those should suffice. Alternatively, if you want a different output via \ElsIf, define it as such:

enter image description here

\documentclass{article}

\usepackage{algpseudocode}

\algrenewcommand\algorithmicthen{\relax}
\algrenewcommand\algorithmicif{If}
\algrenewcommand\algorithmicelse{Else}
\algrenewcommand\algorithmicend{End}

\algdef{C}[IF]{IF}{ElsIf}[1]{ElIf\ #1}% Update display of \ElsIf

\begin{document}

\begin{algorithmic}[1]
  \If{$quality \geq 9$}
    \State $a \gets perfect$
  \ElsIf{$quality \geq 7$}
    \State $a \gets good$
  \ElsIf{$quality\geq 5$}
    \State $a \gets medium$
  \ElsIf{$quality \geq 3$}
    \State $a \gets bad$
  \Else
    \State $a \gets unusable$
  \EndIf
\end{algorithmic}

\end{document}

The above example code was partially taken from the algorithmicx documentation.

Related Question