How to add \printindex to tableofcontents

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


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?

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

\makeindex
\begin{document}

\frontmatter
\tableofcontents

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

\backmatter
\appendix
\phantomsection
\cleardoublepage
\printindex

\end{document}

-
Based on your loaded packages and document class. Please provide a minimal working example. (Use \phantomsection after cleardoublepage. –  Marco Daniel May 26 '12 at 13:14
The package tocbibind allows for automatic insertion. Also idxlayout and imakeidx do, though with different ways and purposes. –  egreg May 26 '12 at 13:18
possible duplicate of Index is incorrectly listed in the table of contents –  lockstep May 26 '12 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, –  Marco Daniel May 26 '12 at 13:29
@MarcoDaniel My idxlayout package does support KOMA-Script and memoir. –  lockstep May 26 '12 at 13:30

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.

-

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 nidex 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
\fi
\fi
\fi
\@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}%
\prw@mkboth{\indexname}
\else
\twocolumn[\vspace*{-1.5\topskip}%
\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.

-