6

I want to know how to get the label of the chapter/section/subsection/paragraph which my text is currently in. I don't mean the name of the section via \nameref{} or \titleref{} , nor the number \ref{}, nor any \label{} related output.

I want to get the \chaptername and so on, but related to the current nesting depth, with only one command. Best combined with the babel package for international use. If this works a rearranging of sections/paragraphs is easier, because the labels get updated according to the heading they are in.

I can't provide a MWE, but here is something that comes quite close to what I mean. Note that the \currentheadingname command is the missing piece, which I am looking for.

\documentclass{book}
\begin{document}
\chapter{SomeChapter}
Greetings from this \currentheadingname

\section{SomeSection}
In this \currentheadingname \dots 

\subsection{SomeSubsection}
This \currentheadingname is about \dots
\end{document}

The desired result looks like:

  1. SomeChapter

Greetings from this chapter

1.1. SomeSection

In this section ...

1.1.1. SomeSubsection

This subsection is about...

Is there such a command? Or any workaround?

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Jan 28 '14 at 17:16
8

As far as I know it doesn't exists, but it can be defined.

We'll define it empty

\newcommand{\currentheadingname}{}

and redefine it for each sectioning command with the help of \pretocmd from the etoolbox package:

\pretocmd{\part}{\renewcommand{\currentheadingname}{part\xspace}}{}{}
\pretocmd{\chapter}{\renewcommand{\currentheadingname}{chapter\xspace}}{}{}
\pretocmd{\section}{\renewcommand{\currentheadingname}{section\xspace}}{}{}
\pretocmd{\subsection}{\renewcommand{\currentheadingname}{subsection\xspace}}{}{}
\pretocmd{\subsubsection}{\renewcommand{\currentheadingname}{subsubsection\xspace}}{}{}
\pretocmd{\paragraph}{\renewcommand{\currentheadingname}{paragraph\xspace}}{}{}
\pretocmd{\subparagraph}{\renewcommand{\currentheadingname}{subparagraph\xspace}}{}{}

Complete MWE:

\documentclass{book}
\usepackage{etoolbox}
\usepackage{xspace} 

\newcommand{\currentheadingname}{}

\pretocmd{\part}{\renewcommand{\currentheadingname}{part\xspace}}{}{}
\pretocmd{\chapter}{\renewcommand{\currentheadingname}{chapter\xspace}}{}{}
\pretocmd{\section}{\renewcommand{\currentheadingname}{section\xspace}}{}{}
\pretocmd{\subsection}{\renewcommand{\currentheadingname}{subsection\xspace}}{}{}
\pretocmd{\subsubsection}{\renewcommand{\currentheadingname}{subsubsection\xspace}}{}{}
\pretocmd{\paragraph}{\renewcommand{\currentheadingname}{paragraph\xspace}}{}{}
\pretocmd{\subparagraph}{\renewcommand{\currentheadingname}{subparagraph\xspace}}{}{}

\begin{document}
\chapter{SomeChapter}
Greetings from this \currentheadingname

\section{SomeSection}
In this \currentheadingname \dots

\subsection{SomeSubsection}
This \currentheadingname is about \dots
\end{document} 

Output:

enter image description here

In regards of babel, it unfortunately defines only \partname and \chaptername, so nothing to do for \section and so on.


EDIT

A solution for "international use".

If you load hyperref, the following commands get defined: \partautorefname, \chapterautorefname, \sectionautorefname, etc. They change depending on the language loaded by babel (although not all babel languages are defined, only afrikaans, english, french, german, italian, magyar, portuges, russian, spanish, vietnamese).

So you can change the above definitions to

\pretocmd{\part}{\renewcommand{\currentheadingname}{\MakeLowercase{\partautorefname}\xspace}}{}{}
\pretocmd{\chapter}{\renewcommand{\currentheadingname}{\MakeLowercase{\chapterautorefname}\xspace}}{}{}
\pretocmd{\section}{\renewcommand{\currentheadingname}{\sectionautorefname\xspace}}{}{}
\pretocmd{\subsection}{\renewcommand{\currentheadingname}{\subsectionautorefname\xspace}}{}{}
\pretocmd{\subsubsection}{\renewcommand{\currentheadingname}{\subsubsectionautorefname\xspace}}{}{}
\pretocmd{\paragraph}{\renewcommand{\currentheadingname}{\paragraphautorefname\xspace}}{}{}
\pretocmd{\subparagraph}{\renewcommand{\currentheadingname}{\subparagraphautorefname\xspace}}{}{}

Note \MakeLowercase for \partautorefname and \chapterautorefname, since they are defined capitalised.

The following MWE shows how to obtain current heading names in Italian:

\documentclass{book}
\usepackage[english,italian]{babel}
\usepackage{etoolbox}
\usepackage{xspace}
\usepackage{hyperref}

\newcommand{\currentheadingname}{}

\pretocmd{\part}{\renewcommand{\currentheadingname}{\MakeLowercase{\partautorefname}\xspace}}{}{}
\pretocmd{\chapter}{\renewcommand{\currentheadingname}{\MakeLowercase{\chapterautorefname}\xspace}}{}{}
\pretocmd{\section}{\renewcommand{\currentheadingname}{\sectionautorefname\xspace}}{}{}
\pretocmd{\subsection}{\renewcommand{\currentheadingname}{\subsectionautorefname\xspace}}{}{}
\pretocmd{\subsubsection}{\renewcommand{\currentheadingname}{\subsubsectionautorefname\xspace}}{}{}
\pretocmd{\paragraph}{\renewcommand{\currentheadingname}{\paragraphautorefname\xspace}}{}{}
\pretocmd{\subparagraph}{\renewcommand{\currentheadingname}{\subparagraphautorefname\xspace}}{}{}

\begin{document}
\chapter{SomeChapter}
Greetings from this ``\currentheadingname''

\section{SomeSection}
In this ``\currentheadingname'' \dots

\subsection{SomeSubsection}
This ``\currentheadingname'' is about \dots

\subsubsection{SomeSubsubsection}
This is a ``\currentheadingname''.

\paragraph{SomeParagraph}
This is a ``\currentheadingname''.

\subparagraph{SomeSubparagraph}
This is a ``\currentheadingname''.
\end{document} 

Output:

enter image description here

  • +1, although I'd rather not use \xspace. – lockstep Jan 28 '14 at 17:20
  • +1, thanks for the quick and detailed answer! it's kind of a basic feature to me and i wonder, that it doesn't exist yet. Hopefully babel will add some \sectionname command in future to grab the localized names. – Andreas M Jan 29 '14 at 9:40

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.