# Making each page number link back to beginning of chapter or section

I was wondering if it is possible to have the page numbers that appear in the footer of each page link back to the beginning of the section or chapter in which they appear.

\usepackage{fancyhdr}
\fancypagestyle{plain}{% % <-- this is new
\fancyhf{}
\fancyfoot[CO]{\hyperref[sec:toc]{\thepage}}


...that I found here: Here

I've tried using \thechapter and the \thesection as the hyperlink target but it does not work, even though I accomplished something similar in another question of mine: Question

Here I have every group letter of a glossary list link back to the start of the glossary using \thechapter as a hyperlink target:

...

{
\begin{center}
\vspace{25pt}
\glsnavhypertarget{##1}{}
\vspace{25pt}
\end{center}
\needspace{6\baselineskip}\item[]\makebox[-85pt]{\begin{Large}\textbf{\glsgetgrouptitle{\hyperref[\thechapter]{##1}}}\end{Large}}
}
...


I'm guessing it's because \thechapter and \thesection return a chapter/section number. But not sure why it works in my other question and not here. I was thinking it was a fluke but I get a reference error when trying to link the to the beginning of the chapter using page numbers (using fancyhdr) and not when linking to the start of the glossary (renewing the glossary group header command).

Is there a way to generalize the page numbers as links to the current chapter or section?

Any help is appreciated.

Here is a MWE:

\documentclass{report}
\usepackage[margin=1in]{geometry}

\usepackage{setspace}
\doublespacing
\emergencystretch=1em

\usepackage{ragged2e}
\usepackage[english]{babel}
\usepackage{hyperref}
\usepackage{bookmark}

\usepackage{fancyhdr}
\fancypagestyle{plain}{
\fancyhf{}
\fancyfoot[CO]{\hyperref[sec:toc]{\thepage}}

%Quick Filler Text
\usepackage{blindtext}
\usepackage{kantlipsum}

\begin{document}
\tableofcontents
\label{sec:toc}
\pagestyle{plain}

\chapter{Introduction}
\label{intro}
\blindtext[5]

\chapter{Next}
\label{next}
\blindtext[3]
\blinditemize
\blindtext[2]

\section{And Then}
\label{andthen}
\blindtext[6]
\blindenumerate

\chapter{Math}
\label{math}
\blindtext[5]

\section{Some Math}
\label{somemath}
\blindmathpaper

\chapter{Conclusion}
\label{conclusion}
\blindtext{20}
\end{document}

• \thechapter works (probably) in your other question, because you have a \label{\thechapter} which gives a suitable anchor. But your code is not suitable for tests. Create a sensible complete and minimal example without all this \vspace and other formatting commands. – Ulrike Fischer Jun 22 '20 at 7:34
• I was hoping the solution would be a simple "replace sec:toc with \magicmacro". In any case, I've provided a MWE. – ErMo Jun 23 '20 at 11:00
• This method seems to work: link, by placing \theHsection near chapter\section declarations and the \hyperlink code inside the \fancyhdrcode but it takes me to the first word of the chapter/section instead of the title. Nit picky, I know :/ – ErMo Jun 23 '20 at 12:34

\documentclass{report}
\usepackage[margin=1in]{geometry}

\usepackage{setspace}
\doublespacing
\emergencystretch=1em

\usepackage{ragged2e}
\usepackage[english]{babel}
\usepackage{hyperref}
\usepackage{bookmark}

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\fancyfoot[CO]{\rightmark}
\fancypagestyle{plain}{\pagestyle{fancy}}

%Quick Filler Text
\usepackage{blindtext}
\usepackage{kantlipsum}
\makeatletter
\def\chaptermark#1{\markboth
{\MakeUppercase {\ifnum \c@secnumdepth >\m@ne \@chapapp \ \thechapter . \ \fi #1}}

\makeatother
\begin{document}
\tableofcontents
\label{sec:toc}
\pagestyle{plain}

\chapter{Introduction}
\label{intro}
\blindtext[5]

\chapter{Next}
\label{next}
\blindtext[3]
\blinditemize
\blindtext[2]

\section{And Then}
\label{andthen}
\blindtext[6]
\blindenumerate

\chapter{Math}
\label{math}
\blindtext[5]

\section{Some Math}
\label{somemath}
\blindmathpaper

\chapter{Conclusion}
\label{conclusion}
\blindtext{20}
\end{document}

• This seems to work better. The method I was using occasionally breaks if there are no sections or paragraphs. Is there a way to adapt this to unnamed chapters (\chapter*) like TOC/LOF, References,etc? – ErMo Jun 24 '20 at 8:10
• I had to use a combination of the method I posted as an answer and the solution provided by @fischer to get both named and unnamed chapters to work, although I would have rather have found a nice solution like fischer for both unnamed chapters and subsections (and maybe subsubsections). Thanks fischer. – ErMo Jun 24 '20 at 12:43

You might want to add a label to the chapter title and then refer to it via \pageref

...
\chapter{xx}\label{ch:xx}
... further content goes here
Put something like this where you want the page reference: Chapter \nameref{ch:xx}'' is on \pageref{ch:xx} (you only need the pageref-command)


I hope this helps.

• This doesn't work if I try and put \nameref in the fancyhdr code. Get an error when about a missing '}'. It will work anywhere else, but I need them in the page numbers. – ErMo Jun 24 '20 at 7:46

Looks like this works, had to redefine chapter and section formats:

\documentclass{report}
\usepackage[margin=1in]{geometry}

\usepackage{setspace}
\doublespacing
\emergencystretch=1em

\usepackage{ragged2e}
\usepackage[english]{babel}
\usepackage{bookmark}
\usepackage[explicit, noindentafter]{titlesec}
\usepackage{titlesec}

\AtBeginDocument{\def\chapterautorefname{Chapter}}%
\AtBeginDocument{\def\sectionautorefname{Section}}%

%Chapter Title Format
\titleformat{\chapter}[display]{\hypertarget{section::\theHsection}{}\bfseries\centering}{\huge Chapter \thechapter}{1em}{\huge #1}
\titlespacing{\chapter}{0pt}{-32pt}{1cm}

%Section Title Format
\titleformat{\section}[display]{\hypertarget{section::\theHsection}{}\bfseries}{\large\thesection \hspace{6pt} #1}{3pt}{}
\titlespacing{\section}{0pt}{16pt}{-18pt}

\usepackage{fancyhdr}
\fancypagestyle{plain}{
\fancyhf{}

%Quick Filler Text
\usepackage{blindtext}
\usepackage{kantlipsum}

\begin{document}
\tableofcontents
\label{sec:toc}
\pagestyle{plain}

\chapter{Introduction}
\label{intro}
\blindtext[5]

\chapter{Next}
\label{next}
\blindtext[3]
\blinditemize
\blindtext[2]

\section{And Then}
\label{andthen}
\blindtext[6]
\blindenumerate

\chapter{Math}
\label{math}
\blindtext[5]

\section{Some Math}
\label{somemath}
\blindmathpaper

\chapter{Conclusion}
\label{conclusion}
\blindtext{20}
\end{document}


It works on my MWE, will test it later on my thesis and report back.

• there is no need to add more targets - hyperref already adds them. You only need the right code to use them. – Ulrike Fischer Jun 23 '20 at 13:35
• But then how to refer to them in a general sense? Like I am trying to have the page numbers link to the nearest chapter/section, which is my I am using a modified fancyhdr definition. – ErMo Jun 24 '20 at 7:44
• my answer links to the nearest chapter/section. – Ulrike Fischer Jun 24 '20 at 7:57