Controlling the page count in LaTeX
by gummi
I’ve been working on a project which involves generation of simple books for print in LaTeX. The print shop set the requirement that the page count of the PDFs we give them always are multiple of four.
As this is something LaTeX doesn’t provide out of the box, my first idea was to do some post processing with the PostScript tools that you have on a Unix system. But introducing an extra tool wasn’t feasible at all: Running LaTeX from a webapp is fragile enough. Fortunately, LaTeX is just a module for TeX, and TeX is nothing but a programming language. So there most definitely is a way for doing this in plain TeX. Unfortunately, programming in TeX is everything but straight forward. Luckily I can’t resist language challenges…
And I did it! Here is my LaTeX macro that makes the number of pages in the document a multiple of some number:
The loop construct is the tricky part. It’s of the form
\loop ... \ifsomething... ... \repeat
The if-statement in the middle controls whether we continue the loop or break out. Today, most people are probably more used to seeing the above logic written like this:
while (something) { .... }
A thing to note is that TeX-loops allow for statements to be run both before and after the if-statement is evaluated. Code before the statement is run in each iteration, similar to traditional do {} while (...) loops, but the code after is only run if the if-statement returns true:
\loop % Code to run in each iteration (do-while style) \ifsomething % % Code to run if the if-statement above returned true (while-style) \repeat
Finally, note that the if-statement must not have a closing \fi, as most if-statements in TeX have.
Tip: Finding material on programming in TeX is hard, but the TeXbook provides some help. Deep down in chapter 20 there are some examples of looping and conditionals, which proved to be enough for me.
Update: kongo09 came up with a more robust solution to this, which also works when resetting the page counter. See below. Thanks kongo09!
Comments
Thanks for the great suggestion!
Does that mean I put the \def part in my TeX file
before the \begin{document} and the \makepagesmultipleof4
just before the closing \end{document}?
Yes, at least that is how I use it myself. Technically you can put the \def anywhere you’d like, but it’s customary to put it in the preamble, that is, before the document starts.
Thanks. This works – kind of.
Apparently, \value{page} uses the current page number for the calculation. That is fine unless you have some preface with its own page numbering, like a table of contents. In that case, you end up with a main content part being treated correctly, but not necessarily with the full document having the right number of pages.
You’re right. Actually leaving out the front-matter was exactly what I needed, but in general you of course want to count that in. I don’t know of any other counter to use for this — let me know if you find a way to count everything in.
I struggled a lot with this now. Tried all sorts of other counters, etc… but I can’t get it working. I’m suprprised that there is no package solving this everyday printing problem.
Should I come across something, I’ll post it here.
Problem solved:
\usepackage{atbegshi}
\newcounter{abspage}
\AtBeginShipout{\stepcounter{abspage}}
\makeatletter
\AtBeginDocument{%
\AtEndDocument{%
\if@twoside %
\cleardoublepage
\pagestyle{empty}
\begingroup
\@tempcnta=\value{abspage}
\divide\@tempcnta by 4
\multiply\@tempcnta by 4
\ifnum \@tempcnta=\value{abspage} \else \null\cleardoublepage\fi
\endgroup
\fi
}%
}
\makeatother
Great — thanks a lot! Looks much more hard-core than my solution
[...] These two commands are necessary, as @ isn't treated as a normal … Name (required) …Controlling the page count in LaTeX g:hControlling the page count in LaTeX. I've been working on a project which involves generation of [...]