10

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

6
  • 1
    Welcome to TeX.SX! Obviously algorithm is an environment with floating enabled.
    – user31729
    Commented Oct 5, 2014 at 11:39
  • What Christian means is that you shouldn't specify h in the optional argument of the algorithm environment. Let that environment "float".
    – jub0bs
    Commented Oct 5, 2014 at 11:42
  • @Jubobs: Yes, I was too quick ;-) More over, the code does not compile at all
    – user31729
    Commented Oct 5, 2014 at 11:43
  • Ok sorry for putting an uncompilable code there, I just tried to copy as fast as possible a short example. my fault. Oh wow it worked... But isn't figure also an floating environment, because I-ve seen a lot of examples using the htb arguments for figures?
    – user63716
    Commented Oct 5, 2014 at 11:48
  • [h] tells latex the float isn't allowed at the top of a page (as no t) or bottom (no b) or on a page of floats (no p) which gives latex very few places to place it so holding it to the end is quite likely Commented Oct 5, 2014 at 11:49

1 Answer 1

2

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.

6
  • 1
    [!] will only work by coincident because ! is on it's own no valid option for the floating specifier and will issue a warning and be replaced with the default option !htbp Commented Mar 29, 2019 at 19:03
  • According to this blog, it works on determining "good" float positions. It did not issue a warning in my code, and I searched for what is possible problems associated with [!] and did not find there would be any warning issues. Commented Mar 29, 2019 at 19:30
  • ! can only be used in combination with other floating specifier, such as h, t, b or p, not on its own. You'll get a warning with LaTeX Warning: No positions in optional float specifier. Default added (so using !htbp) on input line 8. Commented Mar 29, 2019 at 21:12
  • Yes, you are right. I just found this warning. However, the pdf is generated as required. Commented Mar 30, 2019 at 2:40
  • As the warning tells you, the missing arguments are replaced with the default ones, that's why the pdf is generated as it should. So instead of suggesting to use [!] in your answer, it might be better to suggest [!htbp] because this is the configuration which is actually used. Commented Mar 30, 2019 at 13:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .