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
\bookmark[dest=tocpage]{Table of Contents}
\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?