[Tex/LaTex] Algorithm keeps appearing on the last page of the latex document

algorithmicalgorithms

I am writing an algorithm with the algorithmic environment and Latex keeps pushing it on the last page, no matter what I do, this is just a short example.

\documentclass[a4paper,10pt, twoside]{book}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage[utf8]{inputenc}


\begin{document}
\begin{algorithm}[h]
\caption{my algorithm}
\label{alg:example}
\begin{algorithmic}
\STATE \REQUIRE \(F, G, j\)
\STATE \(B_{1} = \{b_{1}[1],\dots,b_{1}[|g_r|]\}\)
\STATE \(B_{1}\)
\FOR{\(i=1\) \TO \(n\)}
\STATE DO smoething...
 \STATE  
\ENDFOR
\ENDFOR
\end{algorithmic}
\end{algorithm}
 \end{document}

The problem is that the algorithm is one page long, so I tried to put the h argument, and then after the algorithm a \newpage, so that everything else comes after it. But still it just ignores all my command and puts in on the last page, no matter what I try :S

Best Answer

Replace \begin{algorithm}[h] by \begin{algorithm}[H] in your code.

It would place the float precisely (instead of approximately) at the location in the LATEX code. See float specifiers.

Note that there are some drawbacks may appear when use [H] specifier Drawbacks of the [H] specifier. However, it works without any problems in my code (which is too long to attach here, according to the nature of the problem).

Alternatively, the specifier [!htbp] works also and without wasting any page area before the algorithm position.

Both specifiers works in my code without any errors.

Related Question