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 edit big documents, so it is useful for me to open the last page after compilation. Normally, the first page is opened, but by using hyperref, I can configure it to open any page at start.


But there is no variable lastpage. Can this be done in some other ways?

share|improve this question
Note that there is a named action LastPage, so with \Acrobatmenu{LastPage}{go to end} you get a "link" to the last page, but I don't know whether it's possible to trigger that when opening the PDF... – Stephan Lehmke Nov 20 '12 at 17:50
Many PDF readers can refresh the document if it changes: e.g., evince on Linux and Skim as well as Preview (?) for the Mac. Then you could "go" to the exact page you were working on, not just the last page. – Reid Nov 27 '12 at 20:47

A package such as zref-totpages that counts the absolute number of pages helps. The critical part is the timing:

  \ifnum\ztotpages>0 %
\chapter{Hello World}
\chapter{Last Chapter}

Update: In the first run, \ztotpages is not yet known and has value 0 causing an error with pdfTeX: page number must be positive. (hyperref 6.83o will add some validation for the value of pdfstartpage.)


  1. Loading of package \zref-totpages.
  2. Explicit call of \AtBeginDocument for setting pdfstartpage.
  3. Loading of package hyperref that
  4. calls \AtBeginDocument for using pdfstartpage.
  5. \begin{document} reads the .aux file.
    \zreftotpages is valid.
  6. The hook \AtBeginDocument is executed, first with \hypersetup to set pdfstartpage.
  7. At last hyperref uses pdfstartpage to set the starting page.

The last page is known at the end of the document, hyperref uses pdfstartpage earlier (TeX does not execute \special after the last DVI output page), thus two LaTeX runs are needed.

Remark: Package lastpage does not help here, because the page number of the last page can be different from the number of absolute pages, especially if there are resettings of the page counter (\frontmatter, \mainmatter, ...).

Named action LastPage

In a comment to the question Stephan Lehmke noted a named action LastPage. Package hyperref does not directly support this for the open action. But hyperref's open action specification can be disabled by an empty start page and the named action can be added manually, e.g. for pdfTeX:

\hypersetup{pdfstartpage={}}% disable openaction of hyperref
  \pdfcatalog{}openaction user{<</S/Named/N/LastPage>>}{}\relax
\chapter{Hello World}
\chapter{Last Chapter}
share|improve this answer
Just to be clear, does this take (at least) two runs to be correct? – Loop Space Nov 20 '12 at 15:50
Yes, at most, answer updated. – Heiko Oberdiek Nov 20 '12 at 15:58
I am getting the following warnings and errors: 1. LaTeX Warning: Reference 'LastPage' on page 1 undefined on input line 61. 2. ! pdfTeX error (ext1): page number must be positive. – Arindam Pal Nov 21 '12 at 2:20
How about @StephanLehmke's comment: Can you combine that with an action @ document loading? – Martin Schröder Nov 21 '12 at 9:31
@MartinSchröder Thanks for notifying, answer updated. – Heiko Oberdiek Nov 21 '12 at 23:09

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.