# Hyperref link placement: alignment to left page border and top of page

(a) Is is possible to have certain hyperref-hyperlinks (such as those to sections and also (when using memoir) to the table of contents, lists of figures and tables, and bibliography) point to the very left border of the page, not to the interior? Note that I have already tried the hypcap package, which positions figure and table links to the left edge of the text (just where the left margin ends) but not to the left page border. (For reading text, it is good if the text starts not exactly at the edge of one's viewing window but a tad into the window's border.) Also, hypcap doesn't deal with the table of contents, the lists of figures/tables, and the bibliography.

(b) If, in addition, one could make hyperlinks point not only to the left page border but also to the top left corner of the respective page, that would be a useful option. Is that possible?

If people would like starter code for this, here is some I've slightly adapted for another, related question:

\documentclass{memoir}
% using the memoir class here is necessary
% for all the desired hyperlinks to be created
\usepackage{hyperref}
\usepackage[all]{hypcap} % hypcap doesn't help here

\begin{document}

\tableofcontents

\section{A section}
This is the only section.

\listoftables
\listoffigures

\begin{thebibliography}{9}
\end{thebibliography}

\end{document}


Note: I am aware of the hypcap package but not sure whether/how it can be used to address this.

• The behavior you seek in question (b) is available automatically whenever you're viewing the page in full-page view. – Mico Jun 11 '12 at 0:54
• Yes. Except giving users a finer-grained choice would be nice. – Lover of Structure Jun 12 '12 at 1:02
• @LoverofStructure 2018 4Q I've devloped a solution with eso-pic (2015) should this still be of interest (since the typesetter has no real control (or desire to control) how the reader views the .pdf). – Saphar Koshet Nov 10 '18 at 15:05

user14996 wrote:

(a) Is is possible to have certain hyperref-hyperlinks [...] point to the very left border of the page, not to the interior?

and

(b) If, in addition, one could make hyperlinks point not only to the left page border but also to the top left corner of the respective page, that would be a useful option. [...]

First I agree with the remark of canaaerus:

Just imagine the user has chosen a high zoom level and you force a jump to the top-left of the page. That point might be very far from the referenced position and the user wont be able to see it.

An alternative answer to the questions: The PDF format also supports other "view" settings for destinations. The default in hyperref is XYZ, X and Y means the anchor position and Z the zoom level (with default null meaning unchanged zoom level). If you want to get the left page border and do not mind also getting the right page border, then you can use

\hyperref{pdfview=FitH}% or FitBH that removes some white space


(Drivers hpdftex and other calculate the Y position from the current position of the anchor.)

You get the whole page with

\hyperref{pdfview=Fit}


But be aware that the user might be annoyed by having to switch the zoom level, if he cannot read the text because of a small window/screen.

• Ah welcome to TeX.SE your highness! :) – percusse Jul 26 '12 at 21:00
• @canaaerus Thanks for your contribution; Heiko: what an honor to be one of the first to get an answer from you here ;-) – Lover of Structure Jul 26 '12 at 23:39
• @canaaerus I appreciate your opinion on this. Just now I was reminded why I have a differing intuitive preference: (1) When I click on a document-internal link, the text is often immediately adjacent to the left margin in my viewer. Every time I need to move the document a bit to the right to make reading more comfortable. As you said, maybe it's the viewer's problem, but it's still a problem. (2) When I click on a link for a chapter, my viewer jumps to a place just above the chapter heading. For me however the chapter starts at the top of the page. (Yes, one can defend either choice.) – Lover of Structure Jan 13 '13 at 12:59
• @HeikoOberdiek Just in case this might give rise to an feature addition to hyperref, here is pointer to my immediately preceding comment :-) If you've already seen this and deem it not necessary, no problem. (Feel free to mark this comment of mine as obsolete once you've seen it.) – Lover of Structure Feb 19 '13 at 10:05

