LaTeX forum ⇒ GeneralLoops 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

Postby Jchen066 » Mon Oct 22, 2018 1:23 pm

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

  1. \documentclass{article}
  2.  
  3. \newcounter{mypageno}
  4. \newcounter{mypageno2}
  5.  
  6. \begin{document}
  7.  
  8. asdfasf
  9.  
  10. \setcounter{mypageno}{\thepage}
  11.  
  12. \loop
  13.  
  14. \setcounter{mypageno2}{\thepage}
  15.  
  16. \ifnum \value{mypageno} = \value{mypageno2}
  17.  
  18. This is the pagenumber \arabic{mypageno2}
  19.  
  20. \repeat
  21.  
  22. \end{document}

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

Postby Stefan Kottwitz » Mon Oct 22, 2018 7:23 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.

  1. \documentclass{article}
  2. \newcount\tmpnum
  3. \def\toendpage#1{\par \bgroup
  4. \setbox0=\hbox{#1}
  5. \null \nobreak \vskip-\baselineskip % we need to set \pagegoal and set zero \prevdepth
  6. \dimen0=\pagegoal
  7. \divide\dimen0 by\baselineskip
  8. \tmpnum=\dimen0 % \tmpnum= number of lines in \pagegoal
  9. \dimen0=1.3\hsize
  10. \divide\dimen0 by\wd0 % \dimen0= number of text in 1.3\hsize
  11. \advance\dimen0 by1sp % increased by one
  12. \multiply\dimen0 by\tmpnum % \dimen0= number of repeats of the text
  13. \tmpnum=0
  14. \setbox0=\vbox{\null \loop #1\advance\tmpnum by1 \ifnum\tmpnum<\dimen0 \repeat}
  15. \vbadness=10000
  16. \dimen0=\pagegoal \advance\dimen0 by-\pagetotal
  17. \setbox0=\vsplit0 to\dimen0 % \vsplit to the end of the current page
  18. \unvbox0
  19. \vfil\break \egroup
  20. }
  21. \begin{document}
  22. \section{Demo}
  23.  
  24. Some text
  25.  
  26. \toendpage{\noindent This is repeated text.\\}
  27. \end{document}


Stefan
Site admin


Return to “General”

Who is online

Users browsing this forum: No registered users and 7 guests