Sign up ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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.

share|improve this answer
    
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
\bookmarkCurrentHref{Table of Contents}

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
http://tex.stackexchange.com/questions/74974/make-a-pdf-bookmark-point-towards-title?answertab=oldest
tex.stackexchange.com
crawl=CC-MAIN-2015-48
78150
application/http; msgtype=response
sha1:MKK27PEKKUXR2AJDDOU6CTBLZOPFCX4P
<urn:uuid:c1bf146e-96be-4049-820f-590b2f49284a>
2015-11-28T23:51:02Z
104.16.115.182
sha1:7N2J2NFJ2TPKJOM33FQ4UYVJ5LX5LRZ2
<urn:uuid:84a82dac-a390-4876-b04e-71c3d63fe4e6>
http://tex.stackexchange.com/questions/74974/make-a-pdf-bookmark-point-towards-title?answertab=oldest
response
<urn:uuid:3d835474-20d8-49d0-8b1c-fc5cef87446c>
robots: classic
hostname: ip-10-71-132-137.ec2.internal
software: Nutch 1.6 (CC)/CC WarcExport 1.0
isPartOf: CC-MAIN-2015-48
operator: CommonCrawl Admin
description: Wide crawl of the web for Nov 2015
publisher: CommonCrawl
format: WARC File Format 1.0
conformsTo: http://bibnum.bnf.fr/WARC/WARC_ISO_28500_version1_latestdraft.pdf