8

I am trying to understand the \pdfbookmark[level]{text}{name} macro. Here are my questions:

  1. Is it defined in the LaTeX core? Where can I see the code?
  2. Where do I find the documentation?
  3. I happen to be using hyperref, and hyperref.sty has this: \newcommand\pdfbookmark[3][]{}, which basically means 3 args (1 optional, 2 mandatory), empty body. What is this doing?
  4. What is the level?

(Note: I am a novice in LaTeX, so would appreciate simpler answers, if possible.)

fishfin
  • 301

2 Answers2

10

\pdfbookmark it is a hyperref command. The definition in hyperref.sty is a dummy, the real definitions are in the various backend files, e.g. hpdftex.def (for pdflatex) contains

\renewcommand\pdfbookmark[3][0]{%
  \Hy@writebookmark{}{#2}{#3.#1}{#1}{toc}%
  \hyper@anchorstart{#3.#1}\hyper@anchorend
}

The level allows to move up and down in the tree. With the name argument hyperref will create a target, so the name should be unique. You must compile the document twice.

\documentclass{article}
\usepackage{hyperref}
\begin{document}

\section{a section bookmark}

\newpage A\pdfbookmark[1]{text A}{targetA}

\newpage B\pdfbookmark[2]{text B}{targetB}

\end{document}

enter image description here

If you want more options to create bookmarks, use the bookmark package. It has more powerful interfaces and requires also only one compilation.

Ulrike Fischer
  • 327,261
  • UlrikeFischer, that helped a lot, thank you!

    So basically:

    1. \section was automatically bookmarked by \hyperref. Some internal anchor-name must have been created.
    2. I can give any name (anchor-name), it just creates an anchorpoint at that place in the document.

    I realize I had made a mistake earlier. Since I wasn't compiling twice, I was not able to see the changes I had made to the text/name. So my question 5 is wrong, I will delete it.

    I will try out the bookmark package.

    – fishfin May 18 '23 at 14:05
2

Based on UlrikeFischer's explanation, I tried the following just to experiment. In case it helps anyone, posting it here. And do not forget to compile twice if using hyperref for pdfbookmarks.

\documentclass{article}
\usepackage{hyperref}
\begin{document}

\section{Section Bookmark}

\newpage \pdfbookmark[0]{Text A}{targetA} A \newpage \pdfbookmark[1]{Text B}{targetB} B \newpage \pdfbookmark[2]{Text C}{targetC} C \newpage \pdfbookmark[2]{Text D}{targetD} D \newpage \pdfbookmark[4]{Text E}{targetE} E Skipped Level \newpage \pdfbookmark[3]{Text F}{targetF} F \newpage \pdfbookmark[1]{Text G}{targetG} G \newpage \pdfbookmark[2]{Text H}{targetH} H \newpage \pdfbookmark[0]{Text I}{targetI} I \end{document}

hyperref Snapshot

And here's how bookmark package works:

\documentclass{article}
\usepackage{bookmark}
\begin{document}

\section{Section Bookmark}

\newpage \hypertarget{targetA}{} \bookmark[level=0,dest=targetA]{Text A} A \newpage \hypertarget{targetB}{} \bookmark[level=1,dest=targetB]{Text B} B \newpage \hypertarget{targetC}{} \bookmark[level=2,dest=targetC]{Text C} C \newpage \hypertarget{targetD}{} \bookmark[level=2,dest=targetD]{Text D} D \newpage \hypertarget{targetE}{} \bookmark[level=4,dest=targetE]{Text E} E Skipped Level \newpage \hypertarget{targetF}{} \bookmark[level=3,dest=targetF]{Text F} F \newpage \hypertarget{targetG}{} \bookmark[level=1,dest=targetG]{Text G} G \newpage \hypertarget{targetH}{} \bookmark[level=2,dest=targetH]{Text H} H \newpage \hypertarget{targetI}{} \bookmark[level=0,dest=targetI]{Text I} I \end{document}

bookmark Snapshot

fishfin
  • 301