[Tex/LaTex] Link to index in TOC points does not point to index, but to whatever is right before index

indexinglinkstable of contents

My LaTeX document contains an index which is also referenced in the table of contents via \addcontentsline.
The page number displayed in the TOC is correct.
The link, however, is not.
(If I view the document structure in my PDF viewer, Evince, the page number also appears to be wrong.)

I found out that the link always seems to point to whatever is right before the index in the document structure (for example a section, subsection or paragraph).

In my example, the link generated by Pdflatex points to the paragraph "Und so weiter" which is right before the index.
If I remove "Und so weiter" and compile again, the link will point to the subsection "Hallo".

Minimal working example

\documentclass[11pt, titlepage]{article}
\usepackage[ngerman]{babel}
\usepackage{blindtext}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{makeidx}
\usepackage{hyperref}

\pagestyle{plain}
\makeindex

\title{Stichwortverzeichnis bei \LaTeX\ korrekt verlinken}
\author{Kunigunde Knitterkleid}

%%%%% DOCUMENT %%%%%

\begin{document}
\maketitle

\setcounter{tocdepth}{3}
\tableofcontents
\thispagestyle{empty}
\clearpage

\section{Abschnitt 1}
\subsection{Blabla}
Lorem\index{Lorem|see{Lorem ipsum}} ipsum\index{Lorem ipsum} dolor sit amet, consectetuer adipiscing elit.
Donec ullamcorper, felis non sodales\index{sodales} commodo, lectus\index{lectus} velit ultrices augue, a dignissim nibh lectus placerat pede.
\clearpage

\section{Abschnitt 2}
\subsection{Hallo}
\paragraph{Und so weiter}
\blindtext
\clearpage

%%%%% INDEX %%%%%

\renewcommand{\indexname}{Stichwortverzeichnis}
\addcontentsline{toc}{section}{Stichwortverzeichnis}
\printindex

\end{document}

Oddly enough, it works perfectly when using a KOMA script class.

The only changes I made was changing the document class to scrartcl with the option index=totoc and removing the unnecessary \addcontentsline line.

Minimal working example using KOMA script

\documentclass[11pt, titlepage, index=totoc]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage{blindtext}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{makeidx}
\usepackage{hyperref}

\pagestyle{plain}
\makeindex

\title{Stichwortverzeichnis bei \LaTeX\ korrekt verlinken}
\author{Kunigunde Knitterkleid}

%%%%% DOCUMENT %%%%%

\begin{document}
\maketitle

\setcounter{tocdepth}{3}
\tableofcontents
\thispagestyle{empty}
\clearpage

\section{Abschnitt 1}
\subsection{Blabla}
Lorem\index{Lorem|see{Lorem ipsum}} ipsum\index{Lorem ipsum} dolor sit amet, consectetuer adipiscing elit.
Donec ullamcorper, felis non sodales\index{sodales} commodo, lectus\index{lectus} velit ultrices augue, a dignissim nibh lectus placerat pede.
\clearpage

\section{Abschnitt 2}
\subsection{Hallo}
\paragraph{Und so weiter}
\blindtext
\clearpage

%%%%% INDEX %%%%%

\renewcommand{\indexname}{Stichwortverzeichnis}
\printindex

\end{document}

Does anybody have any idea how I can make the link point to the correct location (apart from using KOMA script instead)?

I am using Tex Live 2009 on Linux.

Best Answer

\addcontentsline doesn't set an achor; it just refers to the latest previous location where an anchor is set and that's why the hyperlink points to the wrong location (a sectional unit, in your case). To get the correct result, use \phantomsection from hyperref to set an anchor at the right location:

\phantomsection
\addcontentsline{toc}{section}{Stichwortverzeichnis}
\printindex

Another option is to use the tocbibind package:

\documentclass[11pt, titlepage]{article}
\usepackage[ngerman]{babel}
\usepackage{blindtext}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[nottoc]{tocbibind}
\usepackage{makeidx}
\usepackage{hyperref}

\pagestyle{plain}
\makeindex

\title{Stichwortverzeichnis bei \LaTeX\ korrekt verlinken}
\author{Kunigunde Knitterkleid}

%%%%% DOCUMENT %%%%%

\begin{document}
\maketitle

\setcounter{tocdepth}{3}
\tableofcontents
\thispagestyle{empty}
\clearpage

\section{Abschnitt 1}
\subsection{Blabla}
Lorem\index{Lorem|see{Lorem ipsum}} ipsum\index{Lorem ipsum} dolor sit amet, consectetuer adipiscing elit.
Donec ullamcorper, felis non sodales\index{sodales} commodo, lectus\index{lectus} velit ultrices augue, a dignissim nibh lectus placerat pede.
\clearpage

\section{Abschnitt 2}
\subsection{Hallo}
\paragraph{Und so weiter}
\blindtext
\clearpage

%%%%% INDEX %%%%%

\renewcommand{\indexname}{Stichwortverzeichnis}

\printindex

\end{document}
Related Question