3

I use \addcontentsline for putting an entry for the index in the table of contents, but I don't want the page number and I want a hyperlink. How can I do this?

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[makeindex]{splitidx}
\usepackage{hyperref}
\newcommand{\ITerm}{IndexTerms}
\newcommand{\IServ}{IndexServs}
\newcommand{\MyLarge}{\Large}
\newcommand{\smindexxspecial}[2]{{\MyLarge#2} \sindex[#1]{#2}}
\newcommand{\smindexspecial}[2]{{\MyLarge#2}\sindex[#1]{#2}}
\newcommand{\smindexvspecial}[2]{{\MyLarge#2}\sindex[#1]{#2}}
\newcommand{\smsindexspecial}[2]{{\MyLarge \url{#2}}\sindex[#1]{#2}}
\newindex[General Index]{IndexTerms}
\newindex[Websites]{IndexServs}
\tableofcontents
%\addcontentsline{toc}{chapter}{General Index}
\addtocontents{toc}{\protect\contentsline {chapter}{General Index}{}{}}
\printindex[\ITerm]
\addcontentsline{toc}{chapter}{Websites}
\printindex[\IServ]        
 And this is the end of the story.
\end{document}

When I do this one

\addtocontents{toc}{\protect\contentsline {chapter}{General Index}{}{}}

It appears without pagenumber in table of contents, but without hyperlink.

But when I use this one

%\addcontentsline{toc}{chapter}{General Index}

I have page number and a hyperlink.

Also I want it without pagenumber and with hyperlink.

I should mention that this is nothing to do with index termns page but with index names.. I have two index. The first one named as General Index and the other one Websites. I want this index names in table of contents.

  • 1
    Why should the index have no page number in the TOC? – egreg Oct 18 '13 at 14:20
  • @egreg My tutor want this ;) – blue Oct 18 '13 at 14:21
  • 2
    “Vuolsi così colà dove si puote ciò che si vuole, e più non dimandare” says Virgil to Dante in the “Divine Comedy”: “Thus is wanted where will is power, ask no more” could be a translation. ;-) – egreg Oct 18 '13 at 14:24
3

With a minimum of different markup in the document, here's a way:

\documentclass{book}
\usepackage{imakeidx}
\usepackage{hyperref}

% This command does the same as \printindex
% but also encloses the entry in the TOC in a group
% and activates the change of meaning of \l@chapter
\newcommand{\printindexnonumber}[1][\jobname]{%
  \addtocontents{toc}{\begingroup\protect\nonextpagenumber}
  \printindex[#1]
  \addtocontents{toc}{\endgroup}
}
\makeatletter
% this command modifies \l@chapter to ignore the page number
\newcommand\nonextpagenumber{%
  \let\orig@l@chapter\l@chapter
  \def\l@chapter##1##2{\orig@l@chapter{##1}{}}%
}
\makeatother

\makeindex[intoc]

\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{A}
Some text\index{text}

\backmatter
\printindexnonumber

\end{document}

So you have only to use \printindexnonumber instead of \printindex; it will be easy to get back if your tutor changes her/his mind.


For your case with splitindex the solution is similar, it's just necessary to patch \theindex so it will make a note in the Table of Contents. (Note: tocbibind doesn't make the correct link.)

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[makeindex]{splitidx}
\usepackage[colorlinks]{hyperref}

\usepackage{etoolbox}
\makeatletter
\patchcmd{\theindex}
  {\@makeschapterhead}
  {\add@index@to@toc\@makeschapterhead}
  {}{}
\newcommand{\add@index@to@toc}{%
  \csname phantomsection\endcsname
  \addcontentsline{toc}{chapter}{\indexname}%
}
\makeatother


\newcommand{\printindexnonumber}[1][\jobname]{%
  \addtocontents{toc}{\begingroup\protect\nonextpagenumber}
  \printindex[#1]
  \addtocontents{toc}{\endgroup}
}
\makeatletter
% this command modifies \l@chapter to ignore the page number
\newcommand\nonextpagenumber{%
  \let\orig@l@chapter\l@chapter
  \def\l@chapter##1##2{\orig@l@chapter{##1}{}}%
}
\makeatother

\newindex[General Index]{IndexTerms}
\newindex[Websites]{IndexServs}

\begin{document}
\frontmatter
\tableofcontents
\printindexnonumber[IndexTerms]
\printindexnonumber[IndexServs]
\mainmatter
\chapter{Title}
 And this is the end of the story.
\sindex[IndexTerms]{X}
\sindex[IndexServs]{Y}
\end{document}

I don't see why using \ITerm and \IServ, when the names are just the same. I removed the \smindexxspecial kind macros from the example to keep it minimal. The colorlinks option to hyperref is just for showing that links are produced.

enter image description here

  • I think I described my problem wrong. Only the Index Name should be appear in table of contents without pagenumber not the whole index terms. I have two index and they named as general indey and website. I will edit my question, I solve the problem but I need hyperlink too. – blue Oct 18 '13 at 16:10
  • @blue I don't understand. My solution works for as many indices as you want and the hyperlink is created to the index page. – egreg Oct 18 '13 at 16:14
  • I edited my question. – blue Oct 18 '13 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.