GeneralLoops using page no as reference

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

Loops using page no as reference

Post by Jchen066 »

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?

Thanks in advance.

Regards,
Jie

Code: Select all

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

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Loops using page no as reference

Post by Stefan Kottwitz »

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.

Code: Select all

\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
LaTeX.org admin
Post Reply