52

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.

6
  • 3
    Welcome to TeX.sx! What's is the intention of your question? Please add a minimal working example (MWE) that illustrates your problem. Oct 3, 2012 at 14:42
  • Thanks. I added my intent and some examples including a MWE. Oct 3, 2012 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}} Oct 3, 2012 at 15:15
  • 1
    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, 2012 at 15:16
  • 1
    @r0estir0bbe Of course it works out of the box with \currenttitle instead of \currentname.
    – egreg
    Oct 3, 2012 at 18:11

4 Answers 4

40

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.

9
48

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}
3
  • Can this also be used in templates? I want to include the current section name in the frame title, but including it with \setbeamertemplate{frametitle}{\secname} does not work. I figure it has something to do with the order of when which macro is expanded?
    – bmurauer
    Nov 22, 2017 at 16:08
  • 2
    This post comes up when searching for how to do exactly this in beamer, so yes, it is useful.
    – EL_DON
    Apr 12, 2018 at 15:33
  • 2
    Truly, it is useful to others. I would even give this example: begin{frame}{\secname} . I use the name of the section to set the title of the current frame. You can go further, if you use the subsection as subtitle ;) begin{frame}{\secname}{\subsecname} Feb 27, 2019 at 19:39
17

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
5
  • 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. Oct 3, 2012 at 16:19
  • @r0estir0bbe Sorry, that was only for debugging. I'll remove it.
    – egreg
    Oct 3, 2012 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? Oct 3, 2012 at 17:10
  • @r0estir0bbe memoir provides \currenttitle
    – egreg
    Oct 3, 2012 at 17:13
  • Maybe useful to others: make sure to load etoolbox before bookmark, hyperref and such. Otherwise errors may pop up: e.g., ! Use of \Hy@org@chapter doesn't match its definition. \NR@chapter ...\fi \Hy@next \Hy@org@chapter \gdef. Apr 30, 2021 at 21:02
8

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}

enter image description here

3
  • What do you mean with: You probably would want to deal with optional arguments to sectioning commands in a better way? Oct 3, 2012 at 15:31
  • 1
    @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, 2012 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? Oct 3, 2012 at 16:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .