0

I'm making a custom document without any chapters, sections, etc. But I also want a table of contents in my document, with entries that I define. So far, I have used

\phantomsection
\addcontentsline{toc}{section}{My entry}

and this creates an entry within my table of contents (\tableofcontents). But when I click one of these entries, the jump takes me to the exact location in the document where this phantom section was defined. Instead I want jumps that takes me to the top of that page. Ideally, the page should be jumped to and fit to screen (zoom out, typically) when I click the entry.

How can I add entries to the table of contents that jumps to the top of the page I want an entry for?

2

This will take you to the page anchor.

\documentclass{article}
\usepackage{hyperref}

\makeatletter
\newcommand{\phantompageref}{\def\@currentHref{page.\@the@H@page}}
\makeatother

\begin{document}
\tableofcontents
\newpage
\rule{1pt}{.4\textheight}

\phantompageref
\addcontentsline{toc}{section}{My entry}

\rule{2pt}{.4\textheight}
\end{document}

This links to the very top of the page.

\documentclass{article}
\usepackage{everypage}
\usepackage{hyperref}

\AddEverypageHook{\raisebox{1in}[0pt][0pt]{\hypertarget{pagetop.\thepage}{}}}

\makeatletter
\newcommand{\phantompageref}{\def\@currentHref{pagetop.\thepage}}
\makeatother

\begin{document}
\tableofcontents

\newpage
\rule{1pt}{.4\textheight}

\phantompageref
\addcontentsline{toc}{section}{My entry}

\rule{2pt}{.4\textheight}
\end{document}
  • This takes me to a top of my document, not on the page I have \phantompageref together with \addcontentsline. And it will not jump to the page border of my document, instead it jumps to the top of my header on this page (refer to the line which 5 points to in this figure: en.wikibooks.org/wiki/LaTeX/Page_Layout#Page_dimensions). The example above will not jump at all. – telephone Jan 28 '17 at 11:46
  • I remembered that hyperref had its own page counter, but didn't realize it had more than one. – John Kormylo Jan 28 '17 at 14:45
  • Thanks! But it will not compile because of "Undefined control sequence": pagetop.\@the@H@page. Looks like we are closer, though :) – telephone Jan 28 '17 at 14:51
  • Maybe this is something to take usage of: tex.stackexchange.com/a/55620/123984 – telephone Jan 28 '17 at 14:52
  • I must be forgetting to run these things 3 times. – John Kormylo Jan 28 '17 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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