Is there any elegent way to place algorithm without using algorithm environment?

  • 2
    I've added an answer which depends on what you're after in avoiding the algorithm environment. If it's not satisfactory, give some feedback and explain your use case.
    – Werner
    Sep 22, 2013 at 5:38

1 Answer 1


You don't have to put it in a float. But it depends on what you're after. If the intent is to avoid the algorithm float yet still have captions, then the caption package provides \captionof{<float type>}{<caption>} that allows you to set a caption of a float outside a float. For example:

enter image description here

\usepackage{algorithm,algpseudocode,caption}% http://ctan.org/pkg/{algorithms,algorithmicx,caption}

\captionof{algorithm}{Euclid’s algorithm\label{euclid}}
  \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \State \textbf{return} $b$\Comment{The gcd is b}

While algorithm provides the floating algorithm environment, it's only used for the algorithm counter and float type. The above works for algorithmic and/or algorithmicx.

It is advisable to contain the algorithm inside a box (like a minipage), but this may depend on the setting of the algorithm in the code itself. For example, algorithmicx sets its algorithms (in the algorithmic environment) as a list, which will break across the page boundary. If you don't want it, contain it.

algorithm2e provides its own "do not float" [H] float specifier, setting the contents in a minipage if used this way. So one would have to use

% <your algorithm here>

under those conditions. A large amount of leg-work is done by the package at the start of the algorithm environment in order to set things up since it has its own interface (using \; to denote line-ends, otherwise known as a horizontal space in math-mode outside the environment).

  • The first code block raises Package caption Warning: \setcaptiontype outside box or environment now.
    – user202729
    Feb 1 at 11:11
  • @user202729: True, since it is suggested to place the contents within a group (which is necessarily provided by a float/minipage). This is mentioned somewhat in the answer: "It is advisable to contain the algorithm inside a box (like a minipage)...", as well as in the commented code.
    – Werner
    Feb 1 at 19:29

You must log in to answer this question.

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