7

Is there a mechanism similar to testpage to check how many lines of text remain on a page but to do something other than issue a page break?

I would like to determine if there are more than say, 10 lines, remaining on the page, and if there are, fill that space with some sort of decoration. And if the space isn't there, just issue a plain page break.

I've taken a look at the source code for page breaks, but the plain tex is a bit too much for me to understand.

A minimal example would look something like:

\setuppapersize[A5]

\starttext

\input knuth

\testpage[10]

% Since testpage didn't fire, insert a small graphic here
% (this image is being added manually because I know there is space left,
% but I would like an automatic way to determine if there is space)
\vfill
\midaligned{\externalfigure[dummy][height=0.3\textheight]}
\vfill
\page[yes]

\input knuth
\input knuth

\testpage[10]

% Since testpage did fire, leave as is

\input knuth

\stoptext
  • I'm not sure I get what you want. In your example, in the first page the image is put on the page since there is space left. But in the second page a page break is issued before the last instance of knuth.tex. That sounds like just what you want. – mickep Oct 13 '19 at 8:56
  • @mickep Yes, but what's missing is that I had to manually determine whether there was sufficient space for the image or not. Ideally, the image placement should be added as an "else" condition for testpage. That said, I'm not actually tied to testpage and would be happy with any method to simply count how many lines of blank text there are left on the page. – ssokolen Oct 13 '19 at 15:44
9
+100

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

Placement of image when enough space is available on the page

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

Height of the remaining space on the page

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

Number of lines calculated with \getnoflines

Result with \getroundednoflines

\showgrid

\setuppapersize[A7]

\starttext

\input jojomayer

\getroundednoflines{\dimexpr\pagegoal-\pagetotal\relax}
\number\noflines

\stoptext

Number of lines calculated with \getroundednoflines

  • Thanks, that's perfect! It seems like you might be missing a "no" in your first line of "There is existing mechanism", but I'm not sure what the policy is around editing answers. – ssokolen Oct 13 '19 at 17:19
4

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
  • You should add \par at the begin of the \fillpage command to ensure the previous paragraph has ended, otherwise you get wrong result for the value of the remaining space. You should also use camel case for the names of your own commands to avoid clashes with existing macros. – Wolfgang Schuster Oct 13 '19 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.