2

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
\addcontentsline{toc}{chapter}{\indexname}
\printindex
\end{document}

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

6
  • 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).
    – Johannes_B
    Commented Oct 22, 2014 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?
    – wmnorth
    Commented Oct 22, 2014 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).
    – Johannes_B
    Commented Oct 22, 2014 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.
    – Johannes_B
    Commented Oct 22, 2014 at 16:59
  • I'll guess i have a bit of spare time coming up, i might add an answer to this.
    – Johannes_B
    Commented Oct 22, 2014 at 17:05

1 Answer 1

4

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
                \twocolumn[\@makeschapterhead{\indexname}]%
                \@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}%
  \idx@heading%
  \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
                  \idx@@heading{\indexname}]%
  \@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}{%
    {notoc}{0},{nottotoc}{0},{default}{0},{plainheading}{0},%
    {totoc}{1},{toc}{1},{notnumbered}{1}%
  }{#1}%
  \ifx\FamilyKeyState\FamilyKeyStateProcessed
    \ifcase \@tempa\relax
      \renewcommand*{\idx@@heading}{%
      \chapter*
      }%
    \or
      \renewcommand*{\idx@@heading}{%
      \addchap
      }%
    \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
%\addcontentsline{toc}{chapter}{\indexname}
\printindex
\end{document}
9
  • @lockstep Thanks for commenting those lines out. But where is your answer gone?
    – Johannes_B
    Commented Oct 23, 2014 at 13:50
  • 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.)
    – lockstep
    Commented Oct 23, 2014 at 14:08
  • Thank you for that answer. I delayed accepting it because it still left me confused, but I now see that that is due to my own (lack of) TeX knowledge. Thus, I'll accept it and go searching for the (a?) TeX book...
    – wmnorth
    Commented Nov 1, 2014 at 12:16
  • @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
    – Johannes_B
    Commented Nov 1, 2014 at 12:26
  • Or to put it another way, it's nice to know how an internal combustion engine works, but the important part is to get to work on time in the morning. You need to know how to drive the car.
    – Johannes_B
    Commented Nov 1, 2014 at 12:28

You must log in to answer this question.

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