1

I want to put a link to the next section within my footer. I'm using fancyhdr and hyperref.

The kick is that this is a homework file, so I'm jumping around sections (and between books), so there's no regular pattern to the numbering.

I'm hoping for a "set and forget" solution, that doesn't take any maintainence.

I've included an outline of my document below, with Lipsum text.

Any help would be appreciated.

\documentclass[14pt]{extreport}
%\usepackage[utf8]{inputenc}
%\usepackage{amsmath, amsthm, amsfonts, amssymb}
\usepackage[a4paper, total={6.5in, 8.75in}]{geometry}
%\usepackage[mode=buildnew]{standalone}% requires -shell-escape
%\usepackage[dvipsnames]{xcolor}
%\usepackage{tikz}
%\usepackage{cancel}
\usepackage{lipsum}


\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\rightmark}
\fancyhead[R]{\thepage}
\fancyfoot[C]{\hyperlink{toc}{\small Table of Contents}}
%\fancyfoot[L] LINK TO PREVIOUS SECTION
%\fancyfoot[R] LINK TO NEXT SECTION

\title{Stack Exchange Question}

\usepackage{hyperref}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black,
    pdftitle=STEC-2500-2 Exercises,
    pdfauthor=Neil P,
    bookmarksopen=true
}
\begin{document}
    \maketitle
    \setcounter{tocdepth}{3}%Sets TOC to handle subsubsections (which is where notes and problems are stored.)
    \addtocontents{toc}{\protect\hypertarget{toc}{}} %A link to the toc itself
    \tableofcontents
    \newpage

    \chapter{Even More Techniques on Proofs}
    \setcounter{chapter}{6}
    \setcounter{section}{3}
    \section{test}
    \lipsum[15]
    \newpage

    \setcounter{chapter}{7}
    \setcounter{section}{0}
    \section{test}
    \lipsum[15]
    \newpage
    \section{test}
    \lipsum[15]
    \newpage

    \setcounter{chapter}{1}
    \setcounter{section}{0}
    \section{test}
    \lipsum[15]
    \newpage
    \section{test}
    \lipsum[15]
    \newpage
    \section{test}
    \lipsum[15]
    \newpage

    \setcounter{chapter}{1}
    \chapter{Finite and Infinite Sets}
    \setcounter{chapter}{9}
    \setcounter{section}{0}
    \section{test}
    \lipsum[15]
    \newpage

    \setcounter{chapter}{1}
    \setcounter{section}{5}
    \section{test}
    \lipsum[15]
    \newpage

    \setcounter{chapter}{9}
    \setcounter{section}{1}
    \section{test}
    \lipsum[15]
    \newpage
    \section{test}
    \lipsum[15]
    \newpage


    \setcounter{chapter}{1}
    \setcounter{section}{3}
    \section{test}
    \lipsum[15]
    \newpage
    \section{test}
    \lipsum[15]
    \newpage

    \setcounter{section}{6}
    \section{test}
    \lipsum[15]
    \newpage
    \section{test}
    \lipsum[15]
    \newpage

\end{document}
0

1 Answer 1

1

As requested, here’s a solution that does it completely automatically. You could write \mysection and \mysection* commands if you really hate writing \advancesec.

\documentclass[14pt]{extreport}
%\usepackage[utf8]{inputenc}
%\usepackage{amsmath, amsthm, amsfonts, amssymb}
\usepackage[a4paper, total={6.5in, 8.75in}]{geometry}
%\usepackage[mode=buildnew]{standalone}% requires -shell-escape
%\usepackage[dvipsnames]{xcolor}
%\usepackage{tikz}
%\usepackage{cancel}
\usepackage{lipsum}


\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\rightmark}
\fancyhead[R]{\thepage}
\fancyfoot[C]{\hyperlink{toc}{\small Table of Contents}}
\fancyfoot[L]{\hyperlink{\prevlabel}{\contentsname}}
\fancyfoot[R]{\hyperlink{\nextlabel}{\autoref{\nextlabel}}}

\title{Stack Exchange Question}

\usepackage{hyperref}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black,
    pdftitle=STEC-2500-2 Exercises,
    pdfauthor=Neil P,
    bookmarksopen=true
}

