1

Fairly minimal working example (varioref is not necessary, but its output really twists the knife IMHO):

\documentclass{article}

\usepackage[letterpaper, margin=1in]{geometry}
\usepackage{needspace,mwe,varioref}

\renewcommand{\topfraction}{0.99}

\begin{document}

% This is just here to take up space, could be text instead
\includegraphics[height=5in]{example-image-a}

\needspace{10\baselineskip}
\vref{fig:foo}

\begin{figure}[t]
  \centering
  \includegraphics[height=3in, width=\textwidth]{example-image-b}
  \caption{foo}
  \label{fig:foo}
\end{figure}

\begin{itemize}
\item foo
\item bar
\item baz
\item blee
\end{itemize}

\end{document}

This puts the float with example-image-b at the top of page 1, then example-image-a, then there is a page break and the \vref appears on the next page as "1 on the preceding page."

Why is my float appearing at the top of page 1 when the surrounding text is on page 2, the previous page? And, perhaps more importantly, how should I fix this?

I am guessing that it's something to do with \needspace. All TeX.SE hits for float placement either don't address the float appearing on a previous page, or else say that this is not possible! For example, the excellent answer in How to influence the position of float environments like figure and table in LaTeX? says that a float, "[can] not end up on an earlier page as the surrounding text due to the fact that LaTeX does no backtracking an the earlier pages have already been typeset." (Though perhaps I'm not reading this correctly.)

This MWE is, of course, somewhat contrived. In my real document \topfraction is less than 0.99, and I'm using the default float placement (tbp in article IIRC), but I am nonetheless having a float appear at the top of a previous page after using \needspace on the paragraph preceding the definition of the float. My actual document uses \needspace to keep a short introductory paragraph together with the list it introduces.

EDIT: David Carlisle suggested replacing \needspace with \Needspace, which does put the text on the right page, but which then fails to keep my paragraph and my list on the same page, which was the original intent. MWE updated with increased height to example-image-a. Now, using \needspace still behaves as before, with the \vref being pushed to the page after the page where the float is placed at the top, but if you replace \needspace with \Needspace it now puts the \vref paragraph at the end of the first page, and its accompanying list on the second page, which is undesirable [in my real document where I want the short paragraph to introduce the list].

  • you shouldn't really need [nN]eedspace it is more of a sticking plaster for when things go wrong than something to be part of a planned use. Your list environment (like section headings) should ensure there are no page break points in the introductory paragraph if that is what you want. – David Carlisle Oct 4 '17 at 22:45
  • It seems like the answer here is (1) LaTeX isn't putting my float on the previous page, it's putting the surrounding text on the next page (technically correct—reportedly the best kind of correct); and (2) to fix this I should do something other than use needspace. Other TeX.se answers tell me that "something other" is rewriting my document, or use of samepage, or \@beginparpenalty=10000 before this list, I think. I'll accept Mr. Carlisle's answer since he at least explained what was going on. – dsedivec Oct 7 '17 at 17:50
2

You can use \Needspace rather than \needspace then it all comes on one page.

\needspace uses a quicker algorithm to guess the space allowed and it is "confused" by the as yet unreached float so forces a page break after that float is inserted. It isn't that the float moves to a previous page (latex floats can not do that) it is that a page break was forced to push the [1] to the next page.

  • I just edited my question because I thought that \Needspace wasn't keeping my paragraph with the following list, even with something like \Needspace{20\baselineskip}. But is the problem here that I need to make \Needspace reserve something like enough space for my paragraph, some/all of the following list, and also space for the float? \Needspace{30\baselineskip} in the MWE does cause the float and its surrounding text to be pushed to the next page, but that's much more space than I imagined needing. Thanks! – dsedivec Oct 4 '17 at 22:37

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.