2

Last days I met a strange behavior with algorithm2e which produces an unwanted space after an IF-block.

I put here one MWE which shows that the problem really comes from my customization and not from the algorithm2e package.

I don't understand what I've done wrong...

STRANGE OUTPUT

enter image description here

MWE

\documentclass{article} 
    \usepackage[utf8x]{inputenc} 
    \usepackage[longend]{algorithm2e} 

\makeatletter 
    \newcommand\@algoVerboseBlock[3]{% 
        \hspace{-0.8em} \KwSty{#1} \ifhmode \\ \fi #2 \ifhmode \\ \fi \hspace{-0.8em} \KwSty{#3}% 
    } 

    \newcommand\@algoDefVerboseIf[2]{% 
        \renewcommand\algocf@uIf[2]% 
            {\If@ifthen{##1}\If@noend{\@algoVerboseBlock{#1}{##2}{#2}}} 
% 
        \renewcommand\algocf@If[2]% 
            {\If@ifthen{##1}\If@noend{\@algoVerboseBlock{#1}{##2}{#2}}} 
% 
    } 
    \newcommand\@algoDefVerboseElse[2]{% 
        \renewcommand{\algocf@Else}[1]% 
            {\Else@else\If@endif{\@algoVerboseBlock{#1}{##1}{#2}}} 
        \renewcommand{\algocf@uElse}[1]% 
            {\Else@else\If@endif{\@algoVerboseBlock{#1}{##1}{#2}}} 
    } 
\makeatother 


\begin{document} 

\section{Default}

\begin{algorithm}[H]
    \uIf{1}{if...}%
    \Else{else... \\ \If{1}{if...}}%
\end{algorithm}


\section{Customization}

\makeatletter 
    \SetKwIF{If}{ElseIf}{Else}{Si}{Alors}{Sinon Si}{Sinon}{} 
    \@algoDefVerboseIf{Début Si}{Fin Si} 
    \@algoDefVerboseElse{Début Sinon}{Fin Sinon}  
\makeatother 

\begin{algorithm}[H]
    \uIf{1}{if...}%
    \Else{else... \\ \If{1}{if...}}%
\end{algorithm}

\end{document} 

1 Answer 1

1

This seems to be a bug in algorithm2e, it occurs without any of your customisations, and should be reported to the package author; a similar bug was fixed in the last releease.

A workaround is to write \\[-\baselineskip] just after the offending block. Here it is in your example, below that I give minial example displaying the bug.

\documentclass{article} 
    \usepackage[utf8x]{inputenc} 
    \usepackage[longend]{algorithm2e} 

\makeatletter 
    \newcommand\@algoVerboseBlock[3]{% 
        \hspace{-0.8em} \KwSty{#1} \ifhmode \\ \fi #2 \ifhmode \\ \fi \hspace{-0.8em} \KwSty{#3}% 
    } 

    \newcommand\@algoDefVerboseIf[2]{% 
        \renewcommand\algocf@uIf[2]% 
            {\If@ifthen{##1}\If@noend{\@algoVerboseBlock{#1}{##2}{#2}}} 
% 
        \renewcommand\algocf@If[2]% 
            {\If@ifthen{##1}\If@noend{\@algoVerboseBlock{#1}{##2}{#2}}} 
% 
    } 
    \newcommand\@algoDefVerboseElse[2]{% 
        \renewcommand{\algocf@Else}[1]% 
            {\Else@else\If@endif{\@algoVerboseBlock{#1}{##1}{#2}}} 
        \renewcommand{\algocf@uElse}[1]% 
            {\Else@else\If@endif{\@algoVerboseBlock{#1}{##1}{#2}}} 
    } 
\makeatother 


\begin{document} 

\section{Customization}

\makeatletter 
    \SetKwIF{If}{ElseIf}{Else}{Si}{Alors}{Sinon Si}{Sinon}{} 
    \@algoDefVerboseIf{Début Si}{Fin Si} 
    \@algoDefVerboseElse{Début Sinon}{Fin Sinon}  
\makeatother 

\begin{algorithm}[H]
    \uIf{1}{if...}%
    \Else{else... \\ \If{1}{if...}\\[-\baselineskip]}%
\end{algorithm}

\end{document} 

Sample output

\documentclass{article}

\usepackage[utf8x]{inputenc}
\usepackage[longend]{algorithm2e}

\begin{document}
\thispagestyle{empty}

\begin{algorithm}[H]
    \If{1}{if...}%
    \Else{else... \\ \uIf{2}{if...}}%
\end{algorithm}

\end{document}

Minimal buggy output

3
  • Sorry fo the deleted message. It's my fault even if I didn't want to do that. I've just sent a message to the author of algorithm2e which indicates the bug tracking with your MWE
    – projetmbc
    Nov 3, 2012 at 20:35
  • 1
    The bug is now known by the author. It has one solution, so we have to wait for the next version of algorithm2e.
    – projetmbc
    Nov 4, 2012 at 14:07
  • Thanks for explaining the deletion issue and good that you have reported this bug. In the meantime, I hope the workaround above is helpful. Nov 5, 2012 at 7:39

You must log in to answer this question.

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