# Make a PDF bookmark point towards title

I've been using the bookmark package to add custom PDF bookmarks to my thesis. More specifically, I started using this package because I didn't want the the ToC, LoF and LoT to appear in the ToC, but still appear in the bookmarks bar.

The problem I've been encountering is that the hypertarget does not point towards the top of the first page of the targeted section, but rather somewhere above it. The result is that as the ToC, LoF, and LoT get longer the hypertarget is more and more "off target".

Here's a MWE of the code I've been using:

\documentclass[12pt,a4paper]{book}

\usepackage[numbered]{bookmark}

\usepackage{hyperref}

\usepackage[nottoc, notlof, notlot,chapter]{tocbibind}

\begin{document}

\frontmatter

\hypertarget{tocpage}{}
\tableofcontents

\hypertarget{lofpage}{}
\listoffigures
\bookmark[dest=lofpage]{List of Figures}

\hypertarget{lotpage}{}
\listoftables
\bookmark[dest=lotpage]{List of Tables}

\mainmatter

\chapter{Chapter 1}

\section{Section 1.1}

\subsection{Subsection 1.1.1}
\begin{figure}[h]
\LARGE A
\caption{A figure}
\end{figure}
\subsection{Subsection 1.1.2}

\section{Section 1.2}

\subsection{Subsection 1.2.1}

\subsection{Subsection 1.2.2}

\chapter{Chapter 2}

\section{Section 2.1}
\begin{table}[h]
\caption{A table}
\end{table}
\subsection{Subsection 2.1.1}

\subsection{Subsection 2.1.2}

\section{Section 2.2}

\subsection{Subsection 2.2.1}

\subsection{Subsection 2.2.2}

\endmatter

\end{document}


UPDATE The solution I have in mind would behave similarly to what I assume is default hyperref behavior for sections and subsections: the hypertarget points to the title, regardlessly of where in the text that title is placed.

I tried adding something along the following lines in the preamble, using the etoolbox package and the \apptocmd command.

\usepackage{etoolbox}
\makeatletter
\AtBeginDocument{%
\apptocmd{\contentsname}{\hypertarget{tocpage}{}}{}{}
}
\makeatother


Of course, I commented out the existing \hypertarget{tocpage} in the MWE.

When I run the MWE with this code, I get the following error:

[1]./Thesis.toc !Missing control sequence inserted <inserted text> \inaccessible.


Any ideas on what I did wrong in the command definition?

-

Your listsof start a new page after hypertarget has chosen the target page. One way round this would be to insert a \cleardoublepage or a clearpage before hypertarget:

\cleardoublepage
\hypertarget{lofpage}{}
\listoffigures
\bookmark[dest=lofpage]{List of Figures}


Of course, if you were to use the memoir document class, you could use \clearforchapter, which is more semantic, and adjusts automatically for oneside/twoside.

-
This does help, but I'm looking at a solution that links to the title. This is the default behavior for sections, subsections, etc., where you do not need or should clear the page. I've posted an update of my preliminary attempts to make that happen. – Kpantzas Oct 2 '12 at 11:48

hyperref automatically defines something equivalent to the \hypertargets you are trying to insert, and already in the correct locations. Following a \chapter*, the name of the immediately preceding anchor is stored in \@currentHref. Therefore, in your \bookmark commands, you need only refer to \@currentHref (either with \makeatletter active or via \csname @currentHref\endcsname). Here, I have defined a macro \bookmarkCurrentHref in the preamble to save having to use @ in the document body:

\documentclass[12pt,a4paper]{book}
\usepackage[numbered]{bookmark}
\usepackage{hyperref}
\usepackage[nottoc, notlof, notlot,chapter]{tocbibind}

\makeatletter
\newcommand*{\bookmarkCurrentHref}[1][]{\bookmark[dest=\@currentHref,#1]}
\makeatother

\begin{document}

\frontmatter
\tableofcontents

\makeatletter
\listoffigures
\bookmarkCurrentHref{List of Figures}

\listoftables
\bookmarkCurrentHref{List of Tables}

\mainmatter

\chapter{Chapter 1}

\section{Section 1.1}

\subsection{Subsection 1.1.1}
\begin{figure}[h]
\LARGE A
\caption{A figure}
\end{figure}
\subsection{Subsection 1.1.2}

\section{Section 1.2}

\subsection{Subsection 1.2.1}

\subsection{Subsection 1.2.2}

\chapter{Chapter 2}

\section{Section 2.1}
\begin{table}[h]
\caption{A table}
\end{table}
\subsection{Subsection 2.1.1}

\subsection{Subsection 2.1.2}

\section{Section 2.2}

\subsection{Subsection 2.2.1}

\subsection{Subsection 2.2.2}

%\endmatter
\backmatter

\end{document}


As well as offering a solution to your problem, I'll try to explain why your attempts were not successful (note that any code here is offered as explanation only and I strongly suggest using the above approach in preference to anything here):

• Your patch to \contentsname causes issues because the macro is used more than once. In particular, it is used for setting the chapter name and is passed to \@mkboth. Here is the definition of \tableofcontents in effect in your MWE, revealed by \show\tableofcontents just after \begin{document}:

\if@twocolumn \@restonecoltrue \onecolumn \else \@restonecolfalse \fi \chapter *{\contentsname \@mkboth {\MakeUppercase \contentsname }{\MakeUppercase \contentsname }}\@starttoc {toc}\if@restonecol \twocolumn \fi

• If you were to manually redefine \tableofcontents so that only the first \contentsname is affected, it works as you expect:

\def\tableofcontents{\if@twocolumn \@restonecoltrue \onecolumn \else \@restonecolfalse \fi \chapter *{\hypertarget{tocpage}{}\contentsname \@mkboth {\MakeUppercase \contentsname }{\MakeUppercase \contentsname }}\@starttoc {toc}\if@restonecol \twocolumn \fi}


(Note that this needs surrounding with \makeatletter...\makeatother.)

Patching this with etoolbox would not be entirely trivial/minimal due to the \@mkboth also being part of the argument to the \chapter*.

• Similarly for the \listoffigures and \listoftables, except the \@mkboth is not part of the argument to \chapter* in those cases which makes them easier to patch, eg (though again, I don't suggest you do it):

\patchcmd{\listoffigures}{\chapter *{\listfigurename }}{\chapter*{\hypertarget{lofpage}{}\listfigurename}}{}{\errmessage{failed to patch}}


(Note that this does not need surrounding with \makeatletter...\makeatother.)

-