# Wrapfig doesn't detect new page

MWE:

\documentclass[a4paper]{article}
\usepackage{mwe}
\usepackage{wrapfig}
\usepackage{blindtext}

\begin{document}

\blindtext \blindtext \blindtext \blindtext

\begin{wrapfigure}{l}{0.5\textwidth}
\centering
\includegraphics[height=150pt,width=0.5\textwidth]{example-image-a}
\caption{A caption}
\end{wrapfigure}

\blindtext

\end{document}


PICTURE

I want wrapfig do detect new page and start the text from the left margin of the page. Instead, the margin is such like the figure was still there

I would prefer an automated solution, as not to manually specify the number of narrow lines using the optional argument of wrapfig. I frequently change the text before the wrapfig.

Use package needspace and then:

\documentclass[a4paper]{article}
\usepackage{mwe}
\usepackage{wrapfig}
\usepackage{blindtext}
\usepackage{needspace}

\begin{document}

\blindtext \blindtext \blindtext \blindtext

\Needspace{150pt}%% Same as image height
\begin{wrapfigure}{l}{0.5\textwidth}
\centering
\includegraphics[height=150pt,width=0.5\textwidth]{example-image-a}
\caption{A caption}
\end{wrapfigure}
\blindtext

\end{document}


I used only the image height for \needspace. In reality it should include the caption height.

• I didn't know that package. Thanks! For completeness, needspace inserts a pagebreak if there is not enough space – rtrtrt Jan 22 '16 at 11:18

You need L not l to specify that wrapfig may move the figure to a place where it fits

\documentclass[a4paper]{article}
\usepackage{mwe}
\usepackage{wrapfig}
\usepackage{blindtext}

\begin{document}

\blindtext \blindtext \blindtext \blindtext

\begin{wrapfigure}{L}{0.5\textwidth}
\centering
\includegraphics[height=150pt,width=0.5\textwidth]{example-image-a}
\caption{A caption}
\end{wrapfigure}

\blindtext

\end{document}

• This put the figure at the end of the document. I want the figure to start right where I put it, with the text on the new page not wrapping around – rtrtrt Jan 16 '16 at 14:47
• @raffamaiden it can not go there it does not fit! The wrapfig documentation makes it clear that if you use the non floating form then it is the users responsibility to ensure that you are far enough from the end of the page that the image will fit on the page, which is not the case here. – David Carlisle Jan 16 '16 at 15:17

Just for fun:

\documentclass[a4paper]{article}
\usepackage{mwe}
\usepackage{caption}
\usepackage{blindtext}

\begin{document}

\blindtext \blindtext \blindtext \blindtext

\noindent\rlap{\raisebox{-\height}[0pt][0pt]{\begin{minipage}{0.5\textwidth}
\centering
\includegraphics[height=150pt,width=\textwidth]{example-image-a}
\captionof{figure}{A caption}
\end{minipage}}}

\hangindent=\dimexpr 0.5\textwidth+\marginparsep\relax
\hangafter=-8
\blindtext

\end{document}