2

The following M(not)WE should work. Did I find a bug ?

\documentclass{article}

\usepackage{algorithm2e}

\SetKwSwitch{Switch}{Case}{Other}{Switch}{:}{Case}{Other}{} 

\begin{document}

\begin{algorithm}
    \Switch{X}{
        \Case{1}{A}
        \Other{Other}
    }
\end{algorithm}

\end{document}
2

No bug: \SetKwSwitch requires nine arguments and you only supply eight of them so the \par generated by the blank line is taken as ninth. And \par is illegal in the context where the ninth argument is used.

\newcommand{\SetKwSwitch}[9]{% #1=\Switch #2=\Case #3=\Other #4=switch #5=do #6=case #7=otherwise #8=endcase #9=endsw
%       \algocf@newcmdside{#1}{3}%
        \algocf@newcmdside@koif{#1}%
                {\KwSty{#4}\algocf@scond\ArgSty{##2}\algocf@econd\KwSty{#5}\ifArgumentEmpty{##1}\relax{ ##1}\algocf@block{##3}{#9} {##4\relax}}%

The manual has a description where the last (symbolic) argument is not braced, so there is a bug, but only in the documentation. On page 39, start of section 11.6, you read

 \SetKwSwitch{Switch}{Case}{Other}{switch}{do}{case}{otherwise}{endcase}endsw

but it should be

\SetKwSwitch{Switch}{Case}{Other}{switch}{do}{case}{otherwise}{endcase}{endsw}

Fixed example:

\documentclass{article}

\usepackage{algorithm2e}

\SetKwSwitch{Switch}{Case}{Other}{Switch}{:}{Case}{Other}{EndCase}{EndSwitch}

\begin{document}

\begin{algorithm}
    \Switch{X}{
        \Case{1}{A}
        \Other{Other}
    }
\end{algorithm}

\end{document}

enter image description here

1
  • Thanks for your answer. I have read the doc too quickly. :-(
    – projetmbc
    Sep 9 '20 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.