26

I'm typesetting an algorithm using the algorithmx package as follows:

\begin{algorithm}
  \caption{\textsc{Whatever}}
  \begin{algorithmic}[1]
  ...
\end{algorithm}

However, I would like it to float like a figure. If I replace \begin{algorithm} with \begin{figure} it does float as I like, however, the caption does not look as pretty any more (I presume it is redefined by a sig-alternate class I am using for the whole document).

Is it possible to have both a nice caption and floating behaviour at the same time?

Werner
  • 603,163

3 Answers3

25

Thanks to Harald's advice I found the following two-line solution to my problem:

\usepackage{float}
\newfloat{algorithm}{t}{lop}

This was sufficient to make all my algorithm blocks to float exactly as I needed them to.

See also: "float" package reference.

Werner
  • 603,163
11

The LaTeX Wikibook section on algorithms explains this very nicely:

The algorithmicx package provides a number of popular constructs for algorithm designs. Put \usepackage{algpseudocode} in the preamble to use the algorithmic environment to write algorithm pseudocode (\begin{algorithmic}...\end{algorithmic}). You might want to use the algorithm environment (\usepackage{algorithm}) to wrap your algorithmic code in an algorithm environment (\begin{algorithm}...\end{algorithm}) to produce a floating environment with numbered algorithms.

Meaning, you only need to add \usepackage{algorithm} and your code snippet will be rendered as a floating figure.

m0nhawk
  • 9,664
3

Just importing the float package and keeping the optional float argument makes the algorithm float. E.g.,

\usepackage{float}
...

\begin{algorithm}[t]
  \caption{\textsc{Whatever}}
  \begin{algorithmic}[1]
  ...
\end{algorithm}

Replace [t] with [b] if needed.