# Macro to automatically save section name of previous section and next section

EDIT: I've edited the code that reproduces the anomalies, but I'll work on the code to more accurately depict the problem later on the day.

1. Weird spacing in index
2. Index doesn't appear in footlinks / isn't recognised as a section

The code won't show properly on this page (somehow 4 spaces doesn't do the trick), so I pasted it here.

I'm trying to create a macro to save the name of the previous and next section automatically, so I can reuse it in my footer (with a link, but that's easily done through \hyperlink{page.\getpagerefnumber{<anchor>}} and anchors).

See the rfoot with the hyperlinks to different appendices.

MWE:

\documentclass{article}

\usepackage{marginnote}
\usepackage{pbox}
\usepackage[heightrounded]{geometry}
\usepackage{lastpage}
\usepackage[toc,titletoc,page]{appendix}

% Set some commands that I'll use for all footers
\usepackage{fancyhdr}
\fancyhf{}
\renewcommand{\footrulewidth}{.4pt}

% Command for pageref for page/LastPage in footer
\cfoot{\thepage /\pageref{LastPage}\hyperlink{page.\getpagerefnumber{anchor.lastpage}}{ $\rightarrow$}}

% Global pagestyle commands
\fancypagestyle{contents}{
\lfoot{\textbf{Inhoudsopgave}}
}
\fancypagestyle{document}{
\rfoot{\hyperlink{document.toc}{$\leftarrow$ Naar inhoudsopgave}}
\lfoot{\currentsection}
}

% By declaring the plain pagestyle explicitly, the first page is forced in terms of header and footer settings
% Not declaring this page style will not apply fancyhdr settings to the first page
\fancypagestyle{plain}{
\lfoot{\textbf{Inhoudsopgave}}
\cfoot{\thepage /\pageref{LastPage}\hyperlink{page.\getpagerefnumber{anchor.lastpage}}{ $\rightarrow$}}
\rfoot{}
\renewcommand{\footrulewidth}{0.4pt}
}

% header dummies which will be used in appendices - Auto-adjustment for variable page print of appendices
% It's also possible to use \getpagerefnumber and link thereto, but that gets you to the top of the page rather than the top of the appendix
% It's a small difference, but this yields a (slightly) prettier result
\rfoot{\pbox{5cm}{\hyperlink{document.toc}{$\leftarrow$ Naar inhoudsopgave} \\ \hyperlink{appendix.b}{Naar begrippenlijst $\rightarrow$}  \\ \hyperlink{appendix.c}{Naar Index $\rightarrow$ }}}%
\lfoot{\currentsection}
}
\rfoot{\pbox{5cm}{\hyperlink{document.toc}{$\leftarrow$ Naar inhoudsopgave} \\ \hyperlink{appendix.a}{$\leftarrow$ Naar \textbf{\textcolor{blue}{R}} commando's} \\ \hyperlink{appendix.c}{Naar Index $\rightarrow$ }}}%
}
\rfoot{\pbox{5cm}{\hyperlink{document.toc}{$\leftarrow$ Naar inhoudsopgave} \\ \hyperlink{appendix.b}{$\leftarrow$ Naar begrippenlijst} \\ \hyperlink{appendix.d}{Naar Nesting $\rightarrow$ }}}%
}
\rfoot{\pbox{5cm}{\hyperlink{document.toc}{$\leftarrow$ Naar inhoudsopgave} \\ \hyperlink{appendix.b}{$\leftarrow$ Naar begrippenlijst } \\ \hyperlink{appendix.c}{$\leftarrow$ Naar Index }}}%
}

