2

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?

  • There is probably a conflict between \@afterheading and wrapfigure, since both use \everypar. You can fix this by putting \subsection into a minipage and using \noindent on the first line of text. See also \wrapspacer from tex.stackexchange.com/questions/390520/… – John Kormylo Jan 19 at 5:35
  • Wrapfigure itself with text on the left side is really displayed correctly if wrapped by minipage, but it is not useable at least due to two reasons: 1) whole text inside minipage is not able to wrap page, so everything (in some cases tens of pages) overflows below text area 2) footnotes counters start from 1 and additionally they are identified by letters and not numbers as in rest of document. – Joseph Jan 19 at 9:24
  • I've also tried to place into minipage only wrapfigure with subsection title (not the text below subsection) to avoid overflowing text area (enough space is ensured by previous \needspace), but the behaviour was even worse than without minipage - image was still displayed below text and additionally aligned to left without text wrapping instead of right with text wrapping. – Joseph Jan 19 at 9:25
  • It turns out that most of the problems I was having were due to using \lipsum, which seems to have a problem with wrapfig. By replacing the \lipsum macro with the the corresponding text, those problems went away. – John Kormylo Jan 20 at 18:36
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.