# Would it be possible to create a navigation menu at the end of each section with automatically generated links?

I want to make a navigation menu at the bottom of each section|subsection|subsubsection|paragraph.

This document will be embedded in the binary of an application. It is what the user will see when he or she clicks on help. Because of the nature of this document (too long to be printed, yet can be printed), it should have some navigational assistance for an audience with a mouse.

I imagine something like the navigation menu for the beamer class at the bottom of each slide. I am using the article class (though heavily modified), however.

When searching for this, I found

# Implementation

Note that I will use POSIX directory notation to represent parent sections (.. and ../..). The implementation would likely require a macro for the menu with some if/then logic that can detect whether the section has a parent for determining whether or not the "Jump up ../.." button should be visible or at least visibly disabled. The titlesec definitions would need to have hooks such that automatic links to the parent sections (..) and parent parent (../..) sections can be created.

I need to be able to

• Reference ToC with a static link DONE
• Reference parent .. section dynamically (probably could use Get the value of current \label)
• Reference parent ../.. section dynamically
• Reference previous page \thepage - 1 DONE
• Reference next page \thepage + 1 DONE

# Mock-up Implementation

\documentclass{article}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage[compact,explicit,noindentafter]{titlesec}
\usepackage{fancyhdr}
\usepackage{xcolor}
\usepackage[hidelinks]{hyperref}
\usepackage{amsmath,amssymb}

\fancypagestyle{digitalpage} % This style could inject the implementation of the navigation menu into titlesec definitions maybe (so that it can be toggled easily)?
{
\fancyhf{} % clear all fields
\renewcommand{\headrulewidth}{0pt}
\lhead{}
\lfoot{}
\cfoot{}
\rfoot{%
\bfseries\footnotesize\thepage
}%
}%

\newcommand\navigationmenu{\vspace{1ex}\noindent\footnotesize\textcolor{gray}{\sffamily Jump to Contents $\Uparrow$} | \textcolor{gray}{\hyperlink{sub}{\sffamily Jump .. $\uparrow$}} | \textcolor{gray}{\hyperlink{sec}{\sffamily Jump ../.. $\upuparrows$}} | \textcolor{gray}{\sffamily $\leftarrow$ Previous Page} | \textcolor{gray}{\sffamily $\rightarrow$ Next Page}} % contains some logic, if section has no ../.., don't reveal.

% SECTION
\titleformat{\section}[hang]{\Huge\bfseries}{\thesection}{1cm}{#1}[\thispagestyle{digitalpage}]
% SUBSECTION
\titleformat{\subsection}[hang]{\Large\bfseries}{\thesubsection}{1cm}{#1}

\begin{document}
\hypertarget{sec}{\section{Something}}
\lipsum[1]
\navigationmenu
\hypertarget{sub}{\subsection{Something something}}
\lipsum[2]
\navigationmenu
\end{document}


# Output

• it's bad that you can't upvote your own question? :-) Apr 6, 2016 at 18:12
• If i understand, in the end of paragraph jump .. means go back to paragraph heading and Jump ../.. means go back to subsubsection heading, no? 2) are paragraphs numbered? Apr 8, 2016 at 9:35
• @touhami Yes, it could be expressed as: jump .. means go back to the previous/latest paragraph heading (whether it be section/subsection/subsub.. etc.). Whereas jump ../.. means to back to the paragraph heading above the previous/latest paragraph heading-a function only available when this is possible. Apr 8, 2016 at 9:55

## 1 Answer

Here is a solution.

The idea is to redefine \ttl@useclass to get informations about level of sectioning we are starting so that we can prepare the next navigationmenu and by the way display the navigationmenu of previews section just before starting the new one.

\renewcommand*\ttl@useclass[2]{%
\ifnum\@nameuse{ttll@#2}>\c@secnumdepth\relax\stepcounter{mtpar}\fi%
\displaynavigationmenu%
\renewcommand*\displaynavigationmenu{\navigationmenu{#2}}%
\oldttl@useclass{#1}{#2}}


Notes

1. the default value of secnumdepth is supposed so code need to be changed if this value is changed (may be completed later)
2. grouping used in \navigationmenu definition because of font size change.
3. nobreak was add to avoid navigationmenu in top of next page.
4. Contents, previews page and next page and not considered here (already done by the OP).

\documentclass{article}
\usepackage{lipsum}
\usepackage[compact,explicit,noindentafter]{titlesec}
\usepackage{fancyhdr}
\usepackage{xcolor}
\usepackage[hidelinks]{hyperref}
\usepackage{amsmath,amssymb}

\fancypagestyle{digitalpage} % This style could inject the implementation of the navigation menu into titlesec definitions maybe (so that it can be toggled easily)?
{
\fancyhf{} % clear all fields
\renewcommand{\headrulewidth}{0pt}
\lhead{}
\lfoot{}
\cfoot{}
\rfoot{%
\bfseries\footnotesize\thepage
}%
}%

\newcommand*\displaynavigationmenu{}
\newcounter{mtpar}%  global counter for paragraphs

\makeatletter
\newcommand{\mtlink}[2]{%
\ifnum\@nameuse{ttll@#1}>\c@secnumdepth\relax%  % if parent .. section is paragraph
\textcolor{gray}{\hyperlink{#1*.\themtpar}{\sffamily Jump .. $\uparrow$}} |
\else% % if parent .. section is (sub...)section
\textcolor{gray}{\hyperlink{#1.\csname the#1\endcsname}{\sffamily Jump .. $\uparrow$}} |
\fi
\ifnum\value{#2}>0%  % if parent parent ../.. section exist
\textcolor{gray}{\hyperlink{#2.\csname the#2\endcsname}{\sffamily Jump ../.. $\upuparrows$}} |
%\else
%no parant ../.. here
\fi}

\newcommand\navigationmenu[1]{%
\begingroup
\nobreak\vspace{1ex}\nobreak
\noindent\footnotesize\textcolor{gray}{\sffamily Jump to Contents $\Uparrow$} |
\ifcase\@nameuse{ttll@#1}\or
\mtlink{section}{part}\or
\mtlink{subsection}{section}\or
\mtlink{subsubsection}{subsection}\or
\mtlink{paragraph}{subsubsection}\fi
\textcolor{gray}{\sffamily $\leftarrow$ Previous Page} |
\textcolor{gray}{\sffamily $\rightarrow$ Next Page}
\endgroup}

\let\oldttl@useclass\ttl@useclass
\renewcommand*\ttl@useclass[2]{%
\ifnum\@nameuse{ttll@#2}>\c@secnumdepth\relax\stepcounter{mtpar}\fi%
\displaynavigationmenu%
\renewcommand*\displaynavigationmenu{\navigationmenu{#2}}%
\oldttl@useclass{#1}{#2}}
\makeatother

\AtEndDocument{\displaynavigationmenu}% for the last ...section

% SECTION
\titleformat{\section}[hang]{\Huge\bfseries}{\thesection}{1cm}{#1}[\thispagestyle{digitalpage}]
% SUBSECTION
\titleformat{\subsection}[hang]{\Large\bfseries}{\thesubsection}{1cm}{#1}

\begin{document}
\section{Something}
\lipsum[1]

\subsection{Something something}
\lipsum[2]

\paragraph{Something foo}

\lipsum[3]

\subsection{Something something else}
\lipsum[3]

\subsubsection{Something something foo}
\lipsum[3]

\paragraph{Something foo}
\lipsum[3]
\end{document}