Skip to content

A day with LaTeX: add extra empty pages at end of the document

July 27, 2012

There are cases when we want to place empty pages at end of our documents. If we are adding constant number of pages, it’s easy. But what it the page count should be dynamic, to for example meet publisher requirements like “page count divisible by 4”? TeX keeps many of its variables in counters. Page count it kept in counter zero, i.e. one named \count0. But, there is a problem. Page counts can change, if we separate front matter from main matter. Let’s walk around this first. We need to know how many pages were typed out by then. If \mainmatter was not used, we will leave page count before main matter at zero. Let’s add this code to preamble

\makeatletter
\newcount\pages@before@mainmatter\pages@before@mainmatter=0
\let\old@mainmatter\mainmatter
\def\mainmatter{\old@mainmatter\pages@before@mainmatter=\count0}
\makeatother

Lets calculate how many pages we have to add to our document. We need to trigger it at end of the document.

\makeatletter
\AtEndDocument{%
\newcount\pages@in@document\pages@in@document=\count0%
\advance\pages@in@document by \pages@before@mainmatter%
\ifnum\pages@before@mainmatter>0\advance\pages@in@document by -1\fi%
\newcount\pages@to@insert\pages@to@insert=\pages@in@document%
\advance\pages@to@insert by 3%
\divide\pages@to@insert by 4%
\multiply\pages@to@insert by 4%
\advance\pages@to@insert by -\pages@in@document%
% ... (to be continued in code below)
}
\makeatother

First notice, that if main matter occurs, we are counting its first page twice – because we are remembering what page number was right after \mainmatter. We cannot remember what page number was before, because of \cleardoublepage in its definition. That’s why we decrease page count by one if \mainmatter was found. Next, we perform division modulo 4. We have to do it by hand, because TeX does not provide such operation. We are using the fact, that counters when divided always gives integer results rounded down. For example if our document had 17 pages, we do: 17+3=20, 20/4=5, 5*4=20, 20-17=3 (we have to add 3 pages). If we have 16 pages, we do: 16+3=19, 19/4=4.75 (round down to 4), 4*4=16, 16-16=0 (we have to add 0 pages). Finally adding pages. We could use loops, but we know, that we have at most 4 calls… so why bother?

\ifnum\pages@to@insert>0%
\newpage\null\thispagestyle{empty}\newpage%
\advance\pages@to@insert by -1%
\ifnum\pages@to@insert>0%
\newpage\null\thispagestyle{empty}\newpage%
\advance\pages@to@insert by -1%
\ifnum\pages@to@insert>0%
\newpage\null\thispagestyle{empty}\newpage%
\fi\fi\fi%

It’s true, it could be nicer. But again, it works and is easier than coding general looping mechanism in TeX. I hope you find it useful.

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: