I am writing a document using the book class, and the hyperlink to the index sends the pdf viewer a bit too far, i.e. when I click the top of the screen is at the start of the index instead of displaying the index title. Here is a MWE :
\documentclass{book}
\usepackage{hyperref}
\usepackage{makeidx}
\makeindex
\begin{document}
\cleardoublepage
\phantomsection
\addcontentsline{toc}{section}{Table of Contents}
\renewcommand{\contentsname}{Table of Contents} % Original name = Contents
\tableofcontents
\index{foo}
\cleardoublepage
\phantomsection
\addcontentsline{toc}{chapter}{\indexname}
\printindex
\chapter{foo}
\end{document}
When I compile it and click on index, I get to that position :
How can I have the hyperlink to index send exactly to the index page, with the index title clearly visible at the top ?
Edit: Fixed the order of phantomsection and addcontentsline
Edit 2 : The log with a verbose hyperref is the following :
Package hyperref Info: Anchor `Doc-Start' on input line 18.
Package hyperref Info: Anchor `chapter*.1' on input line 20.
(D:\Travail\Bugtex\bug.toc
Package hyperref Info: bookmark on input line 1:
(hyperref) thecounter {0}
(hyperref) text {\contentsname }
(hyperref) reference {chapter*.1}
(hyperref) toclevel {0}
(hyperref) type {toc}.
Package hyperref Info: Reference (link) `chapter*.1' on input line 2.
Package hyperref Info: Reference (link) `section*.2' on input line 3.
Package hyperref Info: Reference (link) `chapter.1' on input line 4.
)
\tf@toc=\write4
Package hyperref Info: Anchor `page.1' on input line 23.
[1
{C:/Users/Arnaud/AppData/Local/MiKTeX/2.9/pdftex/config/pdftex.map}
Package `hyperref' Info: End of reference (link) `chapter*.1', line 2.
Package `hyperref' Info: End of reference (link) `section*.2', line 3.
Package `hyperref' Info: End of reference (link) `chapter.1', line 4.
]
Package hyperref Info: Anchor `page.2' on input line 23.
[2
]
Package hyperref Info: Anchor `section*.2' on input line 24.
Package hyperref Info: bookmark on input line 25:
(hyperref) thecounter {0}
(hyperref) text {\indexname }
(hyperref) reference {section*.2}
(hyperref) toclevel {0}
(hyperref) type {toc}.
(D:\Travail\Bugtex\bug.ind
Package hyperref Info: Reference (link) `page.1' on input line 3.
Package hyperref Info: Anchor `page.3' on input line 5.
[3
Package `hyperref' Info: End of reference (link) `page.1', line 3.
])
Package hyperref Info: Anchor `page.4' on input line 28.
[4
]
Package hyperref Info: Anchor `chapter.1' on input line 28.
Chapter 1.
Package hyperref Info: bookmark on input line 28:
(hyperref) thecounter {1}
(hyperref) text {\protect \numberline {\thechapter }foo}
(hyperref) reference {chapter.1}
(hyperref) toclevel {0}
(hyperref) type {toc}.
Package atveryend Info: Empty hook `BeforeClearDocument' on input line 29.
Package hyperref Info: Anchor `page.5' on input line 29.
Best Answer
Anchor for the index title that uses
\twocolumn
In the meanwhile the trouble (order of
\phantomsection
and\addcontentsline
) with MWE is fixed and we can look at the real problem:suffers from a side effect of the optional argument of
\twocolumn
. The title in the optional argument is put at the top of page in one-column mode, but the current contents of the page (anchor setting) is stalled and is added after the title in two-column mode.Therefore the internals of
\begin{theindex}
needs patching to get the anchor at the right place:Analysis of old MWE
Analysis part is kept, because its method remains useful for debugging similar problems.
With the old MWE of the question I get a link that is two pages early.
The following MWE just adds
\listfiles
and optionverbose
tohyperref
and uses one empty line instead of two:Now the
.log
file contains after running two times withpdflatex
:Analysis:
Doc-Start
is set automatically byhyperref
at the begin of the document.section*.1
is caused by the first\phantomsection
before\tableofcontents
chapter*.2
is set by\chapter*{\contentsname...}
. BTW, this anchor can be used instead of\phantomsection
, see below.Links in the table of contents:
section*.1
: first\phantomsection
before\tableofcontents
chapter*.2
:\tableofcontents
chapter.1
:\chapter{foo}
Output of page 1 with anchor
page.1
.Output of page 2 with anchor
page.2
.The bookmark for the index is using
chapter*.2
, the link generated by\thetableofcontents
.Anchor
section*.3
is caused by\phantomsection
after\addcontentsline
for index.page.1
, the page location of\index{foo}
.page.3
.Versions:
book
: 2007/10/19 v1.4hhyperref
: 20112/11/06 v6.83mmakeidx
: 2000/03/29 v1.0mSummary:
Both the bookmark and the entry in the table of contents for the index points to the table of contents on page 1 instead to the index on page 3. This is fixed by the correct order:
Link for the table of contents
The previous analysis has shown, that the first
\phantomsection
is not needed for the table of contents:Because of the indirection (nested
\add(to)contents(line)
), an additional LaTeX run is needed. The trick is that\addcontentsline
has to go between the title of the table of contents and at the end of its first page.