Handling of wrapfig pictures in LaTeX
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}
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:
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:
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:
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.