[Tex/LaTex] Wrapfigure beside Heading

sectioningwrapfigure

I'm trying to place a figure in the top right corner of the page and have the text wrap around it using the wrapfigure environment (image below). The first few lines of text are a header and subheader, and I expect this layout to reoccur frequently in this document (and I have encountered this problem before, just shooting to spare some odd white-space).

I've scoured the internet and tried various work-arounds myself, but the best I can do is to get the image placed where I want it by cheating with negative \vspace{}'s and invisible \section*{}'s, in which case, the figure wrap continues indefinitely on every page to the end of my document.

Any help is greatly appreciated!!

enter image description here

\documentclass[a5paper]{article}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{lipsum}    

\begin{document}

\begin{wrapfigure}[15]{r}{0pt}
    \begin{tabular}{c}
    \includegraphics[scale=0.4]{image.jpg}
    \end{tabular}
    \caption{Caption.}
    \label{label}
\end{wrapfigure}

\section{Heading}

\subsection{Subheading 1}
\lipsum[10]

\subsubsection{Subheading 2}
\lipsum[4]

\end{document}

Best Answer

A manual way is lying to TeX that the height of the image is smaller than in reality. This moves the image to the top. The amount can be get by trial and error.

\documentclass[a5paper]{article}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{lipsum}

\begin{document}

\section{Heading}
\subsection{Subheading 1}
\begin{wrapfigure}[7]{r}{0pt}
    \centering
    \raisebox{0pt}[\dimexpr\height-5\baselineskip\relax]{%
        \includegraphics[scale=0.4]{example-image-a.jpg}%
    }%
    \caption{Caption.}
    \label{label}
\end{wrapfigure}
\lipsum[10]

\subsubsection{Subheading 2}
\lipsum[4]

\end{document}

Result

Because of the lying, TeX does not know that there is something to the right of the section titles. Therefore, the author has to be take care of that the width of the section titles are not too large and then overprinted by the image.

Related Question