[Tex/LaTex] Wrapfigure displayed at wrong place after some page breaks

alignmentincludegraphicssectioningwrapfigure

I have troubles with displaying right-aligned portraits wrapped by text. These images shall be displayed at the begin of (some) subsections.

Displaying itself was also full of bugs, but in older threads I've got several advices how to solve them. Unfortunately, it's still not perfect.

In most cases it works correctly, but few images out of several hundreds are still overflowed to next subsection although there is enough space at the page (in fact, it always happens at the page begin).

I've observed that it has something to do with page breaking. Not everytime, because as I mentioned, at least 90 % of images are displayed correctly, but in some cases.

Screenshot - correct behaviour
Screenshot - wrong behavious

It seems that issue occurs if previous text ends with enumeration and not normal text. My attempt to force normal text using ~\newline\vspace{-11pt} after ending enumeration and before \needspace initially seemed to work, most incorrectly placed images were then fixed, but there still persist some wrong positioned images which overflowed to next subsection (or in some case to the same subsection, but they were displayed below last text in subsection and additionally aligned to left without wrapping instead of to right with text wrapping).
So the ~\newline\vspace{-11pt} is definitely no solution although there are less incorrectly displayed images than before.

Another suspicious matter could be footnotes at previous page (page height is decreased), but it happens also without footnotes, so I haven't simulated them in MWE.

Whole issue is reproduced in below code:

\documentclass[a4paper]{book}

\usepackage{fontspec}

\usepackage{polyglossia}
\setmainlanguage{czech}

\usepackage{ulem}
\usepackage{spverbatim}
\usepackage[unicode]{hyperref}
\def\do#1{\appto\UrlSpecials{\do#1{\mathchar`#1 \mskip 0mu plus 1mu\penalty100\relax}}}
\do\-\do\/\do\0\do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X\do\Y\do\Z
\usepackage[]{graphicx}
\usepackage{grffile}
\usepackage{color}
\usepackage{capt-of}

\usepackage{wrapfig}
\usepackage{float}
\usepackage[skip=8pt]{caption}
\usepackage[section]{placeins}
\usepackage{hologo}

\usepackage{pdfpages}

\usepackage{needspace}
\intextsep=0pt

\newcommand\img[4]{
\hspace{1pt}
\vtop{
\centering
\captionsetup{justification=centering}
\sbox0{\includegraphics[height=4cm,width=\textwidth,keepaspectratio]{#1}}
\hsize=\wd0
\linewidth=\hsize
\usebox{0}
\phantomsection\label{#3}
\captionof{figure}{\hyperref[#4]{#2}}
}
\ignorespaces}

\makeatletter
\renewcommand{\l@section}{\@dottedtocline{1}{1.5em}{2.6em}}
\renewcommand{\l@subsection}{\@dottedtocline{2}{4.0em}{3.6em}}
\renewcommand{\l@subsubsection}{\@dottedtocline{3}{7.4em}{4.8em}}
\makeatother


\begin{document}
\raggedbottom

\chapter{Chapter}
\section{Section}

\needspace{5cm}
\begin{wrapfigure}{R}{0pt} % 0pt = detect width
   \centering
   {\rule{4.5cm}{4cm}}
\end{wrapfigure}
\subsection{Subsection with portrait}
\label{ID_PER_001}
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline

\needspace{5cm}
\begin{wrapfigure}{R}{0pt} % 0pt = detect width
   \centering
   {\rule{4.5cm}{4cm}}
\end{wrapfigure}
\subsection{Subsection with portrait}
\label{ID_PER_002}
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline

\needspace{5cm}
\begin{wrapfigure}{R}{0pt} % 0pt = detect width
   \centering
   {\rule{4.5cm}{4cm}}
\end{wrapfigure}
\subsection{Subsection with portrait}
\label{ID_PER_003}
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
Force new page just to simulate the bug (in document there is no newpage)
\newpage

\subsection{Subsection without portrait}
\label{ID_PER_1326}
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{List:} \begin{enumerate}
\item{Item}
\end{enumerate}
\textbf{List:} \begin{enumerate}
\item{Item}
\end{enumerate}

\section{Section}

\subsection{Subsection without portrait}
\label{ID_PER_172}
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{Item:} Contents\newline
\textbf{List:} \begin{enumerate}
\item{Item}
\end{enumerate}
\textbf{List:} \begin{enumerate}
\item{Item}
\end{enumerate}

\needspace{5cm}
\begin{wrapfigure}{R}{0pt} % 0pt = detect width
   \centering
   {\rule{4.5cm}{4cm}}
\end{wrapfigure}
\subsection{Subsection with portrait}
\label{ID_PER_110}
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline

\subsection{Subsection without portrait}
\label{ID_PER_431}
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline
This is demo text.\newline

\end{document}

Please note that original document is complex and every line of initial section is really needed for it (although it is purposeless for this minimal working example) – almost all lines have been advised in this forum to solve other issues in rendering document. So, please don't delete any line in the beging of document, because it would definitely break something else in it (although it's invisible in above example).

Do you guess, why the image position doesn't work properly and how to display portraits at the correct place?

Best Answer

At the end I've found the solution myself.

First, I've found in wrapfigure documentation that it mustn't be placed after any list, or it shall be separated by empty line or \par. Unfortunately, placing empty line or even recommended \par doesn't work, so it seems the wrapfigure has a bug.

Nevertheless, for my purposes was solution simple: use {r} instead of {R}, because in my document it's alwayse ensured that 1) there is enough space for image at the page (ensured by \needspace) and 2) text is always higher than an image. Generally, I don't know how to solve it for any document, but for my particular document the issue is solved.