# KOMA book: correct hyperlink to the index?

So I'm using scrbook, and in the TOC, the link to the Index goes a bit beyond what it should, like described here. Unfortunately the solution described therein is for the book class, and it does not work for scrbook (*).

Now, I am able to solve the problem (by adding index=totoc to the document options, as described in this other link). But I only discovered this latter link after having drawn up a MWE. Further, with my MWE, the other solution proposed in the latter link -- using \phantomsection -- does not work: TOC's Index link still goes too far. Could anyone help? Thanks.

Here's the MWE:

\documentclass[oneside]{scrbook}
\usepackage{makeidx}
\usepackage{hyperref}

\makeindex

\newcommand{\nist}{%
NIST%
\index{National Institute of Standards and Technology}}
\begin{document}

\tableofcontents

\chapter{Introduction}
Lorem ipsum \nist.

\cleardoublepage
\phantomsection
\printindex
\end{document}


(*) - In particular, the patch command fails, because scrbook's \theindex command definition does not use twocolumn.

• Just to make sure, what is your question? In any case, you answer it yourself by telling about the option index=totoc. This option (global) should replace those three helper lines (clearpage, phantomsection, toc-line). Oct 22 '14 at 16:47
• I wanted to understand what is wrong with the MWE -- without using the solution you mention. Because as far as I know, the MWE should work fine without it, but doesn't. The question is why? Oct 22 '14 at 16:49
• The whole index is an environment internally, before taking its content (the index) it writes a chapter heading. The phantomsection is treated like the first content in the environment (after the heading). Oct 22 '14 at 16:56
• If the option is given, the internals use addchap for the index heading. Usually a good idea to use addchap for unnumbered stuff that shall appear in the TOC. Oct 22 '14 at 16:59
• I'll guess i have a bit of spare time coming up, i might add an answer to this. Oct 22 '14 at 17:05

The correct way is indeed to use index=totoc, but why? Let's take a look at the internals.

## Flowing down the levels of implementation

How is theindex environment defined in book and scrbook?

book.cls

\newenvironment{theindex}
{\if@twocolumn
\@restonecolfalse
\else
\@restonecoltrue
\fi
\@mkboth{\MakeUppercase\indexname}%
{\MakeUppercase\indexname}%
\thispagestyle{plain}\parindent\z@
\parskip\z@ \@plus .3\p@\relax
\columnseprule \z@
\columnsep 35\p@
\let\item\@idxitem}
{\if@restonecol\onecolumn\else\clearpage\fi}


scrbook.cls

\newenvironment{theindex}{%
\if@twocolumn
\@restonecolfalse
\else
\@restonecoltrue
\fi
\columnseprule \z@
\columnsep 35\p@
\setchapterpreamble{\index@preamble}%
\thispagestyle{\indexpagestyle}\parindent\z@
\setlength{\parskip}{\z@ \@plus .3\p@}%
\setlength{\parfillskip}{\z@ \@plus 1fil}%
\let\item\@idxitem
}{%
\if@restonecol\onecolumn\else\clearpage\fi
}


As you can see, both do basically the same. Checking for one/two-columnmode, setting the headers, changing the parskip and -indent. What standard book does was explained by Heiko before:

[It] suffers from a side effect of the optional argument of \twocolumn. The title in the optional argument is put at the top of page in one-column mode, but the current contents of the page (anchor setting) is stalled and is added after the title in two-column mode.

KOMA on the other hand does \idx@heading which is defined as:

  \if@openright\cleardoublepage\else\clearpage\fi%
\twocolumn[%
\@chaptertolistsfalse
\@mkboth{\MakeMarkcase{\indexname}}{\MakeMarkcase{\indexname}}%


It does the usual check and issues either one page or a double page. It uses \twocolumn with the optional argument as the standard class does and sets the headers. \@chaptertolostsfalse prevents the chapter heading from going to the list of figures and list of tables. The next line leads us to this code

\KOMA@key{index}{%
\KOMA@set@ncmdkey{index}{@tempa}{%
{totoc}{1},{toc}{1},{notnumbered}{1}%
}{#1}%
\ifx\FamilyKeyState\FamilyKeyStateProcessed
\ifcase \@tempa\relax
\chapter*
}%
\or
}%
\fi
\fi
}


If no value was given to index, scrbook uses \chapter* just like the standard book (and also if notoc and similar values were given). If you explicitely decided that the index should go to the toc, addchap is used. addchap calls \@addchap which calls \addchaptertocentry calling addtocentrydefault calling tocbasic@addxcontentsline which finally calls the ususal \addcontentsline in tocbasic.sty. All this still happens within the optional argument of twocolumn.

As you can see, Markus Kohm accounted for nearly every possible situation. Everything is designed in a way, that one change leads to a constant change in the whole document. All mechanisms are shared by the commands.

To sum it up, having a look into the KOMA-script documentation once in a while can save you some trouble searching for a hack.

\documentclass[oneside
,index=totoc
]{scrbook}
\usepackage{makeidx}
\usepackage{hyperref}

\makeindex

\newcommand{\nist}{%
NIST%
\index{National Institute of Standards and
Technology}
}
\begin{document}

\tableofcontents

\chapter{Introduction}
Lorem ipsum \nist.

%\cleardoublepage
%\phantomsection

• I deleted it because idxlayout actually isn't needed to solve OP's problem. (Altough my package is useful in other situations, e.g. for three-column indexes.) Oct 23 '14 at 14:08
• @wmnorth I can understand that this is confusing, but the good news is: You don't have to understand it. All you need to know, is index=totoc which is explained in the documentation. What happens in the background ... Who gives a rats a*s*s ;-p Nov 1 '14 at 12:26