110

Is there a way to avoid having a picture that trails off the end of a page, as in this example?

enter image description here

This is how I wrap the picture:

\begin{wrapfigure}{r}{0.5\textwidth} 
\vspace{-20pt}
  \begin{center}
    \includegraphics[width=0.4\textwidth]{./pictures/DBuserTabel.png}%{./Pictures/mainscreen1.png}
    \caption{Uklip af User tablen i Databasen}
    \label{fig:databaseUserTable}
  \end{center}
  \vspace{-20pt}
  \vspace{1pt}
\end{wrapfigure} 
mortenstarck
  • 2,027
  • I have the same issue. But combining the methods explained by Avi Ginsburg and Gonzalo Medina and the use of the new line command \ before the wrapfigure environment solved my problem. –  Mar 13 '16 at 19:45
  • 2
    You rather want to use \centering over \begin{center}[...]\end{center}, because the environment produces whitespace which one usually don't want in your figures. #tookmeyearstofindout – Bananguin Mar 27 '17 at 13:16
  • What helped for me, is just placing the \clearpage command before my wrapfigure and the text paragraph that comes after that. This way, the entire thing is guaranteed to start on a new page, which solves this problem. – Marie M. Mar 23 '22 at 16:13

4 Answers4

124

The behaviour you describe is caused by using the wrapfig environment too close to a page break, as the following example demonstrates:

\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}

\begin{document}

\lipsum[1-4]
\begin{wrapfigure}{r}{5cm}
\centering
\rule{3cm}{7cm}
\end{wrapfigure}
\lipsum[1-6]

\end{document}

enter image description here

The wrapfig package documentation explicitly warns about this:

The environment should be placed so as to not run over a page break

so, you need to move your wrapfig environment to guarantee that it won't run over a page break. However, using R (or L) instead of r (or l) your figure will float, so simply changing r to R in the above code, as in

\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}

\begin{document}

\lipsum[1-4]
\begin{wrapfigure}{R}{5cm}
\centering
\rule{3cm}{7cm}
\end{wrapfigure}
\lipsum[1-6]

\end{document}

now yields:

enter image description here

Gonzalo Medina
  • 505,128
  • 4
    Okay. So LaTeX doesn't have any automation, that does this automatically. So the solution if i understand you correct, is to do it manually replace the pictures? – mortenstarck May 17 '12 at 02:28
  • 1
    @mortenstarck:in this concrete case (wrapfig package), if the environment runs over a page break, I am afraid that you will have to manually move the environment; of course, those changes must be best done when working on the final version of the document. – Gonzalo Medina May 17 '12 at 02:33
  • 2
    Is there another way of doing it, there it does page break in consideration. Because it's an quit big repport, with quit afew pictures? – mortenstarck May 17 '12 at 02:38
  • @mortenstarck: there are other packages around for wrapping text around elements (cuwin, for example), but I've never used them, so I don't know if they can automatically handle page breaks. Perhaps you should start a fresh new question about this new issue. (Don't forget to revisit this question a little later and accept the answer that best solved the concrte issu of this question). – Gonzalo Medina May 17 '12 at 02:45
  • 3
    @mortenstarck I've updated my answer giving a simple solution to your problem. – Gonzalo Medina May 21 '12 at 00:39
  • Using R instead of r works fine out-of-the-box, unless there's too few high-enough paragraphs - in that case, the figure may jump somewhat too far. In that case, you might also need a \Floatbarrier equivalent. – Igor Jul 20 '18 at 01:34
37

The accepted answer offers one method (make the figure float). If all you need is to remove the white box on the subsequent page, you can just add negative spacing. Adapting Gonzalo Medina's example:

\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}

\begin{document}

\lipsum[1-4]
\begin{wrapfigure}{r}{5cm}
  \centering
  \rule{3cm}{7cm}
  \vspace{-110pt} % This removes the white box on the second page
\end{wrapfigure}
\lipsum[1-6]

\end{document}

Alternatively, you can count the number of rows on the first page the wrapfigure occupies and insert that number in the first optional parameter of the wrapfigure:

\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}

\begin{document}

\lipsum[1-4]
\begin{wrapfigure}[10]{r}{5cm}
%                  ^^ This dictates the number
%                   of text rows the wrapfigure
%                   will occupy.
  \centering
  \rule{3cm}{7cm}
  \vspace{-110pt}
\end{wrapfigure}
\lipsum[1-6]

\end{document}

Both of these methods result in the following output:

enter image description here

8

If you want non-floating wrapfig environments but you don't want the figures to ever extend off the bottom of the page then than means you sometimes have to have a page break before the start of the paragraph. You can do this automatically by defining a command (in the preamble) that stores the figure content in a savebox, tests the height of the box, then forces a page break if necessary. See the following example:

\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}

\newsavebox\curwrapfig
\makeatletter
\long\def\wrapfiguresafe#1#2#3{%
  \sbox\curwrapfig{#3}%
  \par\penalty-100%
  \begingroup % preserve \dimen@
    \dimen@\pagegoal \advance\dimen@-\pagetotal % space left
    \advance\dimen@-\baselineskip % allow an extra line
    \ifdim \ht\curwrapfig>\dimen@ % not enough space left
      \break%
    \fi%
  \endgroup%
  \begin{wrapfigure}{#1}{#2}%
    \usebox\curwrapfig%
  \end{wrapfigure}%
}
\makeatother

\begin{document}
\lipsum[1-4]
\wrapfiguresafe{r}{0mm}{\centering\rule{3cm}{7cm}}
\lipsum[1-6]
\end{document}

which produces the following output:

wrapfiguresafe example

Be warned that, if your figures are tall, this can cause some very bad page breaks with short pages and/or, depending on your settings, result in underful vbox warnings.

0

Use the package that provides the command \FloatBarrier and put that after the paragraph where you want the figure to appear, and use l, r not L,R to place the figure left or right so that it floats. Don't make the figure too big. Maybe I am lucky, but I have yet to have trouble with a page break.

  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Sep 20 '21 at 01:00