2

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}

Screenshot 1 Screenshot 2

  • 2
    off-topic: your problem is not related to miktex , the very same problem you will meet at any latex distribution... wrapfigure work correctly (only) at standard paragraph. – Zarko Dec 22 '18 at 22:46
3

You can set \intextsep to 0 to align with the first line of the paragraph. To move it more up in e.g. a heading use \raisebox and hide the real height with the option argument. Be aware that a long heading will be overwritten by such an image.

\documentclass[a4paper]{book}
\usepackage{wrapfig}
\usepackage{needspace}
\intextsep=0pt
\begin{document}

\chapter{Chapter 1}
\section{Section 1}
\needspace{6cm}
\begin{wrapfigure}{R}{0pt}
   \centering
   \raisebox{\baselineskip}[\dimexpr\height-\baselineskip][0pt]{\rule{3cm}{4cm}}
\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}
 \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}
3

You can play with \raisebox, fooling LaTeX with the real height of the image via the 1st optional argument of the command:

\documentclass[a4paper]{book}

\usepackage{fontspec}

\usepackage{polyglossia}
\setmainlanguage{czech}

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

\usepackage{wrapfig}
\usepackage{hologo}

\usepackage{pdfpages}

\usepackage{needspace}
\usepackage{lipsum}

\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}[8]{R}{0pt}
   \centering
  \raisebox{4ex}[0.8\height]{ \includegraphics[height=4cm,width=3cm]{img.jpg}}
\end{wrapfigure}
\leavevmode \lipsum[1]

\end{document} 

enter image description here

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.