Thursday, January 22, 2009

Sequential page numbering in LaTeX

Did you ever notice that the page number that your PDF reader gave you was not the same as the page number printed at the bottom of the page? By default, some LaTeX commands, such as \tableofcontents, reset the page number to 1.

I don't like this behaviour. I just want my cover page to be numbered 1, the next to be numbered 2, etcetera. This way, the ‘physical’ and ‘virtual’ page numbers line up nicely. It also prevents some problems with hyperref, which will otherwise create duplicate page labels.

You can get sequential page numbering by putting the following snippet in your document preamble:

\let\oldsetcounter=\setcounter
\renewcommand\setcounter[2]{%
    \def\arg{#1}\def\pg{page}%
    \ifx\arg\pg\else\oldsetcounter{#1}{#2}\fi}

This simply overrides \setcounter, such that it ignores any attempt to set the page counter, which holds the current page number. Ugly, but it works.

5 comments:

Akshay said...

Hey,
nice blog, helped me a lot!!!
keep it up!!
:)

o.a.mendez said...

Thanks for the post on numbering, it did the trick for me as well.

Mark IJbema said...

This mysteriously screwed up my appendix numbering. I fixed it using the following dirty hack:

\let\oldappendix=\appendix
\renewcommand\appendix{%
\oldappendix%
\oldsetcounter{chapter}{0}%
}

I don't get why this is needed though. And it scares me a bit, maybe other stuff goes mysteriously wrong as well then? I don't see how the \appendix command would use \setcounter{page}{..}.

Thomas ten Cate said...

@Mark IJbema: Well spotted. In my own thesis, I used a slightly different version, depending on the ifthen package. The commands I presented here were buggy. I edited the post with a new version, that (as far as I can tell) fixes the problem.

corto said...

Please add there that we have to use the "ifthen" package to get this to work. Add this in preamble:

\usepackage{ifthen}

I'm just starting with Latex so it took me a while to realise why this code is working for me :)

But it's great now, thanks a lot!!