37

I want to add the \indexname to the table of contents and to the bookmarks.

Now, I know one way to do this job: add the follow code before \printindex:

\phantomsection
\cleardoublepage
\addcontentsline{toc}{chapter}{\indexname}

but I want to move all the styling or customize code to single one .sty file

Anybody knows how to do this through renew \printindex command or some method can define in a .sty file?

EDIT: add the MWE

\documentclass{book}
\usepackage{makeidx}
\usepackage{hyperref}
\usepackage{lipsum}


\makeindex
\begin{document}

\frontmatter
\tableofcontents


\mainmatter
\chapter{one}
\lipsum[1]
\index{word}

\backmatter
\appendix
\phantomsection
\cleardoublepage
\addcontentsline{toc}{chapter}{\indexname}
\printindex


\end{document}
9
  • 3
    Based on your loaded packages and document class. Please provide a minimal working example. (Use \phantomsection after cleardoublepage. May 26, 2012 at 13:14
  • 2
    The package tocbibind allows for automatic insertion. Also idxlayout and imakeidx do, though with different ways and purposes.
    – egreg
    May 26, 2012 at 13:18
  • possible duplicate of Index is incorrectly listed in the table of contents
    – lockstep
    May 26, 2012 at 13:20
  • @lockstep: Nor really. It depends on the package and class. For example in your linked question is no KOMA support or imakeidx, May 26, 2012 at 13:29
  • @MarcoDaniel My idxlayout package does support KOMA-Script and memoir.
    – lockstep
    May 26, 2012 at 13:30

3 Answers 3

53

Three ways.

  1. tocbibind

    \usepackage{makeidx}
    \makeindex
    \usepackage[nottoc]{tocbibind}
    
  2. idxlayout

    \usepackage{makeidx}
    \makeindex
    \usepackage[totoc]{idxlayout}
    
  3. imakeidx

    \usepackage{imakeidx}
    \makeindex[intoc]
    

With methods 2 and 3 you have also that the last page of the index is balanced.

1
  • 4
    I was looking for that - thanks! I found this solution to fit my needs best: \usepackage{imakeidx}, \indexsetup{level=\section}, \makeindex.
    – gilu
    Apr 14, 2016 at 8:36
7

I've struggled with the very same problem, trying to make tocbibind make what I wanted it to. (add Index to TOC). Unfortunately with the above tocbibind-answer, the link (using hyperref) jumps to the chapter before the index. In my case, this is the bibliography. I fixed this by editing the tocbibind.sty file (you can probably do this in your own .sty-file). However, this solution requires the multicol package.

First of all, I added the following line after ProvidesPackage :

\RequirePackage{multicol}

Then, I re-defined the \printindex command, which adds a \chapter or \chapter* in front of the index environment (I think the actual pain in the a** is that the generated .ind file only holds the theindex environment and no command for adding the chapter/section/ other caption):

\if@inltxdoc\else
  \renewcommand{\printindex}{
    \if@bibchapter
      \if@donumindex
        \chapter{\indexname}
      \else
        \chapter*{\indexname}
          \if@dotocind
            \addcontentsline{toc}{chapter}{\indexname}
          \fi
        \fi
      \fi
    \addcontentsline{toc}{chapter}{\indexname}
    \@input@{\jobname.ind}
  }
\fi

Finally, in the code renewing the theindex environment I deleted the stuff from the \if@bibchapter branch (which now only reads \begin{multicols}{2}) and added the according \if@bibchapter\end{multicols}\fi into the environment's ending code:

\if@inltxdoc\else
  \renewenvironment{theindex}%
    {\if@twocolumn
       \@restonecolfalse
     \else
       \@restonecoltrue
     \fi
     \if@bibchapter
        \begin{multicols}{2}
     \else
        \if@donumindex
          \twocolumn[\vspace*{-1.5\topskip}%
                     \@nameuse{\@tocextra}{\indexname}]%
          \csname \@tocextra mark\endcsname{\indexname}
        \else
          \if@dotocind
            \twocolumn[\vspace*{-1.5\topskip}%
                       \toc@headstar{\@tocextra}{\indexname}]%
            \prw@mkboth{\indexname}
            \addcontentsline{toc}{\@tocextra}{\indexname}
          \else
            \twocolumn[\vspace*{-1.5\topskip}%
                       \toc@headstar{\@tocextra}{\indexname}]%
            \prw@mkboth{\indexname}
          \fi
        \fi
     \fi
   \thispagestyle{plain}\parindent\z@
   \parskip\z@ \@plus .3\p@\relax
   \let\item\@idxitem}
   {\if@bibchapter\end{multicols}\fi\if@restonecol\onecolumn\else\clearpage\fi}
\fi

Now the index page has the very same chapter caption and the hyperref link works.

1
  • 1
    Your solution works also very well with \bibliography{}. Solved 2 problems !
    – SDrolet
    Jul 13, 2016 at 18:00
0

Here is what I did and seems to be working well. Add TOC request when you make the index:

\makeindex[columns=3, title=Alphabetical Index, intoc]

1
  • This requires the imakeidx package. Aug 30, 2022 at 0:21

You must log in to answer this question.

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