[Tex/LaTex] Automatic target “here” with \bookmark (as with \pdfbookmark)

bookmarkshyperref

Please consider the following MWE:

\documentclass{article}

\usepackage{hyperref}
\usepackage{xcolor,bookmark}
\usepackage{lipsum}

\begin{document}

\section{Section 1}

\lipsum[1-3]

  % hyperref:
  \pdfbookmark[2]{Test Bookmark Sect. 1}{TstBookmark1}
  \lipsum[4-12]

\section{Section 2}

  \lipsum[13-16]

  % bookmark:
  \bookmark[page=\thepage,rellevel=1,keeplevel,view={XYZ},bold,color=blue]{Test Bookmark Sect. 2}
  %
  % bookmark with target:
  \hypertarget{hereSectII}{}% second arg prints text!
  \bookmark[dest=hereSectII,rellevel=1,keeplevel,view={XYZ},bold,color=blue]{Test Bookmark Sect. II}
  %
  \lipsum[17-20]

\section{Section 3}

  Finished.

\end{document}

… which generates output like this (left evince, right Adobe Reader):

evince_reader.png

As it can be seen, the target location being focused when the "Test Bookmark Sect. 1":

\pdfbookmark[2]{Test Bookmark Sect. 1}{TstBookmark1}

… is clicked, is between the paragraphs – basically, the target of the bookmark is "here" (so to speak), the location where \pdfbookmark was placed in the code.

My question is – is there a similar syntax for \bookmark?

If I specify a page action, that one links to the top of a page – and otherwise, I have to manually specify a \hypertarget:

\hypertarget{hereSectII}{}
\bookmark[dest=hereSectII]{Test Bookmark Sect. II}

Compared to this, it seems that \pdfbookmark inserts both a hypertarget in the document – and the bookmark (which shows in the PDF bookmarks index). Is there a similar syntax for \bookmark?

Best Answer

The bookmark package redefines \pdfbookmark as a compatibility-feature to hyperref:

\let\pdfbookmark\ltx@undefined
\newcommand*{\pdfbookmark}[3][0]{%
  \bookmark[level=#1,dest={#3.#1}]{#2}%
  \hyper@anchorstart{#3.#1}\hyper@anchorend
}

In essence, \pdfbookmark uses \bookmark with a specific dest key-value, and places both a \hyper@anchorstart and \hyper@anchorend to match that.

The following MWE provides a similar interface as an extension to \bookmark that now takes an optional *. When you use \bookmark[<options>]{<text>}, things work as before. However, \bookmark*[<options>]{<text>} overrides any use of dest in <options> in inserts an automated (and incremental) destination:

\documentclass{article}
\usepackage{hyperref,bookmark}% http://ctan.org/pkg/{hyperref,bookmark}
\usepackage{xcolor,lipsum}% http://ctan.org/pkg/{xcolor,lipsum}

\makeatletter
\newcounter{@bookmark@cntr}
\renewcommand{\the@bookmark@cntr}{autobookmark-\arabic{@bookmark@cntr}}
\let\@bookmark\bookmark
\renewcommand{\bookmark}{%
  \@ifstar\@bookmark@star\@bookmark
}
\newcommand{\@bookmark@star}[2][]{%
  \stepcounter{@bookmark@cntr}%
  \@bookmark[#1,dest=\the@bookmark@cntr]{#2}%
  \hyper@anchorstart{\the@bookmark@cntr}\hyper@anchorend%
}
\makeatother

\begin{document}

\section{Section 1}
\lipsum[1-3]

% hyperref:
\pdfbookmark[2]{Test Bookmark Sect. 1}{TstBookmark1}
\lipsum[4-12]

\section{Section 2}

\lipsum[13-16]

% bookmark:
\bookmark[page=\thepage,rellevel=1,keeplevel,view={XYZ},bold,color=blue]{Test Bookmark Sect. 2}
%
% bookmark with target:
\hypertarget{hereSectII}{}% second arg prints text!
\bookmark[dest=hereSectII,rellevel=1,keeplevel,view={XYZ},bold,color=blue]{Test Bookmark Sect. II}
%
\lipsum[17-20]

\section{Section 3}

% Updated \bookmark* usage.
\bookmark*[rellevel=1,keeplevel,view={XYZ},bold,color=blue]{Test Bookmark Sect. 3}

\lipsum[21-30]

\end{document}
Related Question