1

I have following code in which I use custom heading to:

  1. Create TOC entry
  2. Create custom bookmark

And I want first PDF bookmark to point to TOC page.

Since \addcontentsline{toc}{chapter}{#1} automatically creates PDF bookmarks if either hyperref or bookmark packages are loaded, I used \hypersetup{bookmarksdepth=-1} to supress those PDF bookmarks that are created with it. Thanks to it, I can create all bookmarks that I need with bookmark package.

\documentclass[final,11pt]{book}
\usepackage{blindtext}
\usepackage{hyperref}
\hypersetup{bookmarksdepth=-1}
\usepackage{bookmark}
\def \myheading#1{
\chapter*{#1}
\addcontentsline{toc}{chapter}{#1}
\bookmark[page=\thepage,level=-1]{My own bookmark for #1}
}
\begin{document}
\bookmark[page=\thepage,level=-1]{My own TOC bookmark}
\tableofcontents
\myheading{One}
\blindtext[10]
\myheading{Two}
\blindtext[10]
\myheading{Three}
\blindtext[10]
\myheading{Four}
\blindtext[10]
\myheading{Five}
\blindtext[10]
\end{document}

Code above produces document in which both TOC and PDF bookmarks point to the right places.

However, when I try to change page numbering, things start to go south.

When I use:

\documentclass[final,11pt]{book}
\usepackage{blindtext}
\usepackage{hyperref}
\hypersetup{bookmarksdepth=-1}
\usepackage{bookmark}
\def \myheading#1{
\chapter*{#1}
\addcontentsline{toc}{chapter}{#1}
\bookmark[page=\thepage,level=-1]{My own bookmark for #1}
}
\begin{document}
\frontmatter
% \bookmark[page=\thepage,level=-1]{My own TOC bookmark} % This is commented out, because adding it will cause compilation error.
\tableofcontents
\mainmatter
\myheading{One}
\blindtext[10]
\myheading{Two}
\blindtext[10]
\myheading{Three}
\blindtext[10]
\myheading{Four}
\blindtext[10]
\myheading{Five}
\blindtext[10]
\end{document}

TOC works just fine. But PDF bookmarks generated by bookmarks package got confused about page numbers of respective headings and they point to wrong place as a result: "Two" bookmark points to page 3 instead of 5, "Three" points to page 7 instead of 9 and so on.

And using bookmark for TOC causes compilation error (so I had to comment it out).

I was able to deal with this particular issue of compilation error by using \setcounter{page}{1} instead of \frontmatter/\mainmatter:

\documentclass[final,11pt]{book}
\usepackage{blindtext}
\usepackage{hyperref}
\hypersetup{bookmarksdepth=-1}
\usepackage{bookmark}
\def \myheading#1{
\chapter*{#1}
\addcontentsline{toc}{chapter}{#1}
\bookmark[page=\thepage,level=-1]{My own bookmark for #1}
}
\begin{document}
\bookmark[page=\thepage,level=-1]{My own TOC bookmark}
\tableofcontents
\myheading{One}
\setcounter{page}{1}
\blindtext[10]
\myheading{Two}
\blindtext[10]
\myheading{Three}
\blindtext[10]
\myheading{Four}
\blindtext[10]
\myheading{Five}
\blindtext[10]
\end{document}

but it makes bookmark for TOC to have same page number (1) as bookmark for "One" and all other PDF bookmarks pointing to wrong page.

How can I create proper custom bookmarks pointing to right page (as in first code sample), while at the same time changing numeration for everything before "One" to roman numerals (with TOC being on page "i") and "One" being on page 1.

(Of course TOC should also point to right pages, but it doesn't seem to be an issue.)

  • The page counter has the value 1 twice, since \frontmatter etc. changes the pagenumbering – user31729 Oct 21 '15 at 15:54
  • I suggest to use dest=... rather with some uniquely defined hyperref anchor instead of the page=... way – user31729 Oct 21 '15 at 16:06
1
  • \pdfbookmark can be used for the table of contents.

  • \texorpdfstring can be used to get different texts into the table of contents and the bookmarks. Then pdfbookmarksdepth=-1 is not needed.

Example:

\documentclass[final,11pt]{book}
\usepackage{blindtext}
\usepackage{hyperref}
\usepackage{bookmark}
\def \myheading#1{%
  \chapter*{#1}
  \addcontentsline{toc}{chapter}{\texorpdfstring{#1}{My own bookmark for #1}}%
}
\begin{document}
\frontmatter
\pdfbookmark[0]{My own TOC bookmark}{toc}
\tableofcontents
\mainmatter
\myheading{One}
\blindtext[10]
\myheading{Two}
\blindtext[10]
\myheading{Three}
\blindtext[10]
\myheading{Four}
\blindtext[10]
\myheading{Five}
\blindtext[10]
\end{document}

Result

  • Thank you: this solves my problem. Though, I am curious why bookmark package gets confused by \frontmatter/\mainmatter or any other change in page numeration. You are probably the best person to answer this since you wrote both hyperref and bookmarks packages :) (Ps. With your solution, bookmarks package does not need to be loaded at all, even though you included it in the example). – Rafal Oct 22 '15 at 10:44
  • 1
    @Rafal I would always load package bookmark, then the bookmarks get faster updated. – Heiko Oberdiek Oct 22 '15 at 17:40
  • 1
    @Rafal Option page expects the absolute page number. \thepage will only work, if the page number equals the absolute page number and its expansion is a plain number. – Heiko Oberdiek Oct 22 '15 at 17:42
2

Here's a proposition using dest= instead of page= - label usage

\documentclass[final,11pt]{book}
\usepackage{blindtext}
\usepackage{hyperref}
\usepackage[depth=-1]{bookmark}

\newcounter{fakecntr}

\newcommand{\myheading}[1]{%
\refstepcounter{fakecntr}
\phantomsection
\chapter*{\hypertarget{chap::\number\value{fakecntr}}{#1}}
\addcontentsline{toc}{chapter}{#1}
\bookmark[dest=chap::\number\value{fakecntr},level=-1]{My own bookmark for #1}
}
\begin{document}
\frontmatter
\bookmark[dest=toc::bookmark,level=-1]{My own TOC bookmark}
\hypertarget{toc::bookmark}{\tableofcontents}
\mainmatter
\cleardoublepage
\myheading{One}
\blindtext[10]
\myheading{Two}
\blindtext[10]
\myheading{Three}
\blindtext[10]
\myheading{Four}
\blindtext[10]
\myheading{Five}
\blindtext[10]
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.