TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 14 down vote accepted
pdflatex "\AtBeginDocument{\setcounter{page}{5}}\input{test.tex}"
share|improve this answer

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

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

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.