Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Why do neither \hyperlink nor \hyperref (either explicitly or from the TOC) not go to the page top for Chapter 1, but they both do for Chapter 2 in the below code?

\documentclass{book}
\usepackage{hyperref}


\begin{document}

\tableofcontents

\chapter{Chapter 1}%
\hypertarget{chapter1}{}%
\label{chapter1}%

Go \hyperlink{chapter1}{directly to chapter 1 via hyperlink}.

Go \hyperref[chapter1]{directly to chapter 1 via hyperref}.

\bigskip

Go directly to \hyperlink{chapter2}{chapter 2 from Chapter 1 via hyperlink}.

Go directly to \hyperref[chapter2]{chapter 2 from Chapter 1 via hyperref}.


\chapter{Chapter 2}%
\hypertarget{chapter2}{}%
\label{chapter2}

This is chapter 2.

Go \hyperlink{chapter1}{directly to chapter 1 via hyperlink}.

Go \hyperref[chapter1]{directly to chapter 1 via hyperref}.

\bigskip

Go directly to \hyperlink{chapter2}{chapter 2 from Chapter 2 via hyperlink}.

Go directly to \hyperref[chapter2]{chapter 2 from Chapter 2 via hyperref}.


\end{document}
share|improve this question
3  
Works as expected for me (when compiled with pdflatex and a current hyperref). –  Ulrike Fischer Mar 6 '13 at 7:55
    
I think it's questionable practice to use the same string for the arguments of both \label and \hypertarget. You might get away with it if the commands occur together, but its going to he confusing, to say the least, if they don't. –  Mico Mar 6 '13 at 11:19
    
Ulrike, what does as expected'' mean? Mico, I agree, for documentation purposes, anyway, as it doesn't seem to affect the behavior. Heiko, I understand where the anchors are supposed to be set. I assume your expected behavior'' is \hypertarget pointing to somewhere above the chapter title (but not the top of the page) and \hyperlink pointing to the bottom of whatever line on which the corresponding \hypertarget is. However, as I wrote, I get different behavior for Chapter 1 vs. Chapter 2. I am using an updated version of hyperref. I tested with latex, pdflatex, xelatex, and luatex. I viewe –  user27113 Mar 9 '13 at 17:22
1  
It looks like you've got two separate accounts, which means you cannot edit your original post or leave comments. The StackExchange staff can merge them together for you. –  Werner Mar 9 '13 at 18:36

1 Answer 1

\chapter{Chapter 1}%
\hypertarget{chapter1}{}%
\label{chapter1}%

\chapter automatically sets an anchor. This anchor is used for:

  • the entry in the table of contents,
  • the bookmarks, and
  • the following \label that is linked via \hyperref[...].

Therefore all these links point to the top of the chapter title.

On the other hand \hypertarget sets a new anchor, where it is called: after the chapter title. Therefore the \hyperlink addresses a location below the chapter title. Since there is not a page break in between the chapter title and \hypertarget, the location is still on the same page.

Quoting Ulrike Fischer: "Works as expected for me" also holds for me, tested with pdflatex, xelatex, latex/dvips/ps2pdf. Do you use a different driver? Package hyperref is uptodate? Perhaps you can test a different PDF viewer (if PDF is the output format)?

Anchor positions

Running pdflatex, the anchor positions (horizontal and vertical positions) of the PDF file:

chapter.1: 106.869 668.127
chapter.2: 106.869 668.127
chapter1:  106.869 488.961
chapter2:  106.869 488.961

Thus the corresponding anchors for the first and second chapter have the exact same positions on the page. If they differ for your PDF file, then put the file somewhere for downloading.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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