I'm trying to display image wrapped by text, but I'm not able to align them exactly. Image is always rendered one line below the begin of text block. Additionally, it's displayed line below even when I want to align it with the \subsection.
I've tried to shift the image by \vspace{-33pt}, but it works only when \wrapfigure has specified width. This setting is completely ignored in my case when I need to detect \wrapfigure width using 0pt width settings. (Please don't be confused by specified image (=rule) width in my example - real document contains hundreds of images of variable width, so width can't be specified as a parameter and \wrapfigure width must be really detected.)
How shall be correctly aligned top of the image to
1) \subsection header
2) first line of text
with additional requirement for \wrapfigure width detection?
Full non-working code and screenshots describing observed and expected behaviour are below.
\documentclass[a4paper]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{czech}
\usepackage{ulem}
\usepackage{spverbatim}
\usepackage[unicode]{hyperref}
\usepackage[]{graphicx}
\usepackage{grffile}
\usepackage{color}
\usepackage{wrapfig}
\usepackage{hologo}
\usepackage{pdfpages}
\usepackage{needspace}
\begin{document}
\chapter{Chapter 1}
\section{Section 1}
\needspace{6cm}
\begin{wrapfigure}{R}{0pt}
%\vspace{-33pt} % Doesn't work for width detection (0pt above)
\centering
\rule{3cm}{4cm}
%\includegraphics[height=4cm,width=3cm]{img.jpg}
\end{wrapfigure}
\subsection{Subsection 1} % <----- Image shall be here, but is one line below Some text
Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below.
\chapter{Chapter 2}
\section{Section 2}
\subsection{Subsection 2}
\needspace{6cm}
\begin{wrapfigure}{R}{0pt}
\centering
\rule{3cm}{4cm}
%\includegraphics[height=4cm,width=3cm]{img.jpg}
\end{wrapfigure}
Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below.
\end{document}
latex
distribution...wrapfigure
work correctly (only) at standard paragraph.