5

I've seen a pdf LaTeX document where the page numbers at the bottom of the page are hyperref links, and clicking them causes you to jump to the contents table. I don't have the tex file and couldn't work out how it's done from the hyperref package. Can anyone help?

NakedBrunch
  • 48,713
  • 13
  • 73
  • 98
qaz
  • 167
  • 2
  • 6

3 Answers3

2

You could set an anchor at the toc and redefine \thepage to link to it. Here's an example:

\documentclass{report}
\usepackage[colorlinks]{hyperref}
\renewcommand*{\contentsname}{\hyperlink{contents}{Contents}}
\renewcommand*{\thepage}{\hyperref[contents]{\arabic{page}}}
\begin{document}
\tableofcontents
\chapter{One}
Text
\end{document}

If you use babel and wish to redefine \contentsname, use the \addto command of babel or redefine \contentsname after \begin{document}.

Stefan
  • 3,550
  • 1
  • 18
  • 18
  • 1
    Almost worked. The page numbers are links as I wanted, but unfortunately the linking mechanism fails, and ends up linking to the first page instead of the anchor on the contents page. It says 'contents' is undefined. – qaz Aug 03 '10 at 13:51
  • If you replace [contents] with a label to an equation it works. However the command \hyperref[label]{text} does not work when label is a reference to an anchor. – qaz Aug 03 '10 at 14:15
  • I haven't explicitly added the babel package. I moved the renewcommands to after the \begin{document} command anyway. I still get the same problem. – qaz Aug 04 '10 at 20:22
  • I get a bunch of warnings "name{contents} has been referenced but does not exist" and "Token not allowed in a PDF string" (I'm using Roman numerals for the first few pages and I think that might screw things up). Any thoughts? – gghuffer Sep 10 '15 at 22:03
  • @gghuffer Thoughts, sure. We could it also discuss in a [LaTeX forum](http://latex-community.org/forum/) I maintain, since comments here are not suitable for discussion. – Stefan Sep 10 '15 at 22:41
0

Have you tried defining the page numbering using this?

\pagestyle{myheadings}
\markright{ ... }

where \markright specifies the page number with a link to the content page.

Sentry
  • 4,102
  • 2
  • 30
  • 38
0

Here is how I did it

% !TEX encoding = UTF-8 Unicode
% !TEX TS-program = xelatex
\documentclass[UTF8, english]{article}
\usepackage{lipsum} %% produce dummy texts

\usepackage{hyperref}
\usepackage[pagestyles]{titlesec}

\newpagestyle{article}{
\setfoot
%% even pages
[]
[\footnotesize \hyperlink{toc}{\thepage}]
[]
%% odd pages
{}
{\footnotesize \hyperlink{toc}{\thepage}}
{}
}

\begin{document}

\title{example}
\date{}
\author{author}

\maketitle

\pagenumbering{roman}
\setcounter{tocdepth}{2}
\addtocontents{toc}{\protect\hypertarget{toc}{}}
\tableofcontents
\newpage
\pagenumbering{arabic}

\pagestyle{article}

\section{A}
\lipsum[1]
\subsection{a}
\lipsum[2]
\subsection{b}
\lipsum[3]
\subsection{c}
\lipsum[4]
\section{B}
\lipsum[5]
\subsection{d}
\lipsum[6]
\subsection{e}
\lipsum[7]
\section{C}
\lipsum[8]
\subsection{f}
\lipsum[9]
\subsection{g}
\lipsum[10]
\subsection{h}
\lipsum[11]

\end{document}

you can of course customize the link text back to table of contents however you like in the preamble, please read documentation of titlesec for more details.

zyy
  • 1,271
  • 15
  • 25