# Miktex/XeLaTeX - wrong images wrapping at the page breaks (wrapfigure)

My intention is to create PDF document with right positioned image of specified height (and calculated width) wrapped by text from the left side. Text could be short or long - contents is generated, so TeX document must be able to process both possibilities.

I've tried to implement it using wrapfigure:

\begin{wrapfigure}{R}{0pt}
\centering
\includegraphics[height=4cm,width=2.5cm]{img.jpg}
\end{wrapfigure}
\paragraph{}\vspace*{-\parskip}
Some text...


For full source see code below:

\documentclass[a4paper]{book}

\usepackage{fontspec}

\usepackage{polyglossia}
\setmainlanguage{czech}

\usepackage{ulem}
\usepackage{spverbatim}
\usepackage[unicode]{hyperref}
\usepackage{graphicx}
\usepackage{grffile}

\usepackage{wrapfig}
\usepackage{hologo}

\usepackage{pdfpages}

\begin{document}

\chapter{Chapter}
\section{Section}
\subsection{Subsection}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{wrapfigure}{R}{0pt}
\centering
\includegraphics[height=4cm,width=2.5cm]{img.jpg}
\end{wrapfigure}
\paragraph{}\vspace*{-\parskip}
Begin of text. This is short text. This is short text. This is short text. This is short text. This is short text. This is short text. This is short text.\newline
%%%%%%%%%%
\begin{wrapfigure}{R}{0pt}
\centering
\includegraphics[height=4cm,width=4.5cm]{img.jpg}
\end{wrapfigure}
\paragraph{}\vspace*{-\parskip}
Begin of text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text.\newline
%%%%%%%%%%
\begin{wrapfigure}{R}{0pt}
\centering
\includegraphics[height=4cm,width=2.5cm]{img.jpg}
\caption{This is caption for testing purposes.}
\end{wrapfigure}
\paragraph{}\vspace*{-\parskip}
Begin of text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text.\newline
%%%%%%%%%%
\begin{wrapfigure}{R}{0pt}
\centering
\includegraphics[height=4cm,width=4.5cm]{img.jpg}
\caption{This is caption for testing purposes.}
\end{wrapfigure}
\paragraph{}\vspace*{-\parskip}
Begin of text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text.\newline
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Section 2}
\subsection{Subsection 2}
\paragraph{}Section 2 contents...\newline

\end{document}


Result of my attempt is at first sight wrong, see attached screenshots:

There are at least three issues with my code:

1) [red remark] If the text is too short, next block is not shifted below image as needed, but it starts immediately although image from previous block is still being rendered.

2) [violet remark] Bottom image border is too high although there is no caption inside. Interesting is that other images have correct bottom border.

3) [blue remark] Second image is not aligned to second block of text, but it has dropped to next page. Additionally it's on the left side, although setting contains {R} right alignment. It could seem that reason is in previous issue (image 1 overflowed to space which should belong to image 2, so image 2 is confused and displayed at wrong place), but exactly the same bug is at second screenshot [red remark] - here is enough space both vertically and horizontally, but image is displayed at next page too (even in completely different section)

Above two screenshots have been taken for {R} settings. I've also tried {r} with other side effect:

