[Tex/LaTex] Fixing the location of the appearance in algorithmicx environment

algorithmicxalgorithmsfloatspositioning

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.

Best Answer

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.

enter image description here

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