0

I'm trying to keep floats contained in their respective sections in a book environment. However, using the placeins package, I get this weird behavior where the floats flushed at the end of the document take up an entire page, regardless of their size, as shown below: enter image description here

I would like instead to have the flushed floats laid out as compactly as possible, and the next section starting on the same page, below the last float, if there's enough space. How can I achieve that?

Please note that I haven't had much luck using \afterpage{\clearpage} either.

Here is the code to reproduce that document (placeholder.png is just a square, as you can see in the first picture).

\documentclass[twoside, a4paper, 12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel} 
\usepackage{pdfpages}
\usepackage[section]{placeins}
\usepackage{lipsum}
\usepackage{graphicx}

\begin{document}

\chapter{First chapter}

\section{First section}

\lipsum[1-5]

\begin{figure}[h!] \centering \makebox[\textwidth][c]{\includegraphics[height=15 cm]{example-image}} \end{figure}

\begin{figure}[h!] \centering \makebox[\textwidth][c]{\includegraphics[width=0.5\textwidth]{example-image}} \caption{Caption.} \end{figure}

\section{Second section}

Second section content.

\end{document}

Banjo
  • 123
  • 1
    placeins is "if there are pending floats, then \clearpage" so what you show is the expected outcome. you are forcing the problem to occur by using [h] so not allowing the float to be placed at the top or bottom of a page or on a page of floats unless it is forced out by \clearpage. – David Carlisle Nov 28 '22 at 13:44
  • Thanks for the explanation! I did not think of that as there were some cases (for exemple having both figures at 0.5 textwidth) where I obtained the expected result. In the example above though, I need to remove [h!] in both figures to achieve the expected results. This is not a good thing for my actual document: if I were to remove the [h!] everywhere, I'd end up with a lot of figures in the wrong/less than ideal places. What would you recommend I do? – Banjo Nov 28 '22 at 13:57
  • using [h!] everywhere is simply wrong. Using ! everywhere is wrong is means "ignore the user set constraints for this one special case" ignoring your own constraints in every case is weird. Using h everywhere means floats are not allowed at top of page (no t) or bottom of page (no b) or on a page of floats (no p) so basically you are not allowing them to be placed anywhere so they will all drift to the end unless you flush them out with \clearpage – David Carlisle Nov 28 '22 at 14:04
  • we can not run your example as it has no documentclass and uses unavilable images, to please fix to use article class and example-image (which is available for tests) and check the problem occurs in that form – David Carlisle Nov 28 '22 at 14:09
  • unrelated but you can delete both \makebox[\textwidth][c]{ which are doing nothing useful. – David Carlisle Nov 28 '22 at 14:20
  • I have edited my initial post to include the class and example-image. I'm trying to fix the problem within the book class so I'm sticking with that. Thanks for the comments about [h!] and \makebox[\textwidth][c]{, I'll keep that in mind. Before making further comments, I'm trying to read a bit more about [h!] and figure placement, so that I can understand better what I'm supposed to do on the actual document. – Banjo Nov 28 '22 at 14:45
  • So, after refreshing my memory about figure placement (here for instance https://tex.stackexchange.com/questions/8652/what-does-t-and-ht-mean) : is using h by itself really that problematic? I just want to have the figures as close as possible to the relevant parts of the text. Apart from the issue mentioned in my post, I have no problem with the figure placement in my actual document : they're at the top or the bottom if need be, even when using [h!] everywhere. Not sure why ! is that problematic either, it just seems to lift some LaTeX restrictions. – Banjo Nov 28 '22 at 15:11
  • If you want them as close as possible use [htbp]. If you just use [h], it is internally made into [ht] and then the algorithm kicks in. First it looks once to see if it can be placed as an h float, this means that there needs to be text above and below. If that is not possible h is removed, and we look at the rest. Her we have t, now if the float is too large to be a t float is is placed back into the queue and since t is the only possibility t is tested (and fails) on each page until the end of the document or the next \clearpage. This is where the p placement is relevant. – daleif Nov 28 '22 at 15:22
  • no thy will never be at the bottom nor top of current page nor on float page, You probably get a warning that h has been changed to ht so as error recovery it will be allowed at the top of later pages. But not including p makes it much harder to set a float and much more likely it drifts to the end. Yes ! drops constraints but normally yu do not want them dropped (eg allowing just a single line of text under a float) and in cases where you do want to relax the constraints, better to set relaxed values than set strict values you then explicitly ignore – David Carlisle Nov 28 '22 at 15:22
  • If you're using [htbp], then if h and then t and b fails, then at each new page p is checked and the figure will be placed as soon as possible. – daleif Nov 28 '22 at 15:23

0 Answers0