40

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.

  • 3
    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
  • 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 '12 at 15:16
  • 1
    @r0estir0bbe Of course it works out of the box with \currenttitle instead of \currentname. – egreg Oct 3 '12 at 18:11
30

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.

28

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}
  • 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 '17 at 16:08
  • This post comes up when searching for how to do exactly this in beamer, so yes, it is useful. – EL_DON Apr 12 '18 at 15:33
  • 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} – loved.by.Jesus Feb 27 at 19:39
14

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
6

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

  • 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
  • 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 '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

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.