# Wrapfig and page breaks after section heading

I'm experimenting with typesetting "pull quotes" using wrapfig and noticed some strange behaviour. Under certain conditions, having a wrapfigure at the beginning of the first paragraph after a new section (subsection, etc), LaTeX will sometimes break the page between the section header and the following text/wrapfigure block.

This is of course unacceptable. Any ideas how to prevent it? (The page break also destroys spacing in the wrapfigure, remove the \lipsum[1-4] for comparison.)

MWE

\documentclass{article}
\usepackage{tikz}
\usepackage{wrapfig}
\usepackage{lipsum}

\begin{document}

\lipsum[1-4]
sdjfkljsdf kjsd kdsfj sadlkfj sdjfskldf lskdjf lksdjf klsdf
sklfjsa dklf sdjfkljsdf kjsd kdsfj sadlkfj sdjfskldf lskdjf lksdjf klsdf
sklfjsa dklfsdjfkljsdf kjsd kdsfj sadlkfj sdjfskldf lskdjf lksdjf klsdf
sklfjsa dklf sdjfkljsdf kjsd kdsfj sadlkfj sdjfskldf lskdjf lksdjf klsdf
sklfjsa dklf sklfjsa dklf sdjfkljsdf kjsd kdsfj sadlkfj sdjfskldf lskdjf
lksdjf klsdf sklfjsa dklf sdjfkljsdf kjsd kdsfj sadlkfj sdjfskldf lskdjf
lksdjf klsdf sklfjsa dklf sklfjsa dklf

\section{Section}

\begin{wrapfigure}[5]{R}{50mm}
\fontsize{13}{17}\selectfont\vspace*{-10pt}
\hspace*{4pt}\begin{tikzpicture}
\node[anchor=north east,inner xsep=1pt] at (0,12pt) {\fontsize{32}{32}\selectfont''};
\node[anchor=north west,text width=40mm, align=left, inner sep=0pt] at (0,4pt) { Pull       quote for a nice touch };
\end{tikzpicture}
\end{wrapfigure}
\lipsum[1-2]

\end{document}


You can give wrapfigure a bit of a hint that things are not quite the same after a section heading

\section{Section}
\pagedepth\maxdimen
\begin{wrapfigure}[5]{R}{50mm}


seems to work and pushed the section head on to the next page.

• How exactly does this work? I guess \pagedepth contains the depth of the section heading's box? And \maxdimen is just 16383.99998pt, so what does \pagedepth\maxdimen change precisely? I mean, it works perfectly, but I'd like to know why. :) – Wouter May 4 '17 at 11:40
• @Wouter \pagedepth is an internal register that tex updates as things are added to the page, so it's only a very transient setting, but it lasts long enough to be in force at the start of wrapfig code where it checks to see if it is near the bottom of a page and if so it forces a page break. So any value close to or more than \textheight would do, \maxdimen is overkill but works whatever the page size is. – David Carlisle May 4 '17 at 12:54
• So does \pagedepth\maxdimen actually change the value stored in \pagedepth to that of \maxdimen? When I first found your answer to this question I just assumed it did and that the section head got pushed to the next page because its 'box' then looked deep enough for wrapfig to wrap around. But upon actually thinking about it, in my limited interpretation, I would expect it to evaluate simply to something like 15pt16383.99998pt. I feel like I'm missing a crucial understanding here. – Wouter May 4 '17 at 21:19
• @Wouter yes but only temporarily, and only just as a way of forcing this test in wrapfig : \ifdim\@tempdimb<\z@ % \WF@info{Page overfull already;}% to decide that there is not room on the page. – David Carlisle May 4 '17 at 21:22
• @Wouter you could use \setlength, \setlength\zz{...} is just a simple macro that expands to \zz=... Knuth's texbook is the definitive reference for pagedepth etc. – David Carlisle May 4 '17 at 21:28