2

I tried the solution in How do you make a reference from all page numbers to the ToC? to add a link to Table of Contents at each page number. The solution added the links at the page numbers, but directs to the first page of the document, not the Table of Contents. The code I have used:

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\fancyhead[CO]{\hyperref[toc-contents]{\thepage}}

\usepackage{blindtext}

\begin{document} \frontmatter Page 1 \newpage Page 2 \newpage

\label{toc-contents} \tableofcontents

\mainmatter \blinddocument

\end{document}

Any help?

Stefani
  • 21

1 Answers1

0

To direct all the links to the TOC attach the label to its title.

It is also necessary to redefine the plain style, used on the first page of the chapters.

Notes.

(1) The solution proposed in how-do-you-make-a-reference-from-all-page-numbers-to-the-toc is not correct. It only works because it puts the TOC as the first page of the document, as you already discover.

(2) To test this solution put \tableofcontents just before \end{document} and compile twice. Now all links will send you to the page #5 (the last).

b

a

\documentclass{book}
\usepackage[utf8]{inputenc}

% ***********************************************************
\usepackage{fancyhdr}
\fancyhf{} \renewcommand{\headrulewidth}{0pt} \fancyhead[C]{\hyperref[toc-contents]\thepage} % all pages <<<

\fancypagestyle{plain}{%redefine \pagestyle{plain} to add the link <<< \fancyhf{} \fancyfoot[C]{\hyperref[toc-contents]\thepage} \renewcommand{\headrulewidth}{0pt} \renewcommand{\footrulewidth}{0pt} }%

\pagestyle{fancy} \renewcommand{\contentsname}{Some title\label{toc-contents}} % label in TOC title<<<< % ***********************************************************

\usepackage{blindtext}

\usepackage{hyperref} % last to load

\begin{document} \frontmatter Page 1 \newpage Page 2 \newpage

\tableofcontents    
\mainmatter
\blinddocument

\end{document}

Simon Dispa
  • 39,141
  • Thanks for the reply. I suppose i have to maintain the \label{toc-contents} before \tableofcontents. Still, the links direct to the first page. – Stefani Jun 13 '21 at 13:51
  • @Stefani Did you try the code of my answer? The link is to the title of TOC. Never goes to the first page. – Simon Dispa Jun 13 '21 at 14:21
  • I added your code into the doc template I have to use. The template has many other latex codes, maybe one of those is interfering. – Stefani Jun 13 '21 at 18:17
  • @Stefani To test put \tableofcontents before \end{document} and compile twice. All links will send you to the last page (#5). – Simon Dispa Jun 13 '21 at 18:23
  • @Stefani Remember to put hyperref as the last package to load. – Simon Dispa Jun 13 '21 at 18:28