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.

I made a small example:

\documentclass[a4paper,12pt]{article}
\usepackage[pdfstartview=FitH]{hyperref}
\usepackage{bookmark}
\begin{document}
we want to go\hypertarget{t1}{} here.\\
instead we go here.
\bookmark[dest=t1]{goto 1}
\end{document}

When clicking the bookmark in the PDF file, the second line will be at the top of the screen, but the first one should. What do I understand wrongly? How do I have to do it?

edit: my summary of the answers (i feel that \Hy@raisedlink looks a bit better since the other one is really close to the top of the line). the problem is: this doesnt seem to work in macros. i always get an 'undefined control sequence' error:

\documentclass[a4paper,12pt]{article}
\usepackage[pdfstartview=FitH]{hyperref}
\usepackage{bookmark}
\newcommand{\nop}{\makeatletter\Hy@raisedlink{\hypertarget{t4}{}}\makeatother}
\begin{document}
apparently \textbackslash hypertarget marks the bottom of its line instead
of the top. it can be fixed using \textbackslash Hy@raisedlink or 
\textbackslash raisebox (a bit lower).\\\\
we cant go\hypertarget{t1}{} here.\\\\
we can go here\makeatletter\Hy@raisedlink{\hypertarget{t2}{}}\makeatother{}. %
we can go here\raisebox{\ht\strutbox}{\hypertarget{t3}{}} too.\\\\
go here by macro: \nop .
\bookmark[dest=t1]{cant} 
\bookmark[dest=t2]{can} 
\bookmark[dest=t3]{can too}
\bookmark[dest=t4]{macro}
\end{document}

edit 2: Ok, it works now. I have noticed that \Hy@raisedlink seems to pretend that the link was in the line above (e.g. it can go to the end of a page if the marked place is at the beginning of the next) while \raisebox goes to the top of the current text-line (thereby sometimes cutting big formulas).

share|improve this question
1  
\makeatletter is somewhat tricky. Basically, you should define your macro like \makeatletter\newcommand{nop}{\Hy@raisedlink{...}}\makeatother. Think of the \makeatletter...\makeatother pair as an environment in which it is acceptable to use @ in macros. –  Ryan Reich May 3 '11 at 18:01
    
Interestingly, because of the way TeX parses its input, even if you have a macro (such as \nop) whose contents contain @'s in other macros, it is usable anywhere, as long as its own name has only letters. –  Ryan Reich May 3 '11 at 18:10

3 Answers 3

up vote 14 down vote accepted

This was my very first question here: Hyperlinks to a bibliography are one line off. The reason is that hyperref links, like all TeX boxes, are placed on the baseline. There is an internal command \Hy@raisedlink which does more or less what Bruno's answer describes. Here is an example of its usage both inside and outside macros:

\documentclass{article}
\usepackage{hyperref}

\makeatletter
 \newcommand{\nop}[1]{\Hy@raisedlink{\hypertarget{#1}{}}}
\makeatother

\begin{document}

 Here are targets created
 \makeatletter
  \Hy@raisedlink{\hypertarget{t1}{}}directly
 \makeatother
 and \nop{t2}indirectly.  Here is one created by \hypertarget{t3}{}hypertarget.

 \hyperlink{t1}{Here} \hyperlink{t2}{are} \hyperlink{t3}{links}

\end{document}

The first two links should point above their targets, and the third one should point to its baseline.

share|improve this answer
    
I dont understand. Is \Hy@raisedlink supposed to replace \hyperref or \bookmark? It sounds like the latter but I didnt find any examples. –  peter May 2 '11 at 15:50
1  
@peter: No, it's a modifier. You use it like this: \makeatletter \Hy@raisedlink{\hypertarget{target}{}} \makeatother, and the link is raised like in Bruno's answer. However, the second argument of \hypertarget should be empty (also like in Bruno's answer), or else the contents will also be raised. Basically, don't use this command, but do what Bruno did. However, some hyperref macros, such as \phantomsection, do call it internally, and produce correctly raised links. –  Ryan Reich May 2 '11 at 16:59
    
thanks. it's in the \hyperref package, right? because when I use it I get undefined control sequence errors whereever my macro containing it appears. also @raisedlink appears in the output file. –  peter May 3 '11 at 6:26
    
@peter: Yes, you have to load hyperref (the Hy@ prefix is its personal namespace). Also, if you use it within a document, you have to wrap the whole expression in \makeatletter...\makeatother like I did, so that @ will be considered part of the macro name. If you are getting @raisedlink in the output, it sounds like this is the reason. –  Ryan Reich May 3 '11 at 14:12
    
i put it in a minimal example, see edited question. it works if i insert it directly, but it doesnt if i put it inside a macro (which is unfortunately my intended use). i'd like it if you could have a look at that. –  peter May 3 '11 at 17:53

I remember having the same problem (and failing to get a good solution). A clunky way is to raise the target by hand with \raisebox (from the graphicx package, I believe).

\documentclass{article}
\usepackage{hyperref}
\usepackage{graphicx}
\begin{document}
If we try to go \hypertarget{t1}{here}, this fails 
(it bring us at the bottom of the word ``here''), 
as you can \hyperlink{t1}{check}.

If we use some dirty code to raise the 
target\raisebox{\ht\strutbox}{\hypertarget{t2}{}},  %% 
then we will go to the right place. A \verb|\strut|
is a zero-width object whose height above the baseline
and depth below the baseline are such that it spans
the line completely from top to bottom. Then \verb|\ht\strutbox|
gives the height of such a \verb|\strut|, and our
\hyperlink{t2}{link} points to the right place.
\end{document}
share|improve this answer
    
I had heard about such hacks, thanks for this concrete example. I am using it now and it works mostly. I still think I am missing something here (or is it really the purpose of \hypertarget to mark the bottom of it's target?), so I will still wait a bit until I accept this as answer (which will, I assume, discourage others from answering more). –  peter May 1 '11 at 14:29
2  
@peter: I don't think that you should accept this answer, even after some time: there must definitely be a better answer, and I only posted this to solve your problem in the short term (I'd be happy myself to know a better way). –  Bruno Le Floch May 1 '11 at 14:34

Sorry to violate guidelines, but I don't yet have enough reputation to comment. The problem that \Hy@raisedlink may wind up at the end of the previous page seems significant to me. The problem that \raisebox looking bad because it is too close to the top of the line can be solved by using a coefficient: \raisebox{1.2\ht\strutbox} looks pretty good to my eye.

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.