5

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 '13 at 5:38
4

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

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

%\noindent
%\begin{minipage}{\linewidth}
\captionof{algorithm}{Euclid’s algorithm\label{euclid}}
\begin{algorithmic}[1]
  \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$
    \EndWhile\label{euclidendwhile}
    \State \textbf{return} $b$\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}
%\end{minipage}
\end{document}

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

\begin{algorithm}[H]
% <your algorithm here>
\end{algorithm}

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).

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.