[Tex/LaTex] algorithm2e – Unwanted vertical space

algorithm2espacing

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} 

Best Answer

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