# Access label of chapter / section / … based on nesting depth

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}

\section{SomeSection}

\subsection{SomeSubsection}
\end{document}


The desired result looks like:

1. SomeChapter

Greetings from this chapter

1.1. SomeSection

In this section ...

1.1.1. SomeSubsection

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

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


Complete MWE:

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

\begin{document}
\chapter{SomeChapter}

\section{SomeSection}

\subsection{SomeSubsection}
\end{document}


Output:

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


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}

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

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