# Algorithm tag and page break

I'm starting my experiment with LaTeX, i'm writing an article and all that I want is to embed an algorithm inside normal text, this is my code:

\documentclass{article}
\usepackage{algorithmic}
\begin{document}
\section{Lettura file .gpx}
Lorem ipsum ...

\algsetup{indent=1.7em}
\begin{algorithm}
\caption{Alg capt}
\label{alg1}

\begin{algorithmic}[1]
... algorithm here...
\end{algorithmic}
\end{algorithm}
\end{document}


This is fully working but algorithmic uses a pagebreak automatically, how can I avoid this break and show the algorithm immediately after "lorem ipsum" part?

-

## migrated from stackoverflow.comNov 6 '11 at 19:45

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

Here are some suggestions:

• If you're adding your algorithm inside the algorithm float, then it will float according to the "float specifiers" given as the optional argument to the algorithm environment. For example

\begin{algorithm}[ht]
...
\end{algorithm}


To force the float to be less float-y, add ! to the float specifier. However, as suggested by the float package, this still remains a "suggestive specifier". Instead, the package provides the additional H float specifier which tells LaTeX to "PUT IT HERE!"

• Use the algorithmx package. It is very similar to algorithmic, but allows for more flexibility. Here's a short, yet complete, minimal example (from the algorithmicx package documentation).

• If you wish to have the algorithm break across the page boundary, you have to set things up differently. I've incorporated a breakablealgorithm environment below.

\documentclass{article}
\usepackage{algorithm,algpseudocode,float}
\usepackage{lipsum}

\makeatletter
\newenvironment{breakablealgorithm}
{% \begin{breakablealgorithm}
\begin{center}
\refstepcounter{algorithm}% New algorithm
\hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled
\renewcommand{\caption}[2][\relax]{% Make a new \caption
{\raggedright\textbf{\ALG@name~\thealgorithm} ##2\par}%
\ifx\relax##1\relax % #1 is \relax
\else % #1 is not \relax
\fi
\kern2pt\hrule\kern2pt
}
}{% \end{breakablealgorithm}
\kern2pt\hrule\relax% \@fs@post for \@fs@ruled
\end{center}
}
\makeatother

\begin{document}

\listofalgorithms

\section{Some section}

\lipsum[1]

\begin{algorithm}[H]
\caption{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 \neq 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
\State \textbf{return} $b$\Comment{The g.c.d.\ is~$b$}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\lipsum[2]

\begin{breakablealgorithm}
\caption{Euclid’s algorithm}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d.\ of~$a$ and~$b$}
\State $r \gets a \bmod b$
\While{$r \neq 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
\State \textbf{return} $b$\Comment{The g.c.d.\ is~$b$}
\EndProcedure
\end{algorithmic}
\end{breakablealgorithm}

\end{document}


The above redefinition of \caption assumes that it will be placed where it's located in the algorithm; specifically at the top. Rule definitions were taken from the float package's \@fs@ruled construction.

-
Werner's answer proposing begin{algorithm}[H] which is basically a float[H]? This means place the float 'here', however, it is still treated as a float and if Latex doesn't find enough text around it, the vertical spaces will be noticeable –  isti_spl Jul 6 '13 at 18:34
@isti_spl: True. As such, it's not always advisable to use the [H] specifier. For more detail on the float placement algorithm, see How to influence the position of float environments like figure and table in LaTeX? and Keeping tables/figures close to where they are mentioned. –  Werner Jul 6 '13 at 21:46
@Werner, wow this is so beautiful. Thank you so much. –  CroCo Feb 18 at 23:21

If you don't want your algorithm to be treated as a floating object, then don't use the algorithm environment; you can still have a caption using the \captionof command from the caption package; using the appropriate style and format, this caption can have the same look and feel as those produced by the algorithm environment:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{caption}
\usepackage{lipsum}

\DeclareCaptionFormat{algor}{%
\hrulefill\par\offinterlineskip\vskip1pt%
\textbf{#1#2}#3\offinterlineskip\hrulefill}
\DeclareCaptionStyle{algori}{singlelinecheck=off,format=algor,labelsep=space}
\captionsetup[algorithm]{style=algori}

\begin{document}
\section{Lettura file .gpx}
\lipsum*[1]
\begin{center}
\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
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{center}

\end{document}


-
Is there a way to insert a rule automatically at the end of the algorithm? I've try \rule[15pt]{\textwidth}{0.5pt} manually but it's so inconvenient and there will be a large gap between the rule and the following text. Thank you! @Gonzalo –  Stan Jan 8 '13 at 8:48
@Stan, did you get a solution for the bottom line?? –  CroCo Feb 18 at 22:30