## LaTeX forum ⇒ General ⇒ Loops using page no as reference

LaTeX specific issues not fitting into one of the other forums of this category.
Jchen066
Posts: 13
Joined: Tue Aug 28, 2018 12:50 am

### Loops using page no as reference

Hi,

I am trying to write a code to repeat a command until the end of page. But it seems below code over does it 2 more times into the next page. (I have written below code that just prints the page number instead, to see what is wrong with it.)

Did I write something wrong in my code, or how should I adjust my code to accommodate for the bug?

Regards,
Jie

\documentclass{article} \newcounter{mypageno}\newcounter{mypageno2} \begin{document} 	asdfasf 	\setcounter{mypageno}{\thepage} 	\loop 		\setcounter{mypageno2}{\thepage} 	\ifnum \value{mypageno} = \value{mypageno2} 		This is the pagenumber \arabic{mypageno2} 	\repeat \end{document}

Stefan Kottwitz
Posts: 9322
Joined: Mon Mar 10, 2008 9:44 pm
Hi Jie!

This code is by written by Petr Olsak in TeX, adapted to LaTeX.

It uses a temporary box0 to to repeat the text. Finally this box0 is split by \vsplit to the current page.

\documentclass{article}\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}\begin{document}\section{Demo} Some text \toendpage{\noindent This is repeated text.\\}\end{document}

Stefan