# Automatic target “here” with \bookmark (as with \pdfbookmark)?

\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):

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?

-
I'm sure it would be possible to include a key-value option for \bookmark that would place a \hypertarget and then also set the \bookmark dest value. Is this what you're after? –  Werner Jun 6 '12 at 17:19
Hi @Werner - yes; I was wandering if there is an "official" way to do something like that, so I can type both the target name and the bookmark command on one line (of course, I could always try to build my own macro for that - but I was wandering if there is something "officially" in the bookmark package for this). Thanks! –  sdaau Jun 6 '12 at 17:57
A quick view only shows dest, for which you need a location (or a target... \hypertarget). rawaction is not suggested according to the bookmark documentation while page is page-specific and so seems named. So I don't think so. –  Werner Jun 6 '12 at 18:05
Thanks for confirming, @Werner - would you mind posting your comment as an answer, so I can accept it? Cheers! –  sdaau Jun 6 '12 at 18:14
@sdaau is that the page numbers that we see in the bookmarks in Evince on the right side? Is this the default behaviour? –  pluton Sep 30 '12 at 2:15

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}

-

I don't believe there is a direct interface for this, but you can provide your own boolean key desthere say and use \phantomsection. (Note however that I have never used kvsetkeys and so am not convinced that this is the best way to define the key.)

\documentclass{article}

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

\makeatletter
\define@key{BKM}{desthere}[true]{%
\expandafter\ifx\csname if#1\endcsname\iftrue
\phantomsection
\bookmarksetupnext{dest=\@currentHref}
\fi
}
\makeatother

\begin{document}

\section{Section 1}

\lipsum[1-3]

\bookmark[desthere,rellevel=1,keeplevel,view={XYZ},bold,color=blue]{Test Bookmark Sect. I}

\lipsum[4-12]

\end{document}


Note also that the bookmark package changes \pdfbookmark to use the \bookmark macro, but it does not provide any direct way to add options. However, \bookmarksetupnext can be used there too:

\bookmarksetupnext{bold,color=blue}
\pdfbookmark[2]{Test Bookmark Sect. 1}{TstBookmark1}

-