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
while the second doesn't
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}
Best Answer
The last mandatory argument of
\pdfbookmark
must be a unique name. It is used for the internally created anchor: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. Packagehyperref
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 optioncolor
.Since the last destination name is used,
\sectionbookmark
must be given right after the\section*
command: