15

I am using (plain) TeX (not LaTeX or any other ...TeX). I want a pdf file made from my TeX-source/output that is able to have acrobat reader shown logical page numbers. My document’s preface page numbering should be displayed like “vi (6 of 123)” and the main part like “2 (12 of 123)” in acrobat reader’s tool bar.

How do i achieve that? Do I have to use \special, pdfTeX, dvipdfm(x)? How do I specify the roman numeral page numbers in the TeX source? Is there a (TeX) macro package for that?

Thanks to all TeX Wizards for your answers!

Andrew Swann
  • 95,762
  • @MartinSchröder I wouldn't say so; the question has to somehow involve two counters---one for the PDF metadata, and one for the actual output. I don't (immediately) see how the linked addresses this... :/ – Sean Allred Aug 13 '13 at 11:50
  • @SeanAllred: Why two counters? The “(n of N)” part is implemented directly in Acrobat. The only thing to do here is tell the PDF which number and in which format is displayed on the page. So @MartinSchröder’s link points a duplicate. But I fear it won’t help for plain TeX (as requested here)?! – Tobi Aug 13 '13 at 12:01
  • @Tobi You're right, it doesn't seem hyperref supports plain TeX. Some people have suggested Eplain, but I have zero experience on that one. As you said though, the (n of N) bit is traditionally a reader feature, but I think OP is asking for the string itself to be in the PDF? (I think this needs some more explanation...) – Sean Allred Aug 13 '13 at 12:04
  • @SeanAllred: I never used plain TeX so can’t help in this case too … – Tobi Aug 13 '13 at 12:07
  • Yes, you need \pdfliteral with pdfTeX (don't know how to do this with dvipdfmx), and some heavy use of that. The relevant sections of the PDF spec are 12.4.2 (Page Labels) and 7.7.2 (Document Catalog, Key PageLabels). – Martin Schröder Aug 13 '13 at 16:17

1 Answers1

15

PDF page labels are a data structure in the /Catalog, e.g.:

/PageLabels <<
  /Nums [
    0 <<
      /P (Title)
    >>
    1 <<
      /S /D
    >>
    3 <<
      /S /A
      /St 7
    >>
  ]
>>

This means,

  • the first page number (page numbers are zero based) contains a prefix /P with string "Title".

  • The next two pages (2 and 3) are numbered with arabic numbers, starting with 1 (default).

  • The last page(s), page 4, is numbered with uppercase letters (LaTeX: \Alph) and the numeric values of the page numbers start with seven. Therefore the last page 4 is labeled G.

The documentation for PDF page labels can be found in the PDF specification.

Analyzing an example with hyperref

You can study, how hyperref writes the PDF data structures for PDF page labels.

The example hooks into some internals to expose the written PDF data structures, needed for PDF page labels:

\documentclass{article}
\usepackage[
  pdfpagelabels=true,
  bookmarks=false,
  pageanchor=false,
]{hyperref}

\makeatletter
\newcommand*{\print@arg}[2]{%
  \begingroup
    \edef\x{#2}%
    \@onelevel@sanitize\x
    \typeout{==> \string#1{\x}}%
  \endgroup
}

% DVI drivers
\@ifdefinable\org@special{%
  \let\org@special\special
  \renewcommand*{\special}[1]{%
    \org@special{#1}%
    \print@arg\special{#1}%
  }%
}

% pdfTeX
\ltx@IfUndefined{pdfcatalog}{%
}{%
  \ifpdf
    \@ifdefinable\org@Hy@PutCatalog{%
      \let\org@Hy@PutCatalog\Hy@PutCatalog
      \def\Hy@PutCatalog#1{%
        \org@Hy@PutCatalog{#1}%
        \print@arg\pdfcatalog{#1}%
      }%
    }%
  \fi
}
\makeatother

\begin{document}
\null
\thispdfpagelabel{Title}
\newpage
\pagenumbering{arabic}
\null
\newpage
\null   
\newpage
\pagenumbering{Alph}
\setcounter{page}{7}
\null
\newpage
\end{document}

Now the console output or the .log file contains the data, that can be used for plain TeX.

pdfTeX/LuaTeX

\pdfcatalog{/PageLabels<</Nums[0<</P(Title)>>1<</S/D>>3<</S/A /St 7>>]>>}

XeTeX/dvipdfm(x)

Two compile runs are needed, because the page labels are known at the end of the document and \specials cannot be written after the last page.

Since many \specials are output, the right one needs to be identified, it contains the string /PageLabels:

\special{pdf:docview <</PageLabels<</Nums[0<</P(Title)>>1<</S/D>>3<</S/A /St 7>>]>>>>}

dvips

Currently two runs are needed. (Perhaps I will reduce it to one in the future, using a common PostScript header file to overcome the \special limitation).

Again, the right special is found via searching for /PageLabels:

\special{ps:SDict begin [ {Catalog} <</PageLabels<</Nums[0<</P(Title)>>1<</S/D>>3<</S/A /St 7>>]>>>> /PUT pdfmark end}
Heiko Oberdiek
  • 271,626