46

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.

3 Answers 3

17

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
2
  • 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, 2010 at 11:28
  • Where would this go: in the document preamble, after a certain \usepackage command, or in the document somewhere?
    – jvriesem
    Oct 21, 2020 at 19:18
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.

10
  • 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, 2010 at 12:48
  • 1
    Oh never mind \BeforeTOCHead[toc]{{\cleardoublepage\pdfbookmark[0]{\contentsname}{toc}}} seems to do it. Thanks very much
    – robince
    Aug 16, 2010 at 12:50
  • 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"). Dec 16, 2013 at 8:25
  • 1
    @Anton: Did you load babel, as in e.g. \usepackage[ngerman]{babel}?
    – dpprdan
    Dec 17, 2013 at 22:10
  • 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, 2013 at 21:28
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
1
  • 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 7, 2012 at 20:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .