1

Simple minimal document

\documentclass[a4paper,10pt]{scrbook}

\usepackage{hyperref}

\begin{document}

\chapter*{Preface}
This is the preface.

\tableofcontents

\chapter{Chapter One}
This is chapter one.

\end{document}

By using hyperref, pdflatex produces a "Contents" section in the PDF file. This handy navigator does not include the TOC itself, for this document it only contains "Chapter One". I want my PDF Contents to include a link to the on-page TOC. (Naturally I don't want the on-page TOC to contain itself.) Is this possible?

  • 2
    Have you tried: putting\pdfbookmark[1]{Name}{Anchor} just before calling \tableofcontents where [1] is the level at which the bookmark should appear. You may need a \clearpage or \cleardoublepage before setting the bookmark to ensure it points to the right position. – Wiebke Jan 16 '17 at 14:18
2

The package tocbibind will add the ToC to the ToC itself, since hyperref is loaded, there will be ToC entry in the bookmarks itself. If the ToC should not appear in the ToC, temporarily decrease the tocdepth counter to -2 which means that nothing will be written to the ToC, use the old \latextableofcontents command and write an increased value for the tocdepth counter to the ToC file.

\documentclass[a4paper,10pt]{scrbook}

\usepackage{tocbibind}
\usepackage{hyperref}

\usepackage{bookmark}

\AtBeginDocument{%
\let\latextableofcontents\tableofcontents

\renewcommand{\tableofcontents}{%
  \addtocontents{toc}{\protect\setcounter{tocdepth}{-2}}
  \latextableofcontents%
  \addtocontents{toc}{\protect\setcounter{tocdepth}{3}}
}
}

\begin{document}

\chapter*{Preface}
This is the preface.

\tableofcontents

\chapter{Chapter One}
This is chapter one.

\end{document}

enter image description here

  • 1
    tocbibind is not recommended if you are using a KOMA-Script class. – Schweinebacke Jan 16 '17 at 14:45
  • 1
    @Schweinebacke: Well, I know that. I don't recommend KOMA in turn ;-) – user31729 Jan 16 '17 at 14:46
6

You can use \BeforeTOCHead to add a \pdfbookmark just before the heading of the table of contents. You need an additional \cleardoublepage before the \pdfbookmark otherwise the bookmark will be added to the last page of chapter preface.

\documentclass[a4paper,10pt]{scrbook}

\usepackage{hyperref}
\BeforeTOCHead[toc]{\cleardoublepage\pdfbookmark{\contentsname}{toc}}

\begin{document}

\chapter*{Preface}
This is the preface.

\tableofcontents

\chapter{Chapter One}
This is chapter one.

\end{document}

If you want similar additions for the list of figures or the list of tables, you may use:

\BeforeTOCHead[lof]{\cleardoublepage\pdfbookmark{\listfigurename}{lof}}
\BeforeTOCHead[lot]{\cleardoublepage\pdfbookmark{\listtablename}{lot}}

Alternatively you can add option listof=totoc to \documentclass, which would add these lists also to the table of contents. But don't use both!

  • 1
    Nice! With the info in your answer, I was able to find this answer here: tex.stackexchange.com/a/1824/88710 to add also the bookmarks to lot and lof – Wiebke Jan 16 '17 at 15:22
  • 1
    The second mandatory argument of \pdfbookmark is used to generate a unique anchor for the bookmark. I would not use \contentsname, depending on the language, it might contain "funny" stuff. Therefore, a plain ASCII letter string like "toc" avoids this. Example: \pdfbookmark{\contentsname}{toc} – Heiko Oberdiek Jan 16 '17 at 20:02
  • Neither of these methods makes any difference in my document. – spraff Jan 18 '17 at 17:57
  • @spraff Have you tested my example? Do you see the difference there? Are you using a package that is slightly incompatible with KOMA-Script and should not be used? If so, please add it to the MWE in your question. – Schweinebacke Jan 20 '17 at 6:48
  • I tested your example. Thanks for the suggestion but the other method worked for me so I accepted it. – spraff Jan 22 '17 at 0:14

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.