In my, admittedly simplistic, world view, something like you request should not be necessary. The document should point its links to the actual target position and the viewer application can then make a sensible choice of where to jump. That choice will also depend on the current view mode and zoom level.
Just imagine the user has chosen a high zoom level and you force a jump to the top-left of the page. That point might be very far from the referenced position and the user wont be able to see it.

Now the idea to do it anyway, is to put the label mark at the top left. I got until this point:

\documentclass{memoir}

\usepackage{lipsum}

\usepackage[absolute]{textpos}
\setlength\TPHorizModule{1cm}
\setlength\TPVertModule{1cm}
\textblockorigin{0mm}{0mm}

\newcounter{lbl}
\def\plabel#1{\begin{textblock}{0}(0,0)\refstepcounter{lbl}\label{#1}\end{textblock}}

\begin{document}
\chapter{The first chapter}
\lipsum[1-3]
\section{A section}
Here a label is set!\plabel{lbl1}

\chapter{The second chapter}
\end{document}


First problem is, that the textblock is always at the top left corner of the first page. So there you need to use some other method of absolute positioning. (I bet there are a dozen ways to do that)

Second thing that still needs fixing, is that now the label references the lbl counter. (I think this is similar to the hypcap packages approach.) So you need to fix the reference.

\def\thelbl{\thesection}


would for example help. But maybe you don't only want to reference sections.

(a) Yes.

(b) Yes.