• image overflowed the text are at the bottom of page (here it's only by few pixels, but still visible, but it can also overflow to rest of paper out of text area)

• next page is also affected - although there is no part of image displayed at next page, first two lines are shortened by image width

Basic question: Is wrapfigure useable to fulfill my requirements? Maybe I'm trying to use inconvenient functionality which could be intended for other purposes.

If not, which other method can be used for correct wrapping images by the text without any vertical/horizontal overflowing of text or image?

If wrapfigure can be used, what shall be modified in my code?

• commented on your last question but \paragraph{}\vspace*{-\parskip} is wrong, \paragraph is a level 4 section heading, it should never be empty and should only appear after a level 3 heading (subsubsection) – David Carlisle Dec 21 '18 at 22:54
• please fix your example so people can run it, it makes ! Unable to load picture or PDF file 'img.jpg' – David Carlisle Dec 21 '18 at 22:59
• I've removed the paragraph and also \newline, but without any improvement (just smal formatting difference in text, but this was not cause of non-working page breaks. Picture was just an example, it could be replaces by any JPG you find on your disk. In fact, my image is just temporary 16x16 black image for testing purposes, its scaling is done by TeX code. – Joseph Dec 22 '18 at 0:38
• You can use example-image (which is available for this kind of test, and check that the problem occurs, or use a \rule of the right size rather than \includegraphics don't expect other people will guess the required image size – David Carlisle Dec 22 '18 at 13:09
• example-image is in any reasonably current tex distribution (for several years), if you don't have it you should use your tex package manager and install the mwe package or update your tex system. – David Carlisle Dec 22 '18 at 15:42

Remove this \paragraph{}\vspace*{-\parskip}. That is wrong, \paragraph is a heading command like \section. To start a new paragraph add empty lines.

Remove also the \newline command at the end of your paragraphs.

But beside this: wrapfig has a number of problems. One is e.g. that it doesn't like it if two wrapfigs should be wrapped in one paragraph. That is a problem with the first two wrapfig.

It also has problems at page breaks if it can't freely move to the next page because of a clash there. So your example works ok, if one remove two of the wrapfigure but not if you overdo:

\documentclass[a4paper]{book}

\usepackage{fontspec}

\usepackage{polyglossia}
\setmainlanguage{czech}

\usepackage{ulem}
\usepackage{spverbatim}
\usepackage[unicode]{hyperref}
\usepackage[]{graphicx}
\usepackage{grffile}

\usepackage{wrapfig}
\usepackage{hologo}

\usepackage{pdfpages}

\begin{document}

\chapter{Chapter}
\section{Section}
\subsection{Subsection}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{wrapfigure}{R}{0pt}
\centering
\includegraphics[height=4cm,width=2.5cm]{example-image}
\end{wrapfigure}
Begin of text. This is short text. This is short text. This is short text. This is short text. This is short text. This is short text. This is short text.

%%%%%%%%%%
%\begin{wrapfigure}{R}{0pt}
%  \centering
%  \includegraphics[height=4cm,width=4.5cm]{example-image}
%\end{wrapfigure}
Begin of text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text.

%%%%%%%%%%
\begin{wrapfigure}{R}{0pt}
\centering
\includegraphics[height=4cm,width=2.5cm]{example-image}
\caption{This is caption for testing purposes.}
\end{wrapfigure}
Begin of text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text.

%%%%%%%%%%
%\begin{wrapfigure}{R}{0pt}
%  \centering
%  \includegraphics[height=4cm,width=4.5cm]{example-image}
%  \caption{This is caption for testing purposes.}
%\end{wrapfigure}

Begin of text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text. This is long text.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Section 2}
\subsection{Subsection 2}
Section 2 contents...

\end{document}

• Thank you for several advices - eg. regarding paragraphs or forgotten braces in usepackage. Nevertheless above code is not solution for described issue. Required output is that every paragraph starts with image on the right side and text on the left side. To make things easier, I've deleted my first paragraph with short text (in real document I don't expect shorter text than image height), but every other paragraphs must contain the image. – Joseph Dec 22 '18 at 1:09
• Problem in above code is in the second deleted image. This is not solution, because image must be part of every paragraph. You've just manually avoided overflowing one particular picture, but it's in contrast to my question - how to get page-breaks with wrapped images working. My real document is generated, not manually maintained, so it's not possible to shift (or even delete) the images. There are hundereds of generated images and every could be on different position at the page. – Joseph Dec 22 '18 at 1:09
• Maybe wrapfig is not convenient for my purpose? If not, what could be used instead? Expected behaviour is to break page before whole paragraph if the image doesn't fit rest of page (blank space at previous page is acceptable). – Joseph Dec 22 '18 at 1:09
• What about some conditional page break? Is somethink like that possible? I mean "If at current position is less than 5cm to the end of page, perform the page break". This could solve my issue. – Joseph Dec 22 '18 at 1:37
• You can use needspace for such a test. I can add the code later. – Ulrike Fischer Dec 22 '18 at 11:14