# Wrapfigure, \baselineskip if not top of page

## TL;DR

The wrapfigure environment aligns wrapped content differently, depending on if the page is still empty or not. If the page is not empty, it will lower the wrapped content by \intextsep.

I am using wrapfigure to embed little images into paragraphs. It works quite well, but I'm having this problem. The new problem for this question is, that the problem does not occur, when the paragraph starts on the first line of a page, so I need to detect the "first line on page" case.

In the referenced problem, the wrapfigured paragraph can never on the first line of a page, hence adding \vspace*{-1\baselineskip}% at the beginning of the wrapfigure solves the issue.

In my approach, it is entirely possible, sometimes even wanted, that the wrapfigured paragraph starts at the first line of a page. I use wrapfigure as follows:

\renewcommand{\subsubsubsection}[2][\relax]{%
\ifx#1\relax
\relax
\else
\settowidth\wrapwidth{\includegraphics{#1}}
\begin{wrapfigure}{r}{\wrapwidth}\vspace*{-1\baselineskip}%
\includegraphics{#1}
\end{wrapfigure}
\fi%
\@startsection{paragraph}{4}{\z@}%
{3.25ex \@plus1ex \@minus.2ex}%
{-1em}%
{\raggedsection\normalfont\sc\nobreak\bfseries\small}%
{#2}
}

\subsubsubsection{Just a title}
blah blah blah

\subsubsubsection[imagefile]{Title with image}
blah blah blah


How do I detect if my \subsubsubsection will be the first paragraph and line on a page, so that I can put \vspace*{-1\baselineskip}% only when needed?

Edit 1

I tried appending % to every line in the macro definition, but that didn't yield any visible changes.

Here is a complete document illustrating the problem:

\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}

\makeatletter
\newcommand{\subsubsubsection}[2][\relax]{%
\begin{wrapfigure}{r}{1cm}\rule{1cm}{2cm}\end{wrapfigure}%
\@startsection{paragraph}{4}{\z@}{1em}{-1em}{\bfseries}{#2}%
}
\makeatother

\begin{document}

\subsubsubsection[foo]{This is how I want the black box align to the text}\lipsum[1]

\subsubsubsection[foo]{This is NOT how I want the black box to align with the text}\lipsum[1]

\newpage
\subsubsubsection[foo]{What I want}\lipsum

\subsubsubsection[foo]{Not what I want}
Now, if I just produce enough text to make the beginning of the next subsubsubsection
become the first line on the new page.It will work. Flip over, to see for yourself! I'll just add some text here to avoid the problems that arise when the wrapped paragraph is fewer lines long than the wrapfigure is high. I'll just add some text here to avoid the problems that arise when the wrapped paragraph is fewer lines long than the wrapfigure is high. I'll just add some text here to avoid the problems that arise when the wrapped paragraph is fewer lines long than the wrapfigure is high.

\rule{2pt}{.34\textheight}

\subsubsubsection[foo]{Happens to be what I want}\lipsum[2]

\end{document}


I threw out Koma-Script, the \includegraphics, as well as width-calculation and and the \ifx. It will be harder to see, but the problem also persists when replacing the line starting with \@startsection with simply #2.

Here is a picture showing what I want and what I do not want:

I spent the better part of the time between posting the question and now by reading up on TeX, boxes and modes. This newly gained knowledge makes it easy to spot, that the problem I described above is nothing but the normal and intended behaviour of wrapfigure.

The key lines are:

\ProvidesPackage{wrapfig}[2003/01/31 \space  v 3.6]


and therein:

160    \ifdim\pagetotal=\z@ % \WF@info{Put \WF@wfname at top of p.\thepage}%
162    \else % \WF@info{Putting \WF@wfname in middle of page}%
163     \setbox\WF@box\hbox{\lower\intextsep\box\WF@box}%
164    \fi \dp\WF@box\z@ \box\WF@box \@tempa


Hence the answer is simple: the wrapfigure environment must always know \pagetotal=\z@. This can be achieved through adding the line \let\pagetotal\z@ to the macro definition. This is how it would be done for the example document above:

\newcommand{\subsubsubsection}[2][\relax]{%
\let\pagetotal\z@
\begin{wrapfigure}{r}{1cm}\rule{1cm}{2cm}\end{wrapfigure}%
\@startsection{paragraph}{4}{\z@}{1em}{-1em}{\bfseries}{#2}%
}