1

As I understood, I define my chapter title as follows:

\chapter[title in table of contents][title in header line]{title in body}

afterwards I add a label e.g.

\label{Chapter1} 

I know that I can use something like \nameref(Chapter1) to display the title of the respective chapter. However, how can I display the name/title of the current chapter ... so the chapter in which I give the command (e.g. if I want to display the name of the current chapter in my custom footer) Please no fancyhdr solutions. Thanks!

  • 'Please no fancyhdr solutions' ... well.. please add a compilable document as well. Apparently, you're using memoir. And \nameref(Chapter1) will make (pdf)LaTeX turn back on you ;-) Numbered label names are also very error prone – user31729 Oct 31 '17 at 19:06
  • Shall this have a link to the chapter title as well? – user31729 Oct 31 '17 at 19:16
1

A version without any links (Will edit this later on)

\documentclass{memoir}

\usepackage{xpatch}
\usepackage{hyperref}
\makeatletter

\gdef\@currentchaptername{}%
\xpatchcmd{\@makechapterhead}{%
  \chapterheadstart%
}{%
  \gdef\@currentchaptername{#1}%
  \chapterheadstart%
}{\typeout{Patch success for @makechapterhead}}{\typeout{Patch failure for @makechapterhead}}


\newcommand{\currentchapref}{%
  \@currentchaptername%
}

\makeatother

\begin{document}

\tableofcontents
\chapter[title in table of contents][title in header line]{title in body}\label{Chapter1}

\section{ A foo section}

This is \currentchapref. 
\chapter[title in table of contents again][title in header line again]{title in body again}\label{Chapter2}
\end{document}

And here is version with linking (if necessary) or use the starred macro version \currentchapref* to suppress links:

\documentclass{memoir}

\usepackage{blindtext}
\usepackage{xpatch}
\usepackage{hyperref}
\makeatletter

\gdef\@currentchaptername{}%
\xpatchcmd{\@makechapterhead}{%
  \chapterheadstart%
}{%
  \@ifundefined{@currentHref}{%
    \gdef\@currentchaptername{{}{#1}}%
  }{%
    % Store the current anchor for the chapter
    \edef\@currentchaptername{{\@currentHref}{#1}}%
  }%
  \chapterheadstart%
}{\typeout{Patch success for @makechapterhead}}{\typeout{Patch failure for @makechapterhead}}


\NewDocumentCommand{\currentchapref}{sm}{%
  \IfBooleanTF{#1}{%
    \expandafter\@secondoftwo\@currentchaptername% Use only the second argument
  }{%
    \expandafter\hyperlink\@currentchaptername%
  }%
}

\makeatother

\begin{document}

\tableofcontents
\chapter[title in table of contents][title in header line]{title in body}\label{Chapter1}

\section{ A foo section}

\blindtext[10]
This is \currentchapref. 
\chapter[title in table of contents again][title in header line again]{title in body again}\label{Chapter2}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.