[Tex/LaTex] Get current “section” name without label

cross-referencingmemoir

Update:

I accepted the solution provided by Heiko Oberdiek, because that was doing exactly what I asked.

When interested only in the chapter name, egreg's solution works fine. However, to get it working with memoir, one has to change the parameter from #1 to #2. Don't ask me why.

\documentclass[a4paper, 10pt, oneside]{memoir}
\usepackage{etoolbox}
\makeatletter
\apptocmd{\@chapter}{\gdef\currentchapter{#2}}{}{}
\def\currentchapter{?}
\makeatother

\begin{document}
\chapter{A Chapter}
``\currentchapter''
\section{A Section}
``\currentchapter''
\end{document}

Old question:
I want to get the name of the current "section" (chapter, section, subsection, …) without labeling the section.

I tried using \nameref with e.g. \thechapter which obviously doesn't work, as \nameref expects a reference (the name of \label) and not a number.

Is there a command, that gives me directly the name of the current "section"? Or is there a command which can "generate" a reference out of section numbering, like: \getname{\thechapter}?

I found out already, that it is impossible to get the numbering of the current "section" – I'd need to check the counter to get the correct "section".

My goal is to redefine \todo from the package todonotes that it always displays the current "section" name:

\let\Oldtodo\todo
\renewcommand{\todo}[1]{\Oldtodo{\currentname: #1}}

If it is not possible to get the name of the current "section", I would be happy with the current chapter or section name too:

\let\Oldtodo\todo
\renewcommand{\todo}[1]{\Oldtodo{\currentchaptername: #1}}

Below is a simple example without todonotes.

\documentclass{memoir}
\begin{document}

\section{My section name}
The name of the current section is: " \currentname "
It should be: " My section name "

\subsection{My subsection name}
The name of the current subsection is: " \currentname "
It should be: " My subsection name "

\end{document}

Notice that I'm using documentclass memoir.

Best Answer

Variations of the same theme. All the title-/nameref packages have to remember the current title somewhere.

Package nameref

\documentclass{article}

\usepackage{nameref}
\makeatletter
\newcommand*{\currentname}{\@currentlabelname}
\makeatother

\begin{document}

\section{My section name}
The name of the current section is: "\currentname".\\
It should be: "My section name".

\subsection{My subsection name}
The name of the current subsection is: "\currentname".\\
It should be: "My subsection name".

\end{document}

Result with nameref

Package titleref

\documentclass{article}

\usepackage{titleref}
\makeatletter
\newcommand*{\currentname}{\TR@currentTitle}
\makeatother

\begin{document}

\section{My section name}
The name of the current section is: "\currentname".\\
It should be: "My section name".

\subsection{My subsection name}
The name of the current subsection is: "\currentname".\\
It should be: "My subsection name".

\end{document}

Same result.

Package zref-titleref

\documentclass{article}

\usepackage{zref-titleref}
\makeatletter
\newcommand*{\currentname}{\zref@getcurrent{title}}
% or \newcommand*{\currentname}{\zref@titleref@current}
\makeatother

\begin{document}

\section{My section name}
The name of the current section is: "\currentname".\\
It should be: "My section name".

\subsection{My subsection name}
The name of the current subsection is: "\currentname".\\
It should be: "My subsection name".

\end{document}

Same result.

Related Question