2

I am writing an algorithm in LaTeX and I would like to remove the keyword "then" in the part of else if condition.

\documentclass{article}
\usepackage[noend]{algcompatible}

\begin{document}

\begin{algorithmic}
\If{c1}  
      \State{}
\ElsIf {c2}
      \State{}
\EndIf
\end{algorithmic}

\end{document}

I tried to add the following definition:

\algdef{SE}[ELSIF]{NoThenElseIf}{ElseIf}[1]{\algorithmicelsif\ #1}{\algorithmicelse\ \algorithmicif}% 

and use it in the code.

 \NoThenElseIf {c2}
      \State{}

However, the compiler gives the following output:

! Undefined control sequence.
\ALG@text #1->\algorithmicelsif 

1 Answer 1

3

Your code use resembles what is defined by algpseudocode, not algcompatible. However, the same process holds here (see end of post).

I first found the definition of \ElsIf within algpseudocode.sty:

\algdef{C}[IF]{IF}{ElsIf}[1]{\algorithmicelse\ \algorithmicif\ #1\ \algorithmicthen}

Then, I replicated it for a new command \NoThenElseIf:

\newcommand{\algorithmicelsif}{\textbf{elsif}}
\algdef{C}[IF]{IF}{NoThenElseIf}[1]{\algorithmicelsif\ #1}

enter image description here

\documentclass{article}

\usepackage[noend]{algpseudocode}

% Original definition of \ElsIf from
%   algpseudocode.sty (http://mirrors.ctan.org/macros/latex/contrib/algorithmicx/algpseudocode.sty)
% \algdef{C}[IF]{IF}{ElsIf}[1]{\algorithmicelse\ \algorithmicif\ #1\ \algorithmicthen}%

\newcommand{\algorithmicelsif}{\textbf{elsif}}
\algdef{C}[IF]{IF}{NoThenElseIf}[1]{\algorithmicelsif\ #1}

\begin{document}

\begin{algorithmic}
  \If{c1}  
    \State True branch
  \ElsIf{c2}
    \State Else true branch
  \NoThenElseIf{c3}
    \State No \textbf{then} true branch
  \EndIf
\end{algorithmic}

\end{document}

For algcompatible:

\algdef{C}[IF]{IF}{ELSIF}%
   [2][default]{\algorithmicelse\ \algorithmicif\ #2\ \algorithmicthen\ALG@compatcomm{#1}}%

This can be replaced with

\makeatletter
\newcommand{\algorithmicelsif}{\textbf{elsif}}
\algdef{C}[IF]{IF}{NOTHENELSIF}%
   [2][default]{\algorithmicelse\ \algorithmicif\ #2\ALG@compatcomm{#1}}%
\makeatother

Here's a complete minimal example:

\documentclass{article}

\usepackage[noend]{algcompatible}

% Original definition of \ELSIF from
%   algcompatible.sty (http://mirrors.ctan.org/macros/latex/contrib/algorithmicx/algcompatible.sty)
% \algdef{C}[IF]{IF}{ELSIF}%
%   [2][default]{\algorithmicelse\ \algorithmicif\ #2\ \algorithmicthen\ALG@compatcomm{#1}}%

\makeatletter
\newcommand{\algorithmicelsif}{\textbf{elsif}}
\algdef{C}[IF]{IF}{NOTHENELSIF}%
   [2][default]{\algorithmicelse\ \algorithmicif\ #2\ALG@compatcomm{#1}}%
\makeatother

\begin{document}

\begin{algorithmic}
  \IF{c1}  
    \STATE True branch
  \ELSIF{c2}
    \STATE Else true branch
  \NOTHENELSIF{c3}
    \STATE No \textbf{then} true branch
  \ENDIF
\end{algorithmic}

\end{document}
1
  • 1
    @ Werner: Thank you very much for your fast and detailed explanation of the LaTeX background. What an excellent answer! Commented May 24, 2020 at 17:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.