Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks for the proposition. Indeed this is for at 15 to 18 years old students, so I prefer to use this not so good formatting for algorithms. –  projetmbc Oct 11 '12 at 15:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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