[Tex/LaTex] algorithm2e and enumerate

#enumeratealgorithm2e

I would like to put one algorithm inside one enumerated list like in the following code.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8x]{inputenc}
\usepackage{ucs}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[ruled,french]{algorithm2e}
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{enumerate}
    \item Bla, bla...

\begin{algorithm}[H]
    \caption{Le crible d'Eratosthène}

    \KwData{la liste $L$ des naturels compris de $2$ à $N$.}
    \KwResult{la liste $P$ des nombres premiers compris entre $2$ et $N$.}
    \BlankLine
    \For{$i = 2$ \KwTo $N$}{
        \If{$i$ n'est pas barré dans la liste $L$}{
            Barrer dans la liste $L$ les mutltiples $ki$ où $k \geq 2$.
        }
    }
    \Return{la liste $P$ des nombres non barrés.}
\end{algorithm} 

    \item Bla, bla...
\end{enumerate}

\end{document}

The problem I met is that I obtain the following output where the algorithm is misplaced (or too width).

enter image description here

If someone can give one solution with the algorithm centered regarding to the page, and not to the item content, it would be great.

Best Answer

The main problem is that algorithm2e uses \hsize in its macros, which is not recommended.

You can probably solve the issue by saying

\usepackage{xpatch}
\xpretocmd{\algorithm}{\hsize=\linewidth}{}{}

but I can't guarantee that this has no side effects.

enter image description here

Alternative way, using the full line width: define an listalgorithm environment to be used inside lists

\makeatletter
\newenvironment{listalgorithm}
 {\par\noindent\hspace*{-\@totalleftmargin}%
  \minipage{\textwidth}\algorithm[H]}
 {\endalgorithm\endminipage}
\makeatother

enter image description here

Final comment. I would avoid with all my strengths to place an algorithm inside an enumerate or itemize environment.