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

embert
  • 293

1 Answers1

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}
RicoRally
  • 1,252
  • 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 Nov 14 '14 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 Nov 14 '14 at 11:21
  • Would be an option of course. What's the \break doing? – embert Nov 14 '14 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 Nov 14 '14 at 11:33
  • Ah okay. I tried it, but as expected, it produces unsuited line breaks, stretching white spaces in the previous line. – embert Nov 14 '14 at 11:45
  • Understand. However, as I do not know a priori where line breaks will be set and cannot check after each change this kinda wont work. – embert Nov 14 '14 at 12:24
  • agreed, so don't use \break. anyway who is possibly reading it with such zoom? putting \phantomsection\label{} before the labelled word will solve all other problems. – RicoRally Nov 14 '14 at 12:28
  • I just noticed, why it often does make a difference: Often one (or at least I do) reads not fitting the page but the text, so that the text borders are matched with the pdf viewer borders. So in case the phantom section starts not at the beginning of the line the page is shifted to the left up to a maximum of the page margin (white space between text border and page border) on the right hand side. – embert Nov 25 '14 at 11:22
  • I see and agree. To avoid this right shift we should find a way to operate (insert the commands) when TeX breaks paragraphs into lines, which I can't do. But I can insert correct line break manually, before paragraph split happens, seeing the previous results of TeX's algorithms. I can help you in that with some examples if I had a piece of code. – RicoRally Nov 26 '14 at 10:20