[Tex/LaTex] How to forbid LaTeX to put text between figures


I have some figure, which I want to have one right after another in the document.
Is it possible to forbid pasting anything inbetween, even if there is some space?



Best Answer

I assume that you want to create two individual floats rather than one float that combines two distinct images, in part so that you can (i) assign separate captions and labels to the two figures, (ii) preserve the capability of creating separate cross-references to them, and (iii) preserve compatibility with the hyperref package. If, however, you're OK with placing two images within a single LaTeX float (which also ensures, of course, that they'll never be separated), you should follow the suggestions provided in the answer of @cmhughes instead.

Meeting the objective expressed in the preceding sentence and ensuring that the two floats will be placed consecutively (i.e., without any text between them) is, I believe, not possible in general if there are no constraints whatsoever on the size of the figures. To wit, if each figure's height is greater than 0.5\textheight, then there's no way LaTeX can place them on the same page...

For the sake of this discussion, I'll therefore assume that the figures' combined height does not exceed the LaTeX parameter \floatpagefraction. The default value of this parameter in the article document class is only 0.5, meaning that if the height of a float (or of a combination of floats) exceeds 0.5, the float(s) will be placed on a separate page. Aside: I would recommend that you issue the commands


in your document's preamble. This way, LaTeX will permit some text to coexist with floats as long as the floats' combined height doesn't exceed 85% of \textheight.

After this lengthy introduction, what can be done to answer your question? I will go ahead and make the further assumption that the two floats that should be placed together, without intervening text, should be placed at the top of the page. The following code, which uses the \afterpage command of the afterpage package, will do the trick for you:

  % Begin by clearing out all accumulated floats (if any) and relaxing some
  % of LaTeX's restrictions on how floats may be placed
\caption{This is the first of two consecutive figures}\label{fig:firstoftwo}

\caption{This is the second of two consecutive figures}\label{fig:secondoftwo}
}    % end of \afterpage{...} group

By assumption, the two figures fit on a single page and, moreover, their combined height is less than \floatpagefraction. (You have already increased the values of \topfraction and \floatpagefraction, haven't you?) The first \clearpage instruction clears out any already-pending floats, while the second \clearpage command ensures that we are indeed going to start at the top of the page. (If there are no pending floats, both the first and second \clearpage instructions are ignored because we're already at the top of the page.) Observe the t and h placement parameters on the two floats: they will be applied successfully in the present case, with no intervening text, because you're guaranteed to be at the top of a page (because of the \afterpage statement and the \clearpage commands).

By the way, you can change the distance between the two adjoining floats by changing the value of the length parameter \floatsep (default value in the article document class is the main text's font size is 10pt: "12pt plus 2pt minus 2pt"). If you want to increase the vertical separation between the floats, you could select, say,

\setlength\floatsep{1.5\baselineskip plus 0.25\baselineskip minus 0.25\baselineskip}

It's a good idea to keep this length a "rubber" length, by the way, as the presence of rubber lengths greatly facilitates LaTeX's job of finding decent page breaks.

Related Question