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

Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. –  Torbjørn T. Nov 6 '11 at 19:47

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

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

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{float}% http://ctan.org/pkg/float
\begin{document}
\section{Lettura file .gpx}
\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\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{algorithm}
\end{document}

-
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