13

I used the code from here to get text to wrap around images, but whenever I have a long image near the end of the page, it runs off the page and gets cut off. How do I fix this?

lockstep
  • 250,273
howard
  • 453

2 Answers2

16

When positioning the wrapped float, you can specify uppercase L or R for the position, instead of lowercase l, r. This allows the wrapfigure environment to float and LaTeX can move it to avoid page overflow.

yo'
  • 51,322
  • Hmm, It does, but in my case it moved the figure to the end of the document, which I consider a bit excessive... – JHBonarius May 08 '18 at 09:55
3

You could add the following to your document preamble:

\makeatletter
\newcommand{\checkheight}[1]{%
  \par \penalty-100\begingroup%
  \setbox8=\hbox{#1}%
  \setlength{\dimen@}{\ht8}%
  \dimen@ii\pagegoal \advance\dimen@ii-\pagetotal
  \ifdim \dimen@>\dimen@ii
    \break
  \fi\endgroup}
\makeatother

and then use

\checkheight{\includegraphics[..]{...}}
\begin{wrapfigure}...\end{wrapfigure}

to check whether the image will fit on the page or not. If it does/will fit, nothing will happen. If it does/will not fit, a break (page or column) is issued, so the next paragraph will start on a new page.

The above code was taken from the minimal needspace package.

Werner
  • 603,163
  • 1
    I don't want to be tough, but it does mean that one has to include the graphics twice, first to check its siye and then to include it? – yo' Jan 27 '12 at 19:13
  • @tohecz: That's correct. I'm not sure whether this requirement is an isolated instance, and therefore a separate commands seems viable. If not, and this check has to be included with each, it would be possible to include such a check as part of the wrapfigure environment. – Werner Jan 27 '12 at 19:15