# How to measure the vertical space that was typeset without placing it into an environment?

I would like to achieve automatic clearing of a {floatingfigure} at the beginning of a new \section for texts automatically created by Pandoc. For this to work, I am in need of a way to measure the vertical space that was typeset by LaTeX since placing the {floatingfigure}.

I tried out the placeins package to no effect.

The ideal solution would involve placing a starting marker. An environment or any other block/braced/bracketed structure should not be used as the text that follows the {floatingfigure} is generally unknown. Everything needs to be implemented by renewing commands and environments in the preamble.

Should a second starting marker follow upon a first starting marker without taking a measurement, than the vertical measurement should be reset to 0pt.

Here is a minimum working example. I have placed comments from where to where I would like to measure.

\documentclass{article}
\usepackage{mwe}

\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}

\setlength{\parindent}{0pt}

\usepackage{calc}
\newlength\imgheight

\usepackage{floatflt}
\renewenvironment{description}{%
\renewcommand{\includegraphics}[1]{\vspace{-1ex}\Oldincludegraphics[width=4cm]{##1}}
\renewcommand{\item}[1][]{
\settototalheight\imgheight{##1}%
\global\imgheight=\imgheight%
##1%
\end{floatingfigure}%
% I would like to measure vertical space from here...
}%
\hspace{0pt}%
\begin{floatingfigure}[l]{4cm}}%
{}
% end preamble.tex

\begin{document}
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image-a}]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\end{description}

% ... to here.
\vspace{\dimexpr\imgheight-3\baselineskip}
\section{Section title}
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image-b}]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\end{description}

\blindtext
\end{document}


In below document, the first paragraph is too short to wrap completely around floatingfigure A. Hence, I would like to achieve automatic floatingfigure clearing at the beginning of a new section or a new floatingfigure. Right now, I have done it manually by adding \vspace{\dimexpr\imgheight-3\baselineskip}.

-
The length \pagetotal measures the distance from the baseline of the first line of the page. Probably you can play with it for a solution. –  karlkoeller Aug 29 '13 at 9:25

I have found a solution based on \pagetotal, thanks to the comment of @karlkoeller. In below example, automatic clearing of a wrapped floatingfigure is achieved both at the beginning of a new floatingfigure or a \section.

This is also a solution to the question: How do I clear a floatfig?

\documentclass{article}
\usepackage{mwe}

\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}

\setlength{\parindent}{0pt}

% begin preamble.tex
\usepackage{calc}
\newlength\vimgflt
\newlength\vposflt
\newlength\vtypesetflt
\newlength\vspaceflt

\usepackage{floatflt}
\renewenvironment{description}{%
\renewcommand{\includegraphics}[1]{\vspace{-1ex}\Oldincludegraphics[width=4cm]{##1}}
\renewcommand{\item}[1][]{
\settototalheight\vimgflt{##1}%
\global\vimgflt=\vimgflt%
##1%
\end{floatingfigure}%
\setlength\vposflt\pagetotal%
\global\vposflt=\vposflt%
}%
\clearflt
\hspace{0pt}%
\begin{floatingfigure}[l]{4cm}}%
{}

\newcommand{\clearflt}{
\par
\setlength\vtypesetflt{\dimexpr\pagetotal-\vposflt}
\ifdim \vtypesetflt<\vimgflt \setlength\vspaceflt{\dimexpr\vimgflt-\vtypesetflt+\baselineskip} \else \setlength\vspaceflt{0pt} \fi
\vspace{\vspaceflt}
}

\let\Oldsection\section
\renewcommand{\section}{\clearflt\Oldsection}
% end preamble.tex

\begin{document}
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image-a}]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\end{description}

\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image-b}]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\end{description}

\section{Section title}
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image-c}]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\end{description}

\blindtext
\end{document}


-
This solution still has an issue with page breaks. I am working on it. –  Serge Stroobandt Aug 29 '13 at 14:06
The solution is not compatible (yet?) with \usepackage[nobottomtitles*]{titlesec} in the preamble. –  Serge Stroobandt Aug 29 '13 at 14:32
Happy that my comment leads you to a (almost) working solution! –  karlkoeller Aug 29 '13 at 15:02
@karlkoeller Thanks. A follow-up question dealing with page breaks was posted here. –  Serge Stroobandt Aug 29 '13 at 21:11
@karlkoeller ...and here is a follow-up question about titlesec –  Serge Stroobandt Aug 29 '13 at 21:15