ConTeXt: Calculating remaining space on page like testpage
The pagechecker
mechanism
The \testpage
command uses a mechanism called pagechecker
to compute the remaining space on the page.
You can use the mechanism to place content on the page with the inbetween
key. With the lines
key you specifiy how many lines should be remain on the page to place the content.
The result of the calculated number of lines depend on the method key which accepts a number between 0
and 3
.
\startsetups [fillpage]
\vfill
\midaligned{\externalfigure[dummy][lines=4]}
\vfill
\stopsetups
\definepagechecker [fillpage] [method=2,lines=8,inbetween=\directsetup{fillpage}]
\showgrid
\setuppapersize[A7]
\starttext
\input jojomayer
\checkpage [fillpage] [lines=9]
\page
\input jojomayer
\checkpage [fillpage]
\stoptext
Manual calculation of the remanining space
When you want more control about the measurement of the remaining space you can use the two registers \pagegoal
and \pagetotal
.
\pagegoal
Height of the text block which forces a page break when reached.
\pagetotal
Height of the current material on the page.
To get the height of the remaining space you substract \pagetotal
from \pagegoal
.
\showgrid
\setuppapersize[A7]
\starttext
\input jojomayer
\the\dimexpr\pagegoal-\pagetotal\relax
\stoptext
To get he number of lines for the calculated value you can use the \getnoflines
or \getroundednoflines
commands. The resulting number is stored in the \noflines
register.
Result with \getnoflines
\showgrid
\setuppapersize[A7]
\starttext
\input jojomayer
\getnoflines{\dimexpr\pagegoal-\pagetotal\relax}
\number\noflines
\stoptext
Result with \getroundednoflines
\showgrid
\setuppapersize[A7]
\starttext
\input jojomayer
\getroundednoflines{\dimexpr\pagegoal-\pagetotal\relax}
\number\noflines
\stoptext
Although Wolfgang answered the crux of the question by explaining how page space is counted, it took me a couple more Google searches to figure out how to actually use the noflines
register in an if statement. The following code seems to produce what I was looking for:
\setuppapersize[A5]
\define[1]\FillPage{
\par
\getnoflines{\the\dimexpr\pagegoal-\pagetotal\relax}
\ifnum\number\noflines>#1\relax
\vfill
\midaligned{\externalfigure[dummy][height=0.3\textheight]}
\vfill
\fi
\page[yes]
}
\starttext
\input knuth
\FillPage{10}
\input knuth
\input knuth
\FillPage{10}
\input knuth
\stoptext