47

I am using KOMA-Script (scrreprt) and hyperref.

The PDF bookmarks are generated nicely from the table of contents, but I would like an extra Contents bookmark to point to the table of contents itself (without including an entry in the table of contents) - like the KOMA manual itself.

(Also list of figures and list of tables if I include them)

What is the best way to do this?

I could add a \pdfbookmark manually but I'm not sure how to get it to point correctly to the start of the section - if I do it after it will be on the wrong page (multipage toc), if I do before it will be the page before (since \tableofcontents does \cleardoublepage?). I guess I have to redefine \tableofcontents somehow.

Martin Scharrer
  • 262,582
robince
  • 2,836

3 Answers3

40

A general way for base classes using \addtocontents would be:

\addtocontents{toc}{\protect{\pdfbookmark[0]{\contentsname}{toc}}}

Similar for lof and lot with \listfigurename and \listtablename.

But, since KOMA-Script uses tocbasic, this would be better way and very simple:

\BeforeTOCHead[toc]{{\pdfbookmark[0]{\contentsname}{toc}}}

Just this line will get you the bookmark for the table of contents.

Here's a compilable example, where \BeforeTOCHead is used to create bookmarks for all such lists like toc, lof and lot.

\documentclass{scrbook}
\usepackage{hyperref}
\makeatletter
\BeforeTOCHead{%
  \cleardoublepage
    \edef\@tempa{%
      \noexpand\pdfbookmark[0]{\list@fname}{\@currext}%
    }\@tempa
}
\makeatother
\begin{document}
\tableofcontents
\listoffigures
\listoftables
\chapter{One}
text
\end{document}

No redefinition of \tableofcontents and no etoolbox would be required. Different names by babel are supported.

Stefan Kottwitz
  • 231,401
  • 1
    I tried \BeforeTOCHead[toc]{{\pdfbookmark[0]{\contentsname}{toc}}} but the bookmark seems to link to the blank left hand page before to toc (which starts on right hand side) – robince Aug 16 '10 at 12:48
  • 1
    Oh never mind \BeforeTOCHead[toc]{{\cleardoublepage\pdfbookmark[0]{\contentsname}{toc}}} seems to do it. Thanks very much – robince Aug 16 '10 at 12:50
  • Hmm, the code for all bookmarks doesn't work with the listings package, it points to the right page but the bookmark's name is empty. – letmaik Jan 06 '13 at 17:44
  • 1
    How to make the name of the bookmark be the same as the TOC's header it refers to? At the moment, I have the header in one language and the name of the bookmark in the PDF in English ("Contents"). – Anton Tarasenko Dec 16 '13 at 08:25
  • 1
    @Anton: Did you load babel, as in e.g. \usepackage[ngerman]{babel}? – dpprdan Dec 17 '13 at 22:10
  • @dapperdan Yep, babel has its language and TOC as a header is in local language, but not in the bookmarks. – Anton Tarasenko Dec 18 '13 at 02:21
  • 1
    @Anton: Sorry, then I guess you will have to post a new question with a MWE, because I cannot reproduce your problem with Stefan's example. Let us know here if you do, please. You might also want to check the order of your commands in the preamble, i.e. load babel early, though I doubt that this is the cause of your problem. – dpprdan Dec 18 '13 at 21:28
  • \pdfbookmark[1]{\contentsname}{toc} (level=1) arranges the added entries in line with the normal chapter/section bookmarks. Otherwise you can collapse the "normals" with the added ones. – Dominik Sep 10 '15 at 19:05
  • Your solution is great, but as neo has already mentioned in his comment, it does produce an empty label for \lstlistoflistings. Do you have an idea why and/or could provide a fix? – Stefan Pinnow May 26 '16 at 12:15
  • I'm trying NOT to use KOMA scripts. Adding the line \addtocontents{toc}{\protect{\pdfbookmark[0]{\contentsname}{toc}}} right above \tableofcontents in the document body worked perfectly for me. – jvriesem Oct 21 '20 at 19:29
18

The following code should create a correct ToC bookmark for both scrreprt and scrbook:

\makeatletter
\usepackage{etoolbox}
\pretocmd{\tableofcontents}{%
  \if@openright\cleardoublepage\else\clearpage\fi
  \pdfbookmark[0]{\contentsname}{toc}%
}{}{}%
\makeatother
lockstep
  • 250,273
  • 2
    For the lists of figures and tables, the above code should work by replacing \tableofcontents with \listoffigures resp. \listoftables, \contentsname with \listfigurename resp. \listtablename, toc with lof resp. lot. – lockstep Aug 16 '10 at 11:28
  • Where would this go: in the document preamble, after a certain \usepackage command, or in the document somewhere? – jvriesem Oct 21 '20 at 19:18
14

With KOMA scrreprt here are documentclass options for adding bibliography and the 'list of's to the table of contents:

\documentclass[
    a4paper,
    bibliography=totoc,
    listof=totoc,
    final
]{scrreprt} 

If you want to just add a pdf bookmark, you can use the command \pdfbookmark:

\pdfbookmark{\contentsname}{toc}
\tableofcontents
matth
  • 12,381
  • 4
    Although I prefer this solution to the other given answers, because it makes use of KOMA-options, the bookmark might point the end of page right before the TOC. To solve this, change it to: \clearpage\phantomsection\pdfbookmark{\contentsname}{toc} \tableofcontents – dhst Mar 07 '12 at 20:02