2

I have an algorithm and an if-elseif block in it that does not have a final else. This leaves my algorithm open, I mean the algorithm does not show end after finishing elseif and it needs an \else to close it. But I do not have any else in the logic/procedure of my algorithm. Is there any solution to close it?

\documentclass{article} 
\usepackage{algorithm2e} 
\begin{document} 
\begin{algorithm} 
\raggedright 
\SetAlgoLined 
\LinesNumbered 
\SetKwProg{Function}{function}{}{end} 
\SetKwRepeat{Do}{do}{until}

$p \gets f(x)$\; 
\uIf{$p > 1$}{%   
$p \gets x $\; 
}
\uElseIf{$p < 0$}{% <--   
$p \gets -x $\; 
}

\Return $p$\;   
\end{algorithm} 
\end{document}
3
  • 1
    Welcome to TeX.SE! Can you please make your code snippet be compilable, then we do not have to guess what you are doing ...
    – Mensch
    Commented Aug 3, 2021 at 17:50
  • I don't understand your question. If you add \documentclass{article} \usepackage{algorithm2e} \begin{document} ... \end{document} to your snippet, the document builds just fine. What is the problem? Commented Aug 3, 2021 at 18:07
  • 3
    Does replacing \uElseIf by \ElseIf do what you want? Commented Aug 3, 2021 at 18:10

1 Answer 1

4

How about this (as suggested by Wille):

\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}
\raggedright
\SetAlgoLined
\LinesNumbered
\SetKwProg{Function}{function}{}{end}
\SetKwRepeat{Do}{do}{until}

$p \gets f(x)$\;
\uIf{$p > 1$}{%
  $p \gets x $\;
}\ElseIf{$p < 0$}{% <--
  $p \gets -x $\;
}

\Return $p$\;  
\end{algorithm}
\end{document}

enter image description here

You must log in to answer this question.

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