wrapfig and \hrulefill not as expected

My document has a number of \hrulefill for a student to write about a picture which is inset using the wrapfig package.

However, the rules underneath the figure don't extend far enough-as if the figure keeps on extending downward: When I replace the rules with any text, the wrapfig works fine. It is just the \hrulefill that doesn't behave.

How do I fix it?

MWE:

\documentclass{article}
\usepackage{wrapfig}
\setlength{\parskip}{8ex}
\setlength{\unitlength}{1cm}
\begin{document}
\hrulefill

\hrulefill

\begin{wrapfigure}{r}{0.5\textwidth}
\begin{center}
\framebox(5,5)[c]{some picture}
\end{center}
\end{wrapfigure}
\hrulefill

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\hrulefill
\end{document}

• Wrapfig has a problem with anything which takes more space than \baselineskip per line of text. You can terminate the wrapping using \WFclear. – John Kormylo May 21 '18 at 18:40

Use the optional argument of wrapfig which sets the number of shortlines:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{wrapfig}
\setlength{\parskip}{8ex}
\setlength{\parindent}{0pt}
\setlength{\unitlength}{1cm}
\usepackage{lipsum}

\begin{document}

\hrulefill

\hrulefill

\begin{wrapfigure}[4]{r}{0.5\textwidth}
\centering
\framebox(5,5)[c]{some picture}
\end{wrapfigure}

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\end{document}


Edit : A more automatic solution can be obtained with the plain TeX macro package insbox. The \InsertBoxR command has to be inserted before a paragraph begins. It takes two mandatory arguments: the number of untouched lines before the object in the box is inserted, and the inserted object. As it may happen TeX gets a wrong box height, an optional argument can be use (at the end of the command, which is the number of supplementary shorter lines. Here is a demo with a real graphic file:

    \documentclass{article}
\usepackage{geometry}
\usepackage{graphicx}
\parskip = 8ex
\setlength{\parskip}{8ex}
\setlength{\parindent}{0pt}
\setlength{\unitlength}{1cm}
\input{insbox.tex}

\begin{document}

\hrulefill

\hrulefill

\vspace*{\parskip}
\InsertBoxR{0}{\parbox[t]{0.48\linewidth}{\centering
\includegraphics{pepe-le-pew2}}}%

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\hrulefill

\end{document}


• Either this solution or the use of \WFclear (@John Kormylo) requires me to determine the height of the figure in terms of lines. Unfortunate this is not automatic, but at least for me that's not an issue. – Tim May 22 '18 at 13:57
• @Tim: I've added another, more automatic solution. – Bernard May 22 '18 at 14:56