I'm using the algorithm2e package to put algorithm in my Latex documents.

In my document, I have something like this :

\section My algorithms
\subsection Algo 1
code for first algorithm

\subsection Algo 2
code for second algorithm

\subsection Algo 3
code for third algorithm

\subsection Algo 4
code for fourth algorithm

and so on.

The problem is that when producing the PDF, some algorithm are correctly displayed under their subsection, and some are displayed on other pages, not under the right section.

I get something like this :

1. My algorithms
1.1 Algo 1
    pseudo code for algo 1

1.2 Algo 2
    pseudo code for algo 2

1.3 Algo 3
1.4 Algo 4
---------------- next page -----------
    pseudo code for algo 3
---------------- next page -----------
    pseudo code for algo 4

[WARNING: the following links are broken are should be fixed by the OP]

Here is the full Latex source code : http://www.filedropper.com/demo_1 Here is the PDF I get : http://www.filedropper.com/demo_2

Is there a way to make sure the algo will stick with its subsection ?

migrated from stackoverflow.com Sep 8 '12 at 22:06

This question came from our site for professional and enthusiast programmers.


algorithm is a floating environment, just like figure or table. Quoth the manual:

The optional arguments [Hhtbp] [to the algorithm environment, U.S.] works like those of figure environment. The H argument forces the algorithm to stay in place. If used, an algorithm is no more a floating object. Caution: algorithms cannot be cut, so if there is not enough place to put an algorithm with H option at a given spot, LATEX will place a blank and put the algorithm on the following page.

  • I feel so silly I didn't spot that ! Thanks ! – Jérôme Feb 11 '11 at 8:47

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.