[Tex/LaTex] Index is incorrectly listed in the table of contents

indexingpage-numberingtable of contents

I have a large document with an index compiled through makeindex, and is included in my source .tex as follows:

\cleardoublepage
\phantomsection 
\addcontentsline{toc}{chapter}{Index}
\printindex

However, the \addcontentsline does not correctly add the Index to either the Table of Contents, or the list of bookmarks in the resulting PDF.

Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . 330

One solution is to write a new command, \immediateaddcontentsline, and this adds the Index to both the TOC and the list of Bookmarks. The Bookmark link works correctly. But in the TOC, the page number is now listed with roman numerals as "xvi" rather than "349".

Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . 330

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi

"xvi" is the current page, so it looks like something is mighty confused. It is probably related to the immediate-ness of the new TOC command.

Is it even possible to have an Index listed the Table of Contents? Or must I use a different index maker?

Best Answer

Instead of tinkering with \addcontentsline, simply load my idxlayout package with the totoc option. (idxlayout is compatible with the standard classes, the KOMA-Script classes, and memoir.)

\documentclass{report}

\usepackage{makeidx}
\makeindex

\usepackage[totoc]{idxlayout}

\usepackage{hyperref}

\begin{document}

\tableofcontents

\chapter{foo}

Some text.\index{Text}

\printindex

\end{document}
Related Question