# How to fix paragraph's run-in heading on a single page before \afterpage and the text being on the first page after \afterpage?

TeX Live 2014 on Xubuntu 14.04 LTS, compiling with xelatex.

Minimal working example:

\documentclass[11pt, twoside]{book}
\usepackage{geometry}
\geometry{a4paper, left=3.5cm, right=2.5cm, top=3.5cm, bottom=3.5cm}
\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{Linux Libertine O}
\setmonofont{Linux Libertine Mono O}
\usepackage{lipsum}
\usepackage{afterpage}

\begin{document}
\mainmatter
\lipsum[1-3]
\afterpage{
\clearpage
Here be landscape content.
\clearpage
}
\lipsum[4-5]

\paragraph{Paragraph Title}
Text spanning this and the two next lines. Text spanning this and the two next
lines. Text spanning this and the two next lines. Text spanning this and the
two next lines. Text spanning this and the two next lines. Text spanning this
and the two next lines.

\paragraph{Paragraph Title}
Text spanning this and the three next lines. Text spanning this and the three
next lines. Text spanning this and the three next lines. Text spanning this
and the three next lines. Text spanning this and the three next lines. Text
spanning this and the three next lines. Text spanning this and the three next
lines.

\paragraph{This paragraph breaks uglily}\label{mrkr}
\lipsum[2]

\end{document}


The document looks like this, mind the manually added labels for some explanation:

The first \paragraph to appear after \afterpage is cut into its run-in heading residing on its own page and the remainder after the \afterpage. What am I doing wrong?

PS: I had to add my own dummy paragraphs, because it was too cumbersome trying to get the right height with the \lipsum paragraphs.

• I was unable to reproduce your error. Filling the page using \rule instead of \lipsum, I would go from no break to a clean break after the first two lines (lipsum[2] is part of the paragraph). I should mention that I was also unable to locate your font, so turned off fontenc. – John Kormylo Dec 7 '15 at 17:40
• It was quite tricky to remove the relevant parts from the actual document and still see the problem. I suspect that the following conditions must be met: the label must separate the \paragraph and the text after it; the \paragraph must be at the end of a page, the text after it should break; the \paragraph must be preceded by at least one other \paragraph with some text. That would also explain why you are not seeing the problem with a \rule and without the font. – Kreuvf Dec 10 '15 at 13:27

While constructing the MWE from the actual ~100-page document I removed the \label from after the \paragraph and found that the unwanted page break was gone. Re-reading the corresponding pages in 'Guide to LaTeX' (p. 211-212, fourth edition) I stumbled upon this:

Note that the counter whose value is stored by the \label command depends on the context in which the command is given. This is normally fairly self-evident. Within regular text, it will be the counter and its value for the last-issued sectioning command, which might not be immediately visible. Therefore, when reference to a section number is desired, it is best to place the \label command immediately after the appropriate sectioning command, or even within its argument, as part of the title text. This is in fact the recommended method. Put the \label command within the body of the text only when you want to refer to the page number at that point.

The highlighting was added by me. So, doing just that, the MWE looks like this:

\documentclass[11pt, twoside]{book}
\usepackage{geometry}
\geometry{a4paper, left=3.5cm, right=2.5cm, top=3.5cm, bottom=3.5cm}
\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{Linux Libertine O}
\setmonofont{Linux Libertine Mono O}
\usepackage{lipsum}
\usepackage{afterpage}

\begin{document}
\mainmatter
\lipsum[1-3]
\afterpage{
\clearpage
Here be landscape content.
\clearpage
}
\lipsum[4-5]

\paragraph{Paragraph Title}
Text spanning this and the two next lines. Text spanning this and the two next
lines. Text spanning this and the two next lines. Text spanning this and the
two next lines. Text spanning this and the two next lines. Text spanning this
and the two next lines.

\paragraph{Paragraph Title}
Text spanning this and the three next lines. Text spanning this and the three
next lines. Text spanning this and the three next lines. Text spanning this
and the three next lines. Text spanning this and the three next lines. Text
spanning this and the three next lines. Text spanning this and the three next
lines.

\paragraph{This paragraph breaks nicely\label{mrkr}}
\lipsum[2]

\end{document}


The final document will look like this:

So, now we know why that is the recommended way of doing things.

I found the same issue, but triggered by a string \paragraph{Name}\index{entry}. When recoded as

    blah blah blah

\afterpage{....big landscape figure}

blah blah blah

\paragraph{Name of paragraph\index{entry}} blah blah blah


the problem vanished.