\fancypagestyle{a.appendix.sep}{
\rfoot{\pbox{5cm}{\hyperlink{document.toc}{$\leftarrow$ Naar inhoudsopgave} \\ \hyperlink{appendix.b}{Naar begrippenlijst $\rightarrow$}  \\ \hyperlink{appendix.c}{Naar Index $\rightarrow$ }}}%
\lfoot{\currentsection}
}
\fancypagestyle{b.appendix.sep}{
\rfoot{\pbox{5cm}{\hyperlink{document.toc}{$\leftarrow$ Naar inhoudsopgave} \\ \hyperlink{appendix.a}{$\leftarrow$ Naar \textbf{\textcolor{blue}{R}} commando's} \\ \hyperlink{appendix.c}{Naar Index $\rightarrow$ }}}%
\lfoot{\currentsection}
}
\fancypagestyle{c.appendix.sep}{
\rfoot{\pbox{5cm}{\hyperlink{document.toc}{$\leftarrow$ Naar inhoudsopgave} \\ \hyperlink{appendix.b}{$\leftarrow$ Naar begrippenlijst} \\ \hyperlink{appendix.d}{Naar Nesting $\rightarrow$ }}}%
\lfoot{\currentsection}
}
\fancypagestyle{d.appendix.sep}{
\rfoot{\pbox{5cm}{\hyperlink{document.toc}{$\leftarrow$ Naar inhoudsopgave} \\ \hyperlink{appendix.b}{$\leftarrow$ Naar begrippenlijst } \\ \hyperlink{appendix.c}{$\leftarrow$ Naar Index }}}%
\lfoot{\currentsection}
}

% Cross-references
\usepackage[pageanchor]{hyperref}
hypertexnames=false,
pdfhighlight=/N,
pdfstartview={XYZ null null 1.00}
}

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

\begin{document}

\pagestyle{contents}
\hypertarget{document.toc}{}
\maketitle
\tableofcontents

% Footer section name - Declare after ToC to keep section numbering intact
\newcommand{\currentsection}{}
\let\oldsection\section

\newpage

\pagestyle{document}
\section{Week 1 - Intro \textcolor{blue}{\textbf{R}}}
\subsection{Objecten}
\subsubsection{Objecten met \'e\'en getal}

\newpage

% Clearpage to move floats as well, use newpage if you don't have floats on the last page
\clearpage
\begin{appendices}

% I am using hypertargets from the preamble to make sure hyperref takes you to the top of the page and not the middle of it
% I've already explained why I picked this solution over \getpagerefnumber
\section{\textbf{\textcolor{blue}{R} commando's}}

text

\newpage

\section{\textbf{Begrippenlijst}}

\newpage

\section{\textbf{Index}}

text

\newpage

\section{\textbf{Nesting}}

text

\end{appendices}

% Because the footer is printed last and there is a space between the appendices environment and the end of the document, we have to force this value or define set a manual environment. Setting a new environment would be the more tedious solution
\rfoot{\pbox{5cm}{\hyperlink{document.toc}{$\leftarrow$ Naar inhoudsopgave} \\ \hyperlink{appendix.b}{$\leftarrow$ Naar begrippenlijst } \\ \hyperlink{appendix.c}{$\leftarrow$ Naar Index }}}%
\lfoot{\textbf{Nesting}} % This will have to be set manually to circumvent cross-environment anomalies with fancyhdr

% Anchor on last page
\label{anchor.lastpage}

\end{document}

-
I believe you have to catch the \section command and grab the section title in order to store it to some \gdefed command. –  Christian Hupfer May 3 at 18:06
Maybe it is just me but having compiled this I have no idea what it is supposed to look like or (therefore) what the problem is supposed to be. Is the issue specific to appendices? I'm assuming yes else you wouldn't have them in a minimal example but I just find it confusing. (Probably not helped by not understanding the language, mind.) –  cfr May 3 at 22:43
@cfr I want to automate creation of a dynamic footer which allows the user to navigate with hyperlinks through the appendix –  1010011010 May 4 at 0:08
How is that code supposed to do that? I don't see how you are using \oldsection or which parts of that are meant to be relevant. You use a custom pagestyle for each appendix. I don't understand how the other one you define is supposed to be used or how that one is meant to work with \oldsection etc.? –  cfr May 4 at 1:26

Improved version

I made some kind of a hack, redefining the \section command and introducing the possibility to refer to the previous and next section title name as hyperlinks. The titles are inserted automatically.

\documentclass[paper=a4,12pt,onesided]{scrartcl}
\usepackage{blindtext}
\usepackage{everypage}
\usepackage{lastpage}%
\usepackage{xcolor}%
\usepackage{imakeidx}%
\usepackage[heightrounded]{geometry}%
\usepackage[toc,titletoc,page]{appendix}%

\usepackage{etoolbox}
\usepackage{bbding}

\usepackage[pageanchor]{hyperref}%

\newrobustcmd{\ProvideBool}[2][false]{%
\newbool{#2}%
\setbool{#2}{#1}%
}%

\ProvideBool[true]{DocumentSectionAutoClearpage}%

\newrobustcmd{\EnableSectionAutoClearpage}{%
\setbool{DocumentSectionAutoClearpage}{true}%
}%

\newrobustcmd{\DisableSectionAutoClearpage}{%
\setbool{DocumentSectionAutoClearpage}{false}%
}%

hypertexnames=false,
pdfhighlight=/N,
pdfstartview={XYZ null null 1.00}
}

\AtBeginDocument{%
\setcounter{PrintIndexSectionCounter}{\number\value{IndexSectionCounterValue}}
\EnableSectionAutoClearpage%
}%

\makeatletter
\let\LaTeXStandardSection\section

% Providing section command with no optional argument

\newrobustcmd{\improvedstarredsection}[1]{%
% No name reference to unnumbered sections
%\BeforeSectionStarts{#1}%
\LaTeXStandardSection*{#1}%
}%

\newrobustcmd{\improvedsection@noopt}[1]{%
\BeforeSectionStarts{#1}%
\LaTeXStandardSection{#1}%
}%

% Providing section command with an optional argument
\newrobustcmd{\improvedsection@opt}[2][]{%
\BeforeSectionStarts{#1}%
\LaTeXStandardSection[#1]{#2}%
}%

% "True" section command
\newrobustcmd{\improvedsection}{%
\ifboolexpr{bool{DocumentSectionAutoClearpage}}{\clearpage}{}
\@ifnextchar[{\improvedsection@opt}{\improvedsection@noopt}
}%

%%%%%% Use the 'improved' section commands with forward/backward links
\renewcommand{\section}{%
\@ifstar{\improvedstarredsection}{\improvedsection}%
}
\makeatother

\makeatletter
\renewenvironment{theindex}{%
\if@twocolumn
\@restonecolfalse
\else
\@restonecoltrue
\fi
\phantomsection%
\section{\indexname}%
\setcounter{PrintIndexSectionCounter}{\number\value{CurrentSectionCounter}}%
\@mkboth{\MakeUppercase\indexname}%
{\MakeUppercase\indexname}%
\parindent\z@
% \thispagestyle{plain}\parindent\z@
\parskip\z@ \@plus .3\p@\relax
\columnseprule \z@
\columnsep 35\p@
\let\item\@idxitem}
{\if@restonecol\onecolumn\else\clearpage\fi}%
\makeatother

\makeatletter
\newrobustcmd{\LastSectionInFile}{%
\immediate\write\@auxout{\string
\setcounter{LastSectionValue}{\number\value{CurrentSectionCounter}}%
}%
}

\newrobustcmd{\IndexSectionInFile}{%
\immediate\write\@auxout{\string
\setcounter{IndexSectionCounterValue}{\number\value{PrintIndexSectionCounter}}%
}%
}%

\makeatother

\AtEndDocument{%
\LastSectionInFile%
\IndexSectionInFile%
}%

\newrobustcmd{\PreviousSectionName}{\refcommand{CurrentSection::Title::\number\value{PreviousSectionCounter}}}%
\newrobustcmd{\CurrentSectionName}{\refcommand{CurrentSection::Title::\number\value{CurrentSectionCounter}}}%
\newrobustcmd{\NextSectionName}{\refcommand{CurrentSection::Title::\number\value{NextSectionCounter}}}%

\DoIfNumInequal{\number\value{#1}}{\number\value{#3}}{%
\DoIfNumNotZero{\number\value{PrintIndexSectionCounter}}{%
% Only set a link of the next section is not the index section%
\ifnumequal{\number\value{#1}+1}{\number\value{PrintIndexSectionCounter}}{%
\setcounter{InternalTitleCounter}{\number\value{#2}}%
}{
}}%
{%
}%
}% End of inner \DoIfNumInEqual
}%

\DoIfNumInequal{\number\value{#1}}{1}{%
\DoIfNumNotZero{\number\value{PrintIndexSectionCounter}}{%
\ifnumequal{\number\value{#1}-1}{\number\value{PrintIndexSectionCounter}}{%
\setcounter{InternalTitleCounter}{\number\value{#2}}%
}{
%  \DoIfNumInequal{\number\value{#1}-1}{\number\value{PrintIndexSectionCounter}}{%
}%
}{%
% Insert link in any case, since there is no bottom Index link
}%
}% End of outer \DoIfNumInequal
}% End of command

\DoIfNumInequal{\number\value{#1}}{\number\value{#3}}{%
\DoIfNumNotZero{\number\value{PrintIndexSectionCounter}}{%
\ifnumequal{\number\value{#1}+1}{\number\value{PrintIndexSectionCounter}}{%
\setcounter{InternalTitleCounter}{\number\value{#2}}%
}{%
}%%
}{%
}%
}% End of outer \DoIfNumInEqual
}%

\DoIfNumInequal{\number\value{#1}}{1}{%
% But do nothing if the section number is equal to the total section counter value
\DoIfNumNotZero{\number\value{#1}}{%
\ifnumequal{\number\value{#1}-1}{\number\value{PrintIndexSectionCounter}}{%
\setcounter{InternalTitleCounter}{\number\value{#1}}%
}{%
}%
}{%
}%
}% End of outer \DoIfNumInequal
}% End of command

\renewcommand{\indexname}{Index}
\renewcommand{\contentsname}{Inhoudsopgave}

\newrobustcmd{\DoIfNumNotZero}[3]{%
\ifnumequal{#1}{0}{#3}{#2}%
}%

\newrobustcmd{\DoIfNumInequal}[3]{%
\ifnumequal{#1}{#2}{}{#3}%
}%

\newrobustcmd{\IfNumInequal}[4]{%
\ifnumequal{#1}{#2}{#3}{#4}%
}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcounter{LastDocumentPageCounter}%
\setcounter{LastDocumentPageCounter}{1}

\newcounter{IndexSectionCounterValue}%
\newcounter{PrintIndexSectionCounter}%

\newcounter{PreviousSectionCounter}% No resetting%%%
\setcounter{PreviousSectionCounter}{0}%

\newcounter{NextSectionCounter}% No resetting%%%
\setcounter{NextSectionCounter}{0}

\newcounter{CurrentSectionCounter}% No resetting%%%
\setcounter{CurrentSectionCounter}{0}

\newcounter{LastSectionValue}

\newcounter{InternalTitleCounter}

\newrobustcmd{\refcommand}[1]{%
\nameref*{#1}%
}%

\newrobustcmd{\CurrentSectionSymbol}{\textbf{\textcolor{blue}{\HandRight}}~}

\makeatletter

\newrobustcmd{\WriteUniqueTextLabelContent}[2]{%
\immediate\write\@auxout{%
\string\newlabel{#1}{{\thesection}{\thepage}{\unexpanded{#2}}{}}%
}%
}%

\newrobustcmd{\BeforeSectionStarts}[1]{%
\stepcounter{CurrentSectionCounter}%
%%%%% Check first whether there is already a previous section
\ifnumequal{\number\value{CurrentSectionCounter}}{1}{% Yes, section number is zero
\setcounter{PreviousSectionCounter}{1}%
}{%
\setcounter{PreviousSectionCounter}{\number\value{CurrentSectionCounter}}
}
% CurrentSectionCounter > 1
% Set Target to current section
\phantomsection
\hypertarget{CurrentSection::\number\value{CurrentSectionCounter}}{}%
\WriteUniqueTextLabelContent{CurrentSection::Title::\number\value{CurrentSectionCounter}}{#1}%
% Set correct numbers
\setcounter{NextSectionCounter}{\number\value{CurrentSectionCounter}}
\ifnumequal{\number\value{CurrentSectionCounter}}{\number\value{LastSectionValue}}{}{%
}%
\phantomsection
}%

\newrobustcmd{\InsertToIndexSymbol}[4]{%
% #1: Number of IndexSection
% #2: Number of CurrentSection
% #3: Text for link to previous section (normally left hand?)
% #4: Text for link to next section (normally right hand?)
\ifnumequal{\number\value{#1}}{0}{%
% Do nothing, since there is no Index section (value of #1)!
}{%
\ifnumless{\number\value{#1}}{\number\value{#2}}{%
}{% Not lesser, check greater then....
\ifnumgreater{\number\value{#1}}{\number\value{#2}}{%
}{%
% Do nothing, since both counters are equal!!!!
}%
}% End of \ifnumless is not true, i.e. other branch >= value of #2
}%
}%

\makeatother

\usepackage{fancyhdr}
\fancyhf{}%
\renewcommand{\footrulewidth}{.4pt}%

\fancypagestyle{contents}{%
\lfoot{\begin{tabular}{l}%
\CurrentSectionSymbol\textbf{\contentsname} \tabularnewline
\tabularnewline%
\tabularnewline%
\tabularnewline%
\end{tabular}
}%
\cfoot{}%
\rfoot{}%
}

\fancypagestyle{normalpages}{%
\lfoot{\begin{tabular}{l}%
\CurrentSectionSymbol\textbf{\contentsname} \tabularnewline
\tabularnewline%
\tabularnewline%
\tabularnewline%
\end{tabular}
}%
\cfoot{}%
\rfoot{}%
}

\lfoot{\begin{tabular}{l}%
\CurrentSectionSymbol\CurrentSectionName \tabularnewline
\tabularnewline%
\tabularnewline%
\tabularnewline%
\end{tabular}
}%
\cfoot{\begin{tabular}{c}%
\tabularnewline%
\tabularnewline%
\tabularnewline%
\end{tabular}
}%
\rfoot{\begin{tabular}{@{}rl}
%     \DoIfIndexSection{PrintIndexSectionCounter}{CurrentSectionCounter}{\ToPreviousSectionSymbol}{\ToNextSectionSymbol} &
\InsertToIndexSymbol{PrintIndexSectionCounter}{CurrentSectionCounter}{\ToPreviousSectionSymbol}{\ToNextSectionSymbol} &
\end{tabular}%
}%
}%

\makeindex

\begin{document}
\phantomsection
\hypertarget{document.toc}{}%

\pagestyle{contents}
\tableofcontents
\clearpage

\pagestyle{normalpages}

\section{First Section}
\blindtext[2]
\index{Schr\"odinger}
\section{Another section}
\blindtext[3]

\section[Short Section Title]{Another section with a not so short title}

\section{The three witches}
\index{Shakespeare}
\index{Macbeth}

\clearpage %

\clearpage

\begin{appendices}
\clearpage

\section{\textbf{\textcolor{blue}{R} commando's}}

\blindtext%

\section*{And I am a completely unnumbered section and do not appear in the foot links ;-)}

\section{Begrippenlijst}
\blindtext%

\printindex% Note: \printindex behavious like an extra section

\section{\textbf{$E = mc^2$}}
\blindtext%

\section{Dummy 1}
\section{Dummy 2}
\blindtext%
\clearpage
\end{appendices}
\end{document}


Features:

• The foot link to the next and previous secion are omitted if the standard link to index section is just one section ahead or before. The link then points to the section +- 2 ahead/before

• \printindex behavious like a section entry now, with TOC item

• If there is no \printindex, no bottom index link is inserted. Remove the .aux file in this case manually!
-
It is meant for sections only, not for chapters... –  Christian Hupfer May 4 at 8:49
Applying your \let\LaTeXStandardSection\section etc. code nested in AtBeginEnvironment{appendices} makes this functional in real documents as well (it will exclude section names of the actual document). The last hiatus will be to conditionally set the links to have the same format as what I had in my example document. –  1010011010 May 4 at 14:26
@Euryis: I will look at your edits later on, what they really change. –  Christian Hupfer May 4 at 15:48
@Euryris: I am working out another version of my code, which should it make easier to adapt, also providing a better counting scheme, therefore remove some errors/warnings. Basically you should get the name of the sections ahead with another counter (value) of CurrentSectionCounter + 2, but of course, you should check whether there is such a total section value at all and most probably fix the possible value at the end to the maximum section value. The same holds the other way round for sections before the current section. –  Christian Hupfer May 5 at 17:10
@1010011010: I updated the code, this addresses (somehow) the other question you posted regarding the \printindex –  Christian Hupfer May 14 at 5:55