3

In a document, which uses text links heavily I have the problem, that the target are always out of the view of the pdf viewer, when jumping there and I could not find a proper solution.

Preferably the jump should land in such a way, that the view is limited by the top line of the line where the target is in and the left border should match the left text border (or even leave a little white space). See the images below. This behaviour should be independent of the current zooming state, the viewer is in (while acknowledging that left border matching is not possible if the zoom is at a low value).

Is it possible somehow?

For instance taking this MWE

\documentclass[a4paper]{report}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[hidelinks]{hyperref}

\begin{document}

\chapter{First chapter}
Get to know more about what is a \hyperlink{frog}{frog}.

\chapter{Second chapter}
Did you know, that a \hypertarget{frog}{frog} is an animal?

\end{document}

Is

enter image description here

should

enter image description here

1 Answer 1

3

Just try this \phantomsection. This is old style ref/label format using hyperref, and works.

\documentclass[a4paper]{report}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[hidelinks]{hyperref}

\begin{document}

\chapter{First chapter}
Get to know more about what is a \hyperref[frog]{frog}.

\chapter{Second chapter}
Did you know, that a frog\phantomsection\label{frog} is an animal?

\end{document}

Then you could also wrap everything into commands.

\newcommand{\myref}[1]{\hyperref[#1]{#1}}
\newcommand{\mylabel}[1]{\phantomsection\label{#1}}

Thus becoming:

\documentclass[a4paper]{report}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[hidelinks]{hyperref}

\newcommand{\myref}[1]{\hyperref[#1]{#1}}
\newcommand{\mylabel}[1]{\phantomsection\label{#1}}

\begin{document}

\chapter{First chapter}
Get to know more about what is a \myref{frog}.

\chapter{Second chapter}
Did you know, that a frog\mylabel{frog} is an animal?

\end{document}
9
  • This basically works, rico. It seems to place the anchor in the way that the \phantomsection tag is in the upper left corner, thus if the text being labelled comes behind the tag, it will be in view (note that this behaviour can only observed with a sufficiently high zooming). There is no way, to force the anchor being set at the left text border, is there? Anyway, because zooming the text borders out of view is uncommon, this solution will do it I think.
    – embert
    Commented Nov 14, 2014 at 10:50
  • Yes there is, you should put it at the beginning of the line. Before the first word you write \break\phantomsection\label{}. It is stupid but I don't know if there is actually a method to do what you want.
    – RicoRally
    Commented Nov 14, 2014 at 11:21
  • Would be an option of course. What's the \break doing?
    – embert
    Commented Nov 14, 2014 at 11:31
  • it's a manual line break, preventing \phantomsection to stay in the previous line. If that is your first line of the paragraph you don't need it.
    – RicoRally
    Commented Nov 14, 2014 at 11:33
  • Ah okay. I tried it, but as expected, it produces unsuited line breaks, stretching white spaces in the previous line.
    – embert
    Commented Nov 14, 2014 at 11:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .