[Tex/LaTex] How to add \printindex to tableofcontents

indexingtable of contents

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}

Best Answer

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.

Related Question