4

Is it possible to get section numbers in the PDF outline that shows up in PDF viewers?

By default, when enabling the PDF outline with \outlines, it shows up like this in my PDF viewer's sidebar:

Screenshot of the document outline as shown in the sidebar of a PDF viewer

I'd like to have each section in the sidebar prefixed by its section number like in the table of contents:

Screenshot of a region of the table of contents in the PDF document

1
  • 2
    this is an interesting question, but can you provide code for a minimal document we can compile to reproduce your situation? that is, an MWE or Minimum Working Example?
    – cfr
    Commented Aug 12 at 13:59

1 Answer 1

9

You can patch \_outlinesB using optex trick 137 in the following way

\replmacro \_outlinesB {} {} {"\\_tmp \{", "\\_tmp \{\#3 "} {}

\hyperlinks \Green \Red \outlines 0
\nonum\notoc\sec Contents
\maketoc

\sec Introduction
\lipsum[1]

\secc Motivation
\lipsum[2]

\secc Goals
\lipsum[4]

\sec Related Work
\lipsum[7]

\bye

enter image description here

or you can simply redefine it

\_def\_outlinesB#1#2#3#4#5#6#7{%
   \_advance\_tocrefnum by1
   \_isequal{\relax}{#4}\_iffalse
      \_advance\_count#1 by1
      \_ifcase#1%
         \_tmpnum=\_trycs{_ol:\_the\_count0}{0}\_or
         \_tmpnum=\_trycs{_ol:\_the\_count0:\_the\_count1}{0}\_relax\_or
         \_tmpnum=\_trycs{_ol:\_the\_count0:\_the\_count1:\_the\_count2}{0}\_relax\_or
         \_tmpnum=\_trycs{_ol:\_the\_count0:\_the\_count1:\_the\_count2:\_the\_count3}{0}\_relax\_or
         \_tmpnum = 0\_relax\_fi
      \_isempty{#4}\_iftrue \_pdfunidef\_tmp{#3 #5}\_else \_pdfunidef\_tmp{#3 #4}\_fi % <------------------------
      \_outlinesC{toc:\_the\_tocrefnum}{\_ifnum#1<\_outlinelevel\_space\_else-\_fi}{\_tmpnum}{\_tmp}%
   \_fi
}

\hyperlinks \Green \Red \outlines 0
\nonum\notoc\sec Contents
\maketoc

\sec Introduction
\lipsum[1]

\secc Motivation
\lipsum[2]

\secc Goals
\lipsum[4]

\sec Related Work
\lipsum[7]

\bye

The arguments of \_outlinesB are the same as for \_tocline, i.e.

\_tocline{⟨level⟩}{⟨type⟩}{⟨number⟩}{⟨o-title⟩}{⟨title⟩}{⟨gpageno⟩}{⟨pageno⟩}

see page 129 of optex-doc.pdf. since \_tmp holds the text that is being converted to UTF-16 and written to the outline, you can add the section number in there, before \_pdfunidef does its magic.

1
  • Thank you! I was looking at the \_outlinesB macro in the documentation but could not figure out how it works.
    – pinsl
    Commented Aug 13 at 6:06

You must log in to answer this question.

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