22

I need a document with 500 empty pages, only the pagenumbering in the foot of the page.

This MWE shows 2 pages in the way I'd like to get all 500:

\documentclass[pagenumber=footright, DIV=20, fontsize=16]{scrartcl}
\usepackage[automark]{scrlayer-scrpage}
\pagestyle{scrheadings}
\ihead{}
\ofoot[\pagemark]{\pagemark}
\chead{}
\cfoot[]{}
\ohead{}



\begin{document}
\mbox{}


\newpage

\mbox{}
\newpage



\end{document}

Can somebody write e.g. a loop for 500 pages? Cave: the \mbox{} is necessary, otherwise there is no pagenumber or even a PDF of 0 pages...

(The purpose is to add pagenumbers to an already printed document: I'll put the 500 pages again into the printer and thus get a printed document with pagenumbers. I'm trying to help a poor guy here who otherwise had to write the pagenumbers manually into 40 folders à 500 pages.)

2
  • 2
    \count0=500 \loop\ifnum\count0>0 \null\clearpage\advance\count0 by -1 \repeat (done from memory, not sure if it works)
    – Manuel
    Jul 5 '16 at 11:31
  • 4
    This question would have been even more awesome if you had simply refused to explain yourself. "Trust me. Never mind why I need it.".
    – pipe
    Jul 6 '16 at 2:08
20

Use a primitive \loop with \repeat and \unless testing.

\documentclass[pagenumber=footright, DIV=20, fontsize=16]{scrartcl}
\usepackage[automark]{scrlayer-scrpage}
\pagestyle{scrheadings}
\ihead{}
\ofoot[\pagemark]{\pagemark}
\chead{}
\cfoot[]{}
\ohead{}


\newcounter{loopcntr}

\begin{document}
\loop\unless\ifnum\value{loopcntr}=500
\mbox{}
\newpage
\stepcounter{loopcntr}% Advance the counter
\repeat

\end{document}

Here's a variant with pgffor and forloop packages:

\documentclass[pagenumber=footright, DIV=20, fontsize=16]{scrartcl}
\usepackage[automark]{scrlayer-scrpage}
\pagestyle{scrheadings}
\ihead{}
\ofoot[\pagemark]{\pagemark}
\chead{}
\cfoot[]{}
\ohead{}

\usepackage{pgffor}
\usepackage{forloop}

\newcounter{loopcntr}

\begin{document}

\foreach \x in {1,...,500} {%
\mbox{}
\newpage
}

\forloop{loopcntr}{1}{\value{loopcntr} < 501}{%
\mbox{}
\newpage
}


\end{document}
20
\documentclass[pagenumber=footright, DIV=20, fontsize=16]{scrartcl}
\usepackage[automark]{scrlayer-scrpage}
\pagestyle{scrheadings}
\ihead{}
\ofoot[\pagemark]{\pagemark}
\chead{}
\cfoot[]{}
\ohead{}



\begin{document}


\def\x{\ifnum\value{page}<501\mbox{}\clearpage\expandafter\x\fi}
\x

\end{document}
1
  • A nice trick, I have to remember this ;-)
    – user31729
    Jul 5 '16 at 11:49
13

A variant with package multido (50 bytes):

\documentclass[DIV=20, fontsize=16]{scrartcl}
\usepackage{lmodern}
\usepackage[automark]{scrlayer-scrpage}
\pagestyle{scrheadings}
\cfoot{}
\ofoot[\pagemark]{\pagemark}

\usepackage{multido}

\begin{document}

\multido{}{500}{\null\newpage}

\end{document}
12

With expl3:

\documentclass[pagenumber=footright, DIV=20, fontsize=16]{scrartcl}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{expl3}

\pagestyle{scrheadings}
\ihead{}
\ofoot[\pagemark]{\pagemark}
\chead{}
\cfoot[]{}
\ohead{}

\begin{document}

\ExplSyntaxOn
\prg_replicate:nn { 500 } { \mbox{}\newpage }
\ExplSyntaxOff

\end{document}

With no package and no assignment, but with e-TeX:

\documentclass[pagenumber=footright, DIV=20, fontsize=16]{scrartcl}
\usepackage[automark]{scrlayer-scrpage}

\pagestyle{scrheadings}
\ihead{}
\ofoot[\pagemark]{\pagemark}
\chead{}
\cfoot[]{}
\ohead{}

\begin{document}

\def\x#1{%
  \ifnum#1>0
  \null\clearpage 
  \expandafter\x\expandafter{\the\numexpr#1-1\expandafter}\fi
}
\x{1}
\end{document}

With a simple variant, you can get a sequence that expands to 500 copies of \null\clearpage:

\def\x#1{%
  \ifnum#1>0
  \unexpanded{\null\clearpage}%
  \expandafter\x\expandafter{\the\numexpr#1-1\expandafter}\fi
}
\edef\fivehundredemptypages{\x{500}}
4

The following is how I'd do it. It's very similar to David Carlisle's answer, but in a way which makes the recursion slightly clearer to me (trying to remember how \expandafter works always gives me a headache!).

\begin{document}

\def\x{\ifnum \count0>500
    \let\next\relax
  \else
    \null\clearpage
    \let\next\x
  \fi
  \next}
\x

\end{document}

Also Knuth explicitly guarantees that this is tail-recursive (though I don't doubt that the \expandafter one is, too).

5
  • Much less efficient than the \expandafter version. ;-)
    – egreg
    Jul 5 '16 at 22:41
  • @egreg Is it? Oooh: why? Do you mean one whole \let each time round the loop (in which case ouch, but I may be able to take the hit (premature optimisation and the root of all evil and all that)); or is there something blowing up in there that I can't spot? Jul 5 '16 at 23:34
  • I don't think that efficiency is an issue here, unless a document with $10^{12}$ pages should be generated ;-) (+1)
    – user31729
    Jul 6 '16 at 3:02
  • @NormanGray I was indeed referring to a \let for any cycle.
    – egreg
    Jul 6 '16 at 5:36
  • +1 for the strict relation between expandafter and headache.
    – Rmano
    Jul 7 '16 at 6:50

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.