Loop until the end of a page

The page builder is asynchronous and TeX doesn't have any clue where a page ends when typesetting a paragraph. Only when a paragraph has ended page breaking decisions are made.

My strategy is to build the gigantic paragraph in a box that's then compared to the desired text height. If it's still below it, a new box is prepared with more repetitions. When we have sufficient material (the page height plus three lines just to be sure), the box is split at the desired height and put on the current page.

\documentclass[12pt]{article}

\usepackage[
  margin=0.5in,
  heightrounded, % whole number of lines on the page
  showframe,     % just for the example
]{geometry}
\usepackage{expl3}

\ExplSyntaxOn
% faster than \pgffor
\cs_set_eq:NN \repeatphrase \prg_replicate:nn
\ExplSyntaxOff

\pagestyle{empty}
\setlength{\parindent}{0cm}

\begin{document}

\def\Phrase{Phrase to repeat }
\count255=200 % or anything that seems fit
\loop
  \typeout{Trying \the\count255\space\space repetitions}
  \setbox0=\vbox{\repeatphrase{\count255}{\Phrase}}
  \ifdim\ht0<\dimexpr\textheight+3\baselineskip\relax
  \advance\count255 by 20
\repeat

\setbox0=\vbox to \textheight{
  \vbadness=10000 % remove a spurious message
  \vsplit0 to \textheight
}
\unvbox0

\end{document}

The messages on the terminal and log file are

Trying 200 repetitions
Trying 220 repetitions
Trying 240 repetitions
Trying 260 repetitions
Trying 280 repetitions
Trying 300 repetitions
Trying 320 repetitions

The starting point is at 200, a rough estimate of what's needed (one could start from 1, but it doesn't seem meaningful).

enter image description here

Note that LaTeX has no public interface to \vsplit, so we have to resort to lower level programming.

Here is a full expl3 version that's essentially the same. I just added a small flexibility to the interline skip for better filling the page.

\documentclass[12pt]{article}

\usepackage[
  margin=0.5in,
  heightrounded, % whole number of lines on the page
  showframe,     % just for the example
]{geometry}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\fillpage}{O{200}m}
 {
  \clearpage
  \egreg_fillpage:nn { #1 } { #2 }
  \clearpage
 }

\int_new:N \l__egreg_fillpage_copies_int
\box_new:N \l__egreg_fillpage_box

\cs_new_protected:Npn \egreg_fillpage:nn #1 #2
 {
  \int_set:Nn \l__egreg_fillpage_copies_int { #1 }
  \bool_do_until:nn
   {
    \dim_compare_p:nNn { \box_ht:N \l__egreg_fillpage_box } > { \textheight + 3\baselineskip }
   }
   {
    \typeout{Trying~\int_eval:n { \l__egreg_fillpage_copies_int }~repetitions}
    \vbox_set:Nn \l__egreg_fillpage_box
     {
      \skip_set:Nn \baselineskip { 1\baselineskip plus 0.1pt }
      \prg_replicate:nn { \l__egreg_fillpage_copies_int } { #2 }
     }
    \int_add:Nn \l__egreg_fillpage_copies_int { 20 }
   }
  \vbox_set_split_to_ht:NNn \l__egreg_fillpage_box \l__egreg_fillpage_box { \textheight }
  \box_use_drop:N \l__egreg_fillpage_box
 }
\ExplSyntaxOff

\pagestyle{empty}
\setlength{\parindent}{0cm}

\begin{document}

\fillpage{Phrase to repeat }

\end{document}

The command \fillpage has an optional argument for setting the initial number of repetitions (default 200), so

\fillpage[300]{Phrase to repeat }

would start from 300.


My solution does only one printing of repeated text in temporary box0. Then this box0 is \vsplit to the current page.

The macro \toendpage{repeated text} is provided. This macro fills the current page from current point (it is irrelevant if this is at the start of the page or in the middle of the page) to the end of the page.

The macro \toendpage needs to do a calculation how much repetitions of the text we need. This number must be sufficiently great, so it is calculated as

(1.3 * \hsize / width_of_the_text + 1) * lines_in_the_\pagegoal

The macro follows:

\newcount\tmpnum
\def\toendpage#1{\par \bgroup
   \setbox0=\hbox{#1}
   \null \nobreak \vskip-\baselineskip % we need to set \pagegoal and set zero \prevdepth
   \dimen0=\pagegoal
   \divide\dimen0 by\baselineskip
   \tmpnum=\dimen0                % \tmpnum= number of lines in \pagegoal
   \dimen0=1.3\hsize   
   \divide\dimen0 by\wd0          % \dimen0= number of text in 1.3\hsize     
   \advance\dimen0 by1sp          % increased by one
   \multiply\dimen0 by\tmpnum     % \dimen0= number of repeats of the text
   \tmpnum=0
   \setbox0=\vbox{\null \loop #1\advance\tmpnum by1 \ifnum\tmpnum<\dimen0 \repeat}
   \vbadness=10000
   \dimen0=\pagegoal \advance\dimen0 by-\pagetotal
   \setbox0=\vsplit0 to\dimen0    % \vsplit to the end of the current page
   \unvbox0
   \vfil\break \egroup
}

Usage:        
\toendpage{This is repeated text. }

Tags:

Loops