# previous section name

Is there a way to get the title [short] and the number of the previous and the next sections?

\sectiontitle % where is it defined I wonder
\thesection

So, I can calculate, for example, the counter of the previous section:

\the\numexpr\thesection-1\relax

What I'm missing is something like

\prevsectiontitle, \nextsectiontitle ?
• This deliberately depends on the document class and requires hacking into the section commands – user31729 Jul 30 '17 at 6:28
• You should not rely on the output of \thesection giving the pure number. Rather use \the\numexpr\value{section}-1\relax for example – user31729 Jul 30 '17 at 8:38

A version with saving automatic generated labels containing the information on section titles to the .aux file and extracting the previous, the current and the next section title.

The counter sectioncntr is never reset automatically, it continues to count the number of sections in the document and at each \refstepcounter of this counter a \zlabel is set, which stores the sectiontitle property which has been defined before with \zref@newprop.

The short names \Xtitle where X stands for previous, current or next do not insert links to the section header, whereas the long names \Xsectiontitle insert links. See the example with the page footer for an application.

\documentclass{article}

\usepackage[user,hyperref]{zref}
\usepackage{fancyhdr}

\usepackage{blindtext}

\fancypagestyle{plain}{%
\fancyhf{}
\fancyfoot[L]{\previoussectiontitle}
\fancyfoot[C]{\currenttitle}
\fancyfoot[R]{\nextsectiontitle}
}
\usepackage{xpatch}

\makeatletter

\zref@newprop{sectiontitle}[unknown]{\@currentsectiontitle}

\newcounter{sectioncntr}

\newcommand{\previoustitle}{%
\zref@ifrefundefined{sectiontitle:\the\numexpr\c@section-1}{}{%
\zref@extract{sectiontitle:\the\numexpr\c@section-1}{sectiontitle}%
}%
}

\newcommand{\currenttitle}{%
\zref@ifrefundefined{sectiontitle:\number\value{section}}{}{%
\zref@extract{sectiontitle:\number\value{section}}{sectiontitle}%
}%
}

\newcommand{\nexttitle}{%
\zref@ifrefundefined{sectiontitle:\the\numexpr\c@section+1}{}{%
\zref@extract{sectiontitle:\the\numexpr\c@section+1}{sectiontitle}%
}%
}

\newcommand{\previoussectiontitle}{%
\zref@ifrefundefined{sectiontitle:\the\numexpr\c@section-1}{}{%
}%
}

\newcommand{\currentsectiontitle}{%
\zref@ifrefundefined{sectiontitle:\number\value{section}}{}{%
}%
}

\newcommand{\nextsectiontitle}{%
\zref@ifrefundefined{sectiontitle:\the\numexpr\c@section+1}{}{%
}%
}

\xpatchcmd{\@sect}{%
\@tempskipa #5\relax
}{%
\def\@currentsectiontitle{#7}
\ifnum0=\pdfstrcmp{#1}{section}
\refstepcounter{sectioncntr}\zlabel{sectiontitle:\the\c@section}%
\fi
\@tempskipa #5\relax
}{%
\typeout{Patch Success}
}{%
\typeout{Patch Failure}
}
\makeatother

\usepackage{hyperref}

\pagestyle{plain}
\begin{document}

\section{Foo}
\subsection{Foo subsection}
\blindtext[20]
\section{Foobar}
\blindtext[20]
\section{Next Foo}
\blindtext[20]
\end{document}

• I assumed an article - like class here. book will be a little bit more complicated in order to prevent sections from previous or next chapters to be shown as well – user31729 Jul 30 '17 at 7:00
• It is designed for \section, not for \section* however – user31729 Jul 30 '17 at 7:12
• Thank you, Christian. I'm using a book-like class (with a single chapter per document though). Your \xpatchcmd{\@sect} fails. Also, eventually I'm redefining \section for auto-labeling as follows \LetLtxMacro{\oldsection}{\section} \RenewDocumentCommand \section{sm} {\IfBooleanTF{#1} {\oldsection*{#2}\label{sec:#2}} {\oldsection{#2}\label{sec:#2}} } – user612313 Jul 31 '17 at 8:41
• I'm using extbook – user612313 Jul 31 '17 at 8:53
• @user612313: Yes, that's why it is important to provide a complete example and not only wishes. Patching must fail with xparse macros! You should update your question with the minimal working document – user31729 Jul 31 '17 at 9:17