11

In this MWE, I don't understand why the first PDF bookmark links to the first \section*, while the second bookmark doesn't.

What have I missed?

\documentclass{article}
\usepackage[hidelinks,colorlinks=false,bookmarks=true]{hyperref}
\usepackage[numbered]{bookmark}
\usepackage{lipsum}

\begin{document}

    \pdfbookmark[section]{1st section}{}
    \section*{Section 1}
    \lipsum[1-3]
    \pdfbookmark[section]{2nd section}{}
    \section*{Section 2}

\end{document}

Update #1

For Heiko's answer, I know it is less than issue, but I am curious to understand why the first bookmark points exactly to the section title

enter image description here

while the second doesn't

enter image description here

Update #2

When I tried to understand the effect of options bold or color, I found nothing bold nor red.

What do I misunderstand about these options?

\documentclass{article}
\usepackage[hidelinks,bookmarks=true]{hyperref}
\usepackage[numbered]{bookmark}
\usepackage{lipsum,xcolor}

\makeatletter
\newcommand*{\sectionbookmark}[1][]{%
  \bookmark[%
    level=section,%
    dest=\@currentHref,%
    #1%
  ]%
}
\makeatother

\begin{document}
    \section*{Section 2}
    \sectionbookmark[bold,color=red]{2nd section}
    \lipsum[1-3]
\end{document}

1 Answer 1

9

The last mandatory argument of \pdfbookmark must be a unique name. It is used for the internally created anchor:

\documentclass{article}
\usepackage[hidelinks,bookmarks=true]{hyperref}
\usepackage[numbered]{bookmark}
\usepackage{lipsum}

\begin{document}

    \pdfbookmark[section]{1st section}{sec1}
    \section*{Section 1}
    \lipsum[1-3]
    \pdfbookmark[section]{2nd section}{sec2}
    \section*{Section 2}

\end{document}

The problem with the previous approach is, that \pdfbookmark creates a new anchor and this occurs before the \section* command. Thus it happens, that the first anchor is closer at the \section* command, because at the place at the top of the page. The second \section* adds additional space. Even a page break can occur.

Package bookmark is already loaded, thus it can be used to call \bookmark instead. This command allows the setting of an existing destination. The last argument of \pdfbookmark for unique anchor names is no longer necessary. Package hyperref stores the last destination name in \@currentHref.

Macro \sectionbookmark uses \bookmark with this destination name and uses the section level as default. The example also shows, how it can be used for other section levels. Also additional options can be given, like font options (bold, italic) or option color.

Since the last destination name is used, \sectionbookmark must be given right after the \section* command:

\documentclass{article}
\usepackage[hidelinks,bookmarks=true]{hyperref}
\usepackage[numbered]{bookmark}
\usepackage{lipsum}

\makeatletter
\newcommand*{\sectionbookmark}[1][]{%
  \bookmark[%
    level=section,%
    dest=\@currentHref,%
    #1%
  ]%
}
\makeatother

\begin{document}

    \section*{Section 1}
    \sectionbookmark{1st section}
    \lipsum[1-3]

    \section*{Section 2}
    \sectionbookmark[bold]{2nd section}

    \subsection*{Subsection}
    \sectionbookmark[level=subsection]{subsection}

\end{document}
5
  • Many thanks for your answer, I would be grateful if you could consider my question update.
    – Diaa
    Commented Sep 20, 2016 at 17:57
  • @DiaaAbidou Answer updated. Commented Sep 20, 2016 at 20:37
  • I really appreciate your elite explanation. I have something I don't understand, why when I load xcolor package and use \sectionbookmark[bold,color=red]{2nd section} I found nothing bold nor red? Why are those options not doing anything? You may look at my second update.
    – Diaa
    Commented Sep 21, 2016 at 5:50
  • @DiaaAbidou You have to use a PDF viewer, which support this features, e.g. Adobe Acrobat. Commented Sep 21, 2016 at 5:56
  • Ohh that is my bad, SumatraPDF fooled me. Many thanks for considering my naïve inquiries.
    – Diaa
    Commented Sep 21, 2016 at 5:59

You must log in to answer this question.

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