# Fixing the location of the appearance in algorithmicx environment

I'm trying to force an algorithm to stay between two paragraphs, but after compilation, it shows up in the next page. My algorithm takes more than half a page and is something like:

 \begin{algorithm}[tbh]
\caption{Game}

\begin{algorithmic}
\State  \bf{Initialize:} \normalfont
\begin{itemize}
\item \bf{A preferences:} \normalfont
\begin{itemize}
\item 1 for A
\item 2 for A
\item 3 for A
\end{itemize}
\item \bf{B preferences::} \normalfont
\begin{itemize}
\item 1 for B
\item 2 for B
\item 3 for B
\end{itemize}
\item \bf{Nature sets the laws:} \normalfont
\begin{itemize}
\item The nature chooses a set of incentives $\mathcal{R}$.
\end{itemize}
\end{itemize}
\State
\While {Set of Incentives $\mathcal{R}$ Exists}
\State
\State \bf{Defend:} \normalfont
\begin{itemize}
\item I
\item II
\item III
\end{itemize}

\State
\State \bf{Attack:} \normalfont
\begin{itemize}
\item I
\item II
\item III
\end{itemize}
\State
\State \bf{Nature:} \normalfont
\begin{itemize}
\item The Nature updates $\mathcal{R}$.
\end{itemize}
\State
\EndWhile
\end{algorithmic}
\end{algorithm}


It seems like [tbh] is not working here.

To force a float to remain in a specific location, add the float package to your preamble

\usepackage{float}% http://ctan.org/pkg/float


and use the [H] float placement specifier. Done.

\documentclass{article}
\usepackage[margin=1in]{geometry}% For this example
\usepackage{algorithm,algpseudocode,float}
\usepackage{lipsum}% For this example

\begin{document}

\lipsum[1]

\begin{algorithm}[H]% Use "stay right HERE" already!
\caption{Game}
\begin{algorithmic}
\State \textbf{Initialize:}
\begin{itemize}
\item \textbf{A preferences:}
\begin{itemize}
\item 1 for A
\item 2 for A
\item 3 for A
\end{itemize}
\item \textbf{B preferences::}
\begin{itemize}
\item 1 for B
\item 2 for B
\item 3 for B
\end{itemize}
\item \textbf{Nature sets the laws:}
\begin{itemize}
\item The nature chooses a set of incentives~$\mathcal{R}$.
\end{itemize}
\end{itemize}
\State
\While {Set of Incentives~$\mathcal{R}$ Exists}
\State
\State \textbf{Defend:}
\begin{itemize}
\item I
\item II
\item III
\end{itemize}
\State
\State \textbf{Attack:}
\begin{itemize}
\item I
\item II
\item III
\end{itemize}
\State
\State \textbf{Nature:}
\begin{itemize}
\item The Nature updates~$\mathcal{R}$.
\end{itemize}
\State
\EndWhile
\end{algorithmic}
\end{algorithm}

\lipsum[2]

\end{document}


The reason why the float didn't stay where it was supposed to is because it was probably too large to fit within the page in the first place. See How to influence the position of float environments like figure and table in LaTeX?. Most likely variables like \topfraction or \textfraction caused the float to move onto page of its own.

There might be some drawbacks of using the [H] float specifier, although minimal. See Drawbacks of the H specifier.

There are two kinds of environments in algorithmicx.

1. One is algorithmic, which typesets algorithms very much like normal text. This will stay between two paragraphs, if you put it there, but it will break across pages.

2. The other is algorithm, which is a floating object. This means that it can get a caption, it will stay on one page and LaTeX will try to put it where you want but, pretty much, it will end up wherever LaTeX likes it most.

You are using the latter. Your [tbh] directs LaTeX to try to place the algorithm alternatively:

1. Here, between your two paragraphs.

2. At the top of a page (it can be either this page or the next).

3. At the bottom of a page (same).

If your algorithm is not placed between your paragraphs, it's because LaTeX thinks it cannot fit there. You'll have to decide whether you want to break it across pages (using just algorithmic) or to have it where LaTeX suggests. Or you can force it, using the float package as in Werner's answer, but this will most probably look ugly.

• not in this order it always tries h first, then t then b then p. never use h on its own that makes the float virtually unplaceable as it prevents it being placed at top or bottom or on a float page (or at least ot would do that but latex2e gives a warning and changes it to th) – David Carlisle Mar 11 '14 at 23:38
• Fixed, sorry... – nickie Mar 12 '14 at 6:17