# Get current “section” name without label

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.

-
Welcome to TeX.sx! What's is the intention of your question? Please add a minimal working example (MWE) that illustrates your problem. –  Marco Daniel Oct 3 '12 at 14:42
Thanks. I added my intent and some examples including a MWE. –  r0estir0bbe Oct 3 '12 at 14:55
To my current knowledge, it is only possible to use nameref in combinations with labels. Is there some other way? Like receiving a reference from header numbering? This way, one could get the reference and plug it into nameref: \nameref{\getrefofnumber{\thechapter}} –  r0estir0bbe Oct 3 '12 at 15:15
Very naively, is The name of the current section is: "\rightmark" doing what you would like? This is what is used (at least in memoir) to extract the name of the current section and put it in the headers. Note that, as section numbering is also given in headers, you might be able to find a way to extract it too if you make some digging in this direction. –  Alex Oct 3 '12 at 15:16
@r0estir0bbe Of course it works out of the box with \currenttitle instead of \currentname. –  egreg Oct 3 '12 at 18:11

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}


## 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.

-
Jup, that works. Thank you. –  r0estir0bbe Oct 3 '12 at 17:48
Can you add a comment on how to prevent \currentname from eating of the consecutive space? –  Lukas Feb 5 at 12:47
@Lukas: See the comment of Martin Scharrer for the question "Problem with \newcommand, no space after calling new command in the compiled text ". –  Heiko Oberdiek Feb 5 at 12:54
@Lukas: See also question "Space after LaTeX commands". –  Heiko Oberdiek Feb 5 at 12:57
@HeikoOberdiek: Yes, that solved it. Thank you. –  Lukas Feb 5 at 13:19

If you use the standard classes that rely on the kernel commands for making headers, then here's a possibility that works unmodified if the class is article (removing the \chapter command, of course).

\documentclass{book}

\usepackage{etoolbox}
\makeatletter
\newif\if@chapters
\@ifundefined{chapter}{\@chaptersfalse}{\@chapterstrue}
\if@chapters
\apptocmd{\@chapter}{\gdef\currentname{#1}}{}{}
\apptocmd{\@schapter}{\gdef\currentname{#1}}{}{}
\fi
\apptocmd{\@sect}{\gdef\currentname{#7}}{}{}
\def\currentname{---Still no title given---}
\makeatother

\begin{document}

\chapter{ABC}

\currentname

\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}


For the memoir class there's nothing to do: the class already provides \currenttitle that does exactly what you want.

Note that you shouldn't redefine \todo as you're trying; rather do

\makeatletter
\renewcommand\todo[2][]{\@todo[#1]{\currentname: #2}}
\makeatother

-
When trying to compile your MWE, I get the following error: > \@chapter=macro: [...] l.11 \show\@chapter Commenting the line \show\@chapter solves the compilation error, but I don't know if it breaks something in your idea. –  r0estir0bbe Oct 3 '12 at 16:19
@r0estir0bbe Sorry, that was only for debugging. I'll remove it. –  egreg Oct 3 '12 at 17:07
I tried your solution in my main document and I didn't work. Then I realized, that I'm using class memoir. Apparently, this breaks your solution. Do you have any idea why? –  r0estir0bbe Oct 3 '12 at 17:10
@r0estir0bbe memoir provides \currenttitle –  egreg Oct 3 '12 at 17:13

You could do something like

\let\oldsection\section
\renewcommand{\section}[2][]{\def\currentname{#2}\oldsection[#1]{#2}}


You probably would want to deal with optional arguments to sectioning commands in a better way.

\documentclass{article}
\makeatletter
\let\oldsection\section
\let\oldsubsection\subsection
\renewcommand{\section}[2][]{\def\currentname{#2}\oldsection[#1]{#2}}
\renewcommand{\subsection}[2][]{\def\currentname{#2}\oldsubsection[#1]{#2}}
\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}


-
What do you mean with: You probably would want to deal with optional arguments to sectioning commands in a better way? –  r0estir0bbe Oct 3 '12 at 15:31
@r0estir0bbe I mean that the command is not perfect. If you look at what gets written in the toc file you will see it gets screwed up since I pass the sectioning commands an empty optional argument. An empty optional argument is not the same as no optional argument. It also doesn't handle the * form of sectioning commands at all (and hence breaks \tableofcontents). I think fixing these things would be relatively easy, but is unrelated to the question asked. –  StrongBad Oct 3 '12 at 15:46
Well, it is true that your example breaks * commands. I tried to find some guides on how to redefine a section command, but I wasn't really successful. Do you know a "How To" for redefining section commands or similar? –  r0estir0bbe Oct 3 '12 at 16:09

Not exactly an answer to your question but maybe useful to others:

The Beamer class provides the commands \secname and \subsecname to get the current sections or subsections name.

This example produces a frame containing the text "Current section: Foo":

\documentclass{beamer}
\begin{document}
\section{Foo}
\begin{frame}
Current section: \secname
\end{frame}
\end{document}

-