\newcounter{currsec}
\setcounter{currsec}{0}
\newcounter{nextsec}
\setcounter{nextsec}{1}

\newcommand\advancesec{%
\edef\prevlabel{sec:\thecurrsec}%
\addtocounter{currsec}{1}%
\label{sec:\thecurrsec}%
\addtocounter{nextsec}{1}%
\edef\nextlabel{sec:\thenextsec}%
}

\newcommand\prevlink%
{\fancyfoot[L]{\hyperlink{\prevlabel}{\autoref{\prevlabel}}}}
\newcommand\nonextlink{\fancyfoot[R]{}}

\begin{document}
    \maketitle
    \setcounter{tocdepth}{3}%Sets TOC to handle subsubsections (which is where notes and problems are stored.)
    \addtocontents{toc}{\protect\hypertarget{toc}{}} %A link to the toc itself
    \tableofcontents\label{sec:0}
    \newpage

    \chapter{Even More Techniques on Proofs}
    \setcounter{chapter}{6}
    \setcounter{section}{3}
    \section{test}\advancesec
    \lipsum[15]
    \newpage
    
    \setcounter{chapter}{7}
    \setcounter{section}{0}
    \section{test}\advancesec\prevlink
    \lipsum[15]
    \newpage
    
    \section{test}
    \label{sec:7.2}\advancesec
    \lipsum[15]
    \newpage


    \setcounter{chapter}{1}
    \setcounter{section}{0}
    \section{test}\advancesec
    \edef\nextsec{sec:1.2}
    \lipsum[15]
    \newpage
    
    \section{test}\advancesec
    \lipsum[15]
    \newpage
    
    \section{test}\advancesec
    \lipsum[15]
    \newpage

    \setcounter{chapter}{1}
    \chapter{Finite and Infinite Sets}
    \setcounter{chapter}{9}
    \setcounter{section}{0}
    \section{test}\advancesec
    \lipsum[15]
    \newpage

    \setcounter{chapter}{1}
    \setcounter{section}{5}
    \section{test}\advancesec
    \lipsum[15]
    \newpage

    \setcounter{chapter}{9}
    \setcounter{section}{1}
    \section{test}\advancesec
    \lipsum[15]
    \newpage
 
    \section{test}\advancesec
    \lipsum[15]
    \newpage

    \setcounter{chapter}{1}
    \setcounter{section}{3}
    \section{test}\advancesec
    \lipsum[15]
    \newpage
    
    \section{test}\advancesec
    \lipsum[15]
    \newpage

    \setcounter{section}{6}
    \section{test}\advancesec
    \lipsum[15]
    \newpage
    
    \section{test}\advancesec\nonextlink
    \lipsum[15]

\end{document}

Edit: This initially sets the previous-section title to the table of contents, until you change it to the section name in the second section. (You could also just name them “Previous” and “Next,” I suppose.)

It also fixes a problem that came up in a conversation with Javier Bezos and I suddenly remembered I had done myself. Localization packages, including babel and polyglossia, may redefine \arabic in the current language. Code that uses \arabic to programatically generate label names will then break. I changed the code to use \the... instead.

7
  • I'm glad there's at least a way to do it. Do you have any clue if it can be done automatically, without stating \label{sec:6.4} \advancesec \edef\nextsec{sec:7.1} every time?
    – Neil_P
    Commented Jun 9, 2020 at 13:30
  • 1
    @Neil_P All right, there you go.
    – Davislor
    Commented Jun 9, 2020 at 14:08
  • Brilliant! It works perfectly! I accepted your updated answer, cheers!
    – Neil_P
    Commented Jun 9, 2020 at 17:09
  • For anyone else looking at this, I did have a problem where if the table of contents is longer than one page, then this breaks. If you move \pagestyle{fancy} to AFTER where \tableofcontents is, then it works (then fancyhdr doesn't generate footers until after the table of contents. See this post for more; tex.stackexchange.com/questions/177101/…
    – Neil_P
    Commented Jun 9, 2020 at 17:42
  • @Neil_P What was the issue? \autoname{\prevlabel} not working for the sec:0 I added in the ToC? It’d also be possible to turn on the previous link the way it turns off the next link in the last section.
    – Davislor
    Commented Jun 9, 2020 at 22:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .