2

I am importing bookmarks to an existing PDF with bookmark package.

I want bookmark of level=0 to do no action except to be a clickable link that expands/collapses bookmarks nested in it.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pdfpages}
\usepackage[
  pdfpagelabels=true,
  pdftitle={Optimization},
  pdfauthor={x}
]{hyperref}
\usepackage{bookmark}

\begin{document}
\pagenumbering{arabic}
\setcounter{page}{1}
\includepdf[pages=1-5]{example.pdf}

\bookmark[named=s, level=0]{Letter A}
\bookmark[page=1,level=1]{Ab}
\bookmark[page=2,level=1]{Ac}

\bookmark[named=s, level=0]{Letter B}
\bookmark[page=3, level=1]{Ba}
\bookmark[page=4, level=1]{Bb}

\end{document}

Currently with above code named=s I get a passive link that performs nothing when opened with a PDF reader in Windows 7. However, with PDF readers in mobile tablets for some reason it takes to the first page of the PDF. Please note that nothing is special with s in named=s.

What I want is that it functions as a button that collapses/expands nested bookmarks. For example, if Letter A is clicked then it simply expands to reveal bookmarks of Ab and Ab. If clicked again it collapses them.

15
  • I also get that Letter A and Letter B are not hyperlinks, but instead can just be expanded/collapsed. This seems reader/viewer dependent rather than anything to do with (La)TeX.
    – Werner
    Commented Jan 10, 2017 at 20:20
  • 1
    ...under SumatraPDF.
    – Werner
    Commented Jan 10, 2017 at 20:26
  • Same thing with SumatraPDF.
    – KitKat
    Commented Jan 10, 2017 at 20:31
  • All I want to know what to pass as an argument into \bookmark[named=s, level=0]{Letter A} instead of named=s to make it work.
    – KitKat
    Commented Jan 10, 2017 at 20:32
  • 2
    Ah, then this is definitely off-topic, since it has to do with the functionality of the reader, not with (La)TeX.
    – Werner
    Commented Jan 10, 2017 at 21:02

1 Answer 1

3

PDF viewers with support for Adobe Reader's JavaScript can collapse and open bookmarks by JavaScript. The action for the bookmark is specified as raw action code (option rawaction):

rawaction={%
  /S/JavaScript%
  /JS(event.target.open=!event.target.open)%
}

The target of the event is the bookmark that is clicked.

The following example defines and uses a style top for bookmarks at level 0 and the JavaScript action:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pdfpages}
\usepackage[
  pdfpagelabels=true,
  pdftitle={Optimization},
  pdfauthor={x}
]{hyperref}
\usepackage{bookmark}

\begin{document}
\pagenumbering{arabic}
\setcounter{page}{1}
\includepdf[pages=1-5]{example.pdf}

\bookmarkdefinestyle{top}{
  level=0,
  rawaction={%
    /S/JavaScript%
    /JS(event.target.open=!event.target.open)%
  },
}

\bookmark[style=top]{Letter A}
\bookmark[page=1,level=1]{Ab}
\bookmark[page=2,level=1]{Ac}

\bookmark[style=top]{Letter B}
\bookmark[page=3, level=1]{Ba}
\bookmark[page=4, level=1]{Bb}
\end{document}

You must log in to answer this question.

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