To set the link target position at the top of a page, this example applies the eso-pic package (2015 version) (TeXLive 2016) to position a zero-sized graphic to which graphic’s location is attached the hyperref link. For the graphics, this is compiled with xelatex. (When compiling any of the options, even if the compiler's notes don't indicate a need to compile more than once, it's OK to compile more than once to get the cross-references settled.)

This example illustrates that setting the top-of-page links may/can use custom vertical adjustments, given that the adjustment needed to position the zero-sized graphic depends on how far down the page the first item on the page is. The linking is done “manually” using the \phantomsection command. This code uses four variables, as documented in the code, below.

The output, not included, is 24 pages. Of course, to see all the features, one will want to scroll the whole document. Since the main focus is on the placement of the link targets (to confirm that each link really is at the top of the page while in scroll mode, including the link to/for the ToC page, itself), testing is facilitated by including the Bookmarks view and by clicking “Contents” (the topmost bookmark) to return directly to the ToC page (to test the next link).

While it’s expected that the List of Figures would start on a new page, it’s deduced from the MWE that the List of Tables isn’t necessarily also expected or wanted at the top of another new page. Thus, here, those respective links point to the top of the same page.

This example uses nextpage to illustrate handling of the pages between the chapters. Several example chapters are used so as to illustrate each of the possible \clearto situations. Whether one wants blank pages or filled pages (This Page Intentionally Blank), page numbers or no page numbers, those options are illustrated. Several options are retained as commented lines for use as additional customization examples.

\documentclass{memoir}

\usepackage[latin,english]{babel}
\usepackage{csquotes} % smart quotes

\renewcommand{\chaptermark}[1]{\markboth{#1}{#1}}

%https://tex.stackexchange.com/a/74096/170846
{\footruleskip}

%https://tex.stackexchange.com/a/40552/170846 slightly modified

\copypagestyle{plainnotice}{plain}
\makeevenfoot{plainnotice}{\thepage}{}{}% not used with "openright"
\makeoddfoot{plainnotice}{}{}{\thepage}
\aliaspagestyle{chapter}{plainnotice}

\usepackage{nextpage}

% minor modification of nextpage for even-to-even clearing
\makeatletter
\renewcommand{\cleartoevenpage}[2]{%
\ifodd\c@page\clearpage%
\else\newpage\thispagestyle{#2}\hbox{}#1
\newpage\hbox{}
\fi}
\makeatother

\usepackage[texcoord]{eso-pic}
\usepackage{graphicx}
\usepackage{url}
\usepackage{lipsum}
\usepackage{tocloft}
\usepackage[pagebackref=true]{hyperref}

\hypersetup{
filecolor=blue,
urlcolor=blue,
}

\AtPageUpperLeft{
\begin{picture}(0,0)
\put(0,260){\phantomsection
\end{picture}
}
}

\makeatletter
\renewcommand{\@memb@bchap}{%
\chapter*{\bibname}%
\bibmark
\ifnobibintoc\else
\AtPageUpperLeft{
\begin{picture}(0,0)
\put(0,260){\phantomsection
\end{picture}
}
\fi
\prebibhook}
\makeatother

\AtPageUpperLeft{
\begin{picture}(0,0)
\addcontentsline{#1}{#2}{#3}}% #1 which "list" (e.g., toc)
%     #2 which "level" (e.g., chap.)
%   #3 "title"
\end{picture}
}
}

\setcounter{tocdepth}{1} % -1, 0, 1

\begin{document}
\AtPageUpperLeft{
\begin{picture}(0,0)
\put(0,144){\pdfbookmark[0]{Contents}{tblcntnts}}
\end{picture}
}

\tableofcontents*

% odd to odd -- blank
\cleartooddpage[\thispagestyle{empty}]%  <--- square

% odd to odd -- blank with centered pg no
%\cleartooddpage[\thispagestyle{plain}]

% odd to odd -- blank with Chap pg no
%\cleartooddpage[\thispagestyle{plainnotice}]

\chapter*{Chapter 1}
\chaptermark{Chapter 1}

\lipsum[44-48]

\lipsum[64-69]

% odd to odd -- skipped pg filled (applying Chap footer)

% odd to odd -- skipped pg filled (no page number)

% odd to odd -- skipped pg filled (centered page number)

% odd to odd -- just Chap footer
%\cleartooddpage[\thispagestyle{plainnotice}]

% odd to odd -- just centered page number
%\cleartooddpage[\thispagestyle{plain}]

\chapter*{Chapter 2}
\chaptermark{Chapter 2}

\lipsum[49-53]

% even to odd -- blank    and
% even to odd -- filled
% are the same -- there is no blank page to handle

\chapter*{Chapter 3}
\chaptermark{Chapter 3}

\lipsum[54-57]

% odd to even -- blank    and
% odd to even -- filled
% are the same -- there is no blank page to handle
\cleartoevenpage{some text that won't ever show up}{plainnotice}

% for no page number
%\thispagestyle{empty}
% for Chap 1st page page number
\thispagestyle{plainnotice}
This is the recto page advanced to.  Typically, there'll be pre-chapter text or graphics on this page.

\chapter*{Chapter 4}
\chaptermark{Chapter 4}

\lipsum[58-63]

% even to even -- blank
\cleartoevenpage{}{empty}

\thispagestyle{plainnotice}

\begin{minipage}[c]{\textwidth}
\href{https://ctan.org/lion/files/ctan_lion_350x350.png}{\includegraphics[scale=0.75]{ctan_lion_350x350.png}{}}\\
\end{minipage}

\chapter*{Chapter 5}
\chaptermark{Chapter 5}

\lipsum[59-63]

% even to even -- skipped pg filled (applying Chap footer)

% even to even -- skipped pg filled (centered page number)

% even to even -- skipped pg filled (no page number)

% even to even -- skipped pg w/ defined header/footer only

% even to even -- skipped pg w/ Chap footer only
%\cleartoevenpage{}{plainnotice}

% even to even -- skipped pg w/ centered page number only
%\cleartoevenpage{}{plain}

\setPgLink{toc}{chapter}{The \enquote{cleared-to} verso page, ready for pre-Chap content, with preceding Notice of deliberate blankness}{140}
\thispagestyle{plainnotice}
%\thispagestyle{plain}
%\thispagestyle{empty}

Here is a famous quote by a famous author.

{\textit{Famous Author}}

\chapter*{Chapter 6}
\chaptermark{Chapter 6}

\lipsum[70-73]

\cleartooddpage{}