I would like to typeset a LaTeX file and set the initial page number from the command line (as I need to often change it to merge with different documents). I thought that the following would work, but it does not:

pdflatex "\setcounter{page}{5}\input{test.tex}"

When I do this, the first page number is still 1 - does anyone have any idea why this might be?

3 Answers 3

pdflatex "\AtBeginDocument{\setcounter{page}{5}}\input{test.tex}"

Because that is basically the equivalent of putting \setcounter{page}{5} at the beginning of the LaTeX file, even before \documentclass{...}; at that point the page counter hasn't even been set up, and will be reset by the commands that follow.

There are lots of ways to achieve what you want. It would be easier to make suggestions if I knew what operating system you were using. On a Unix-based system, e.g., you could use sed.

One option that's relatively platform independent would be something like this: in your .tex file put something like this:


which reads the file pagenumbercommand.tex at compilation. (This could go, e.g., right after \begin{document} or something.) pagenumbercommand.tex could then consist of a single line, which you could rewrite at will. Then, e.g.:

echo "\setcounter{page}{5}" > pagenumbercommand.tex && pdflatex test.tex

would do what you wanted. (I think that would even work on Windows!) Things like LuaTeX would offer even easier solutions.


You could define a macro on the command line and use it right after \begin{document}. This gets around the problem @frabjous mentioned without another file.

Use pdflatex "\newcommand{\startpagecount}{\setcounter{page}{5}}\input test" and \startpagecount immediately after \begin{document}.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .