9

I'm writing a document where I have a lot of long listings. I use the package minted to typeset the listings, which I always embed in a float. Since the listings are quite long, and LaTeX usually does not place the floats correctly, I also use the package afterpage to force a \clearpage after each long listing.

Unfortunately I sometimes get a message from LaTeX saying that

... Float(s) lost
... This may be a LaTeX bug.

It seems that this occurs if the float / afterpage command is located right after the first paragraph on a page. Below is a minimum working example that should trigger the error message (compile with latex -shell-escape example.tex):

\documentclass[a4paper,11pt]{report}
\usepackage{afterpage}
\usepackage{minted}
\begin{document}
a

a 

% ...
% Insert as many paragraphs 'a' such that 'begin' below appears
% as the first line on 2nd page (in my case I need 44 x 'a').
%

a

begin

\afterpage{\clearpage}
\begin{figure}[p]
abracadabra
\end{figure}

end

% Even adding more text below won't help

\end{document}

Note that problem disappears if package minted is not included, even though minted is not used.

My question is how can I work around this? What am I doing wrong?

I found a way, which is simply to relocate the float in the text, like one paragraph above or below. But this is not handy since editing further the text might again require to change the float location. Note that I'm ok to replace \afterpage as long as the new package provides similar functionality.

EDIT: The code above is just for triggering the problem. My actual "use case" is to replace the figure float with a listing float:

\begin{listing}[p]
\begin{minted}[gobble=2]{python}
  print "Hello, World!"
  #
  # Typically much
  # ... much
  # ... longer listing (like 3/4 of a page)
  #
\end{minted}
\end{listing}

EDIT2: I updated the float placement option to tell LaTeX we want the float on a separate page (i.e. [p]). Actually the problem occurs whatever the options I use. Also, I would happily use [p] without \afterpage{\clearpage} if LaTeX would not insist on postponing all my floats 10 pages away.

  • 1
    There is absolutely no point at all in using a floating environment and then stopping it float using \afterpage{\clearpage} either let it float or don't put it in a float. and use \includegraphics with figure – David Carlisle Feb 27 '14 at 9:25
  • @david I don't understand, could you please elaborate a bit more? I'm using a float to avoid having the listing cut on 2 pages, and to add a caption. Also, I thought that \afterpage{\clearpage} was exactly for this purpose, i.e. force TeX to place the float on the next page, without interrupting current text flow. If I don't use clearpage, then TeX pushes all my big floats until end of chapter, sometimes 5 pages away. – fuujuhi Feb 27 '14 at 10:08
  • It is almost bound to move the float if you use [h] since that means "don't allow this float to go at the (t)op (b)otton or on a (p)age float, which means that the float is allowed hardly anywhere. If you want to caption a non float use \caption{of}{figure} from the capt-of package. – David Carlisle Feb 27 '14 at 10:25
5

I guess answering my own question is pretty ugly, but following some exchanges with David, I just found a way to solve my problem above. I just force LaTeX to put the float on the next page, i.e. using specifier [p!]

%\afterpage{\clearpage}              % No longer needed
\begin{listing}[p!]                  % Force float on next page
\begin{minted}[gobble=2]{python}
  print "Hello, World!"
  #
  # Typically much
  # ... much
  # ... longer listing (like 3/4 of a page)
  #
\end{minted}
\end{listing}

EDIT: Using [p!] seems better than forcing the flush with clearpage, so I leave this as an answer.

However it turns out that using [htpb] as placement specifier for all my listings also work. I think it did not work initially in my tests because I probably forgot one listing with specifier [htb], which was then pushing all listings up to the end.

\begin{listing}[htpb]                % Give all freedom to LaTeX
\begin{minted}[gobble=2]{python}
  # ...
  # ...
  # ...
\end{minted}
\end{listing}
  • 2
    p would be allowed if you used no option at all (previously you were using [h] the main effect of which is to not allow p. Using ! just means that you specify that the constraints on size of floats allowed on a page be ignored, so rather than habitually use ! it is far better to simply change the specified constraints (such as \floatpagefraction so that latex allows things that you want to allow) – David Carlisle Feb 27 '14 at 12:35
  • 2
    LaTeX does not prefer to move the float 10 pages, you specified that it could not go on the next page by using h and then over-ruled that specification with \clearpage (which dumps all pending floats as an emergency recovery action – David Carlisle Feb 27 '14 at 12:37
  • I've edited the answer to reflect your comments. Your explanations is not fully inline with my previous tests but I probably made a mistake somewhere. – fuujuhi Feb 27 '14 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.