102

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} 
3
  • 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.
    – user100599
    Mar 13, 2016 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, 2017 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, 2022 at 16:13

4 Answers 4

118

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

6
  • 3
    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? May 17, 2012 at 2: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. May 17, 2012 at 2: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? May 17, 2012 at 2: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). May 17, 2012 at 2:45
  • 3
    @mortenstarck I've updated my answer giving a simple solution to your problem. May 21, 2012 at 0:39
34

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

5
  • 8
    I'm not sure that this would usually be the output people are looking for....
    – jon
    Mar 26, 2016 at 5:02
  • 9
    @jon Agreed, but in a less exaggerated form it might be useful to someone out there. It is a big world... Mar 26, 2016 at 18:10
  • 7
    it is a big world indeed, this was useful thank you!
    – Georges
    Jan 26, 2017 at 12:54
  • 4
    This is helpful for me beside the first answer. I use both. Apr 15, 2017 at 22:29
  • 2
    Both of these techniques worked great for me, while the accepted answer did not.
    – Nick S
    Feb 18, 2019 at 18:48
7

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.

1
  • 1
    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 Bot
    Sep 20, 2021 at 1:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .