# How to circumvent a late \pagetotal reset to 0pt?

Below example demonstrates at the beginning of each paragraph how \pagetotal is not always reset to 0pt at the beginning of the page. This appears to interfere with my attempt to automatically clear a floatingfigure. What could be a remedy for this?

\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}%
\the\pagetotal%
\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}
\blindtext[3]
\section{Section title}
\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}

\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}

\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[2]
\end{document}


-
TeX's output routine (page breaker) works asynchronously with the construction of the main vertical list, information about the page so far is never reliably available to macros being executed during the construction of the page, you might be processing paragraphs that end up being added several pages ahead (due to floats etc). What you can presumably reliably detect is whether the package has finished its hanging indent or whether it has a pending indent to set for the next paragraph as it has not indented enough lines. If you get to a section title in that situation you want to clear. – David Carlisle Aug 29 '13 at 15:44
What is that you want to do? – Werner Aug 29 '13 at 16:05
@Werner At a high level, I just would like to clear floats; i.e. end the text wrap around an image. At a low level, I try to do so by adding vertical space to the wrapped paragraph to the amount of the image height minus the height of the wrapped paragraph. It is the measurement of the wrapped paragraph that runs awry, due to the observed \pagetotal imperfection at page breaks. – Serge Stroobandt Aug 29 '13 at 21:23
@Werner The beginning of each paragraph should show the amount of typeset text expressed as a vertical measurement in points. On the first page this works. On the second page, the measurement is not immediately reset to 0pt. – Serge Stroobandt Sep 14 '13 at 20:52

The relevant section from the TeXbook on this topic is the following

\ddanger You can look at the $t$ and $g$ values that are used in page breaking by referring to the \ values ^|\pagetotal|' and ^|\pagegoal|', respectively. You can even change them (but let's hope that you know what you are doing). For example, the command |\pagegoal=500pt| overrides the previously saved value of/ |\vsize|. Besides |\pagetotal|, which represents the accumulated natural height, \TeX\ maintains the quantities ^|\pagestretch|, ^|\pagefilstretch|, ^|\pagefillstretch|, ^|\pagefilllstretch|, ^|\pageshrink|, and ^|\pagedepth|. When the current page contains no boxes, |\pagetotal| and its relatives are zero and |\pagegoal| is $16383.99998\pt$ (\TeX's largest \); changing their values has no effect at such times. The integer $q$ in the formula for page costs is also available for inspection and change; it is called ^|\insertpenalties|.

Whenever TeX comes to a possible breakpoint in the current galley it updates the value for "pagetotal" and other parameters and then carries on, unless this breakpoint is making the current page too full. Only then, TeX decides which of the breakpoints on the page is the brest one to actually break the page (it is definitely not the one that triggered the process but it may not be the one before either). So al you see when looking at \pagetotalis how far TeX got before making the page breaking decision.

In your case it actually got to the point where the page got overfull (and that is what you got recorded) and the final break happened earlier. If you use \tracingpages=1 you can see this behavior in the log file, here are the relevant lines from your MWE with the tracing enabled:

% t=346.0 g=550.0 b=10000 p=0 c=100000#
% t=358.0 g=550.0 b=10000 p=150 c=100000#
% t=371.94444 g=550.0 b=10000 p=-300 c=100000#
% t=424.97215 plus 7.16663 minus 0.86108 g=550.0 b=10000 p=0 c=100000#
% t=436.97215 plus 8.16663 minus 0.86108 g=550.0 b=10000 p=150 c=100000#
% t=448.97215 plus 8.16663 minus 0.86108 g=550.0 b=10000 p=250 c=100000#
% t=524.2731 plus 8.16663 minus 0.86108 g=550.0 b=3118 p=-300 c=2818#
<example-image-b.pdf, id=2, 321.2pt x 240.9pt> <use example-image-b.pdf>
<use example-image-b.pdf>
% t=577.30081 plus 15.33325 minus 1.72217 g=550.0 b=* p=0 c=*
[1{c:/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}


As you can see the last t= on the first page is exactly the one next to your figure B and that one akes the page overfull and so TeX backs up and uses the line

% t=524.2731 plus 8.16663 minus 0.86108 g=550.0 b=3118 p=-300 c=2818#


as the best breakpoint for the first page.

In summary, \pagetotal helps you understanding where TeX is in its process of breaking the page, but it may show a place beyond the breakpoint that will finally be chosen due to the total costs associated with it. So it is no good for what you try to achieve.

-
I agree, \pagetotal was not used here in a helpful way. Contrast this with this answer where \pagetotal, by being used in a much different manner, eventually does turn out to be part of the solution to the very same problem in ConTeXt. – Serge Stroobandt Oct 2 '13 at 17:19