Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 14 down vote accepted
pdflatex "\AtBeginDocument{\setcounter{page}{5}}\input{test.tex}"
share|improve this answer
add comment

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.

share|improve this answer
add comment

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}.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.