# Getting name of current structure level

In a document I'm currently working on, we often change the hierarchy of chapters/sections etc. So within the text I would like to reference the current level of hierarchy in a sentence like

In this chapter we will talk about ...


As the structure changes often, I would like to add the name of the current hierarchy level ("chapter" in the example above) to some command, so that it adapts automatically, when we change the structure again.

I could not find, however, a command for this. Most commands are focused towards getting either the current section's title or the reference to some other section including the section number.

So given this example, what would I need to write as \someCommand to get the current chapter/section/subsection/etc. name?

\documentclass[a4paper]{memoir}
\begin{document}

\chapter{Example-Chapter}

Current level: \someCommand - should be "chapter" and not "Example-Chapter"

\section{Example-Section}

Current level: \someCommand - should be "section" and not "Example-Section"

\end{document}


You can use the fact that \chapter resets the linked counters and the same happens for \section.

\documentclass[a4paper]{memoir}

\newcommand{\someCommand}{%
\ifnum\value{section}=0
chapter%
\else
\ifnum\value{subsection}=0
section%
\else
subsection%
\fi
\fi
}
\setsecnumdepth{subsection}

\begin{document}

\chapter{Example chapter}

Current level: \someCommand, should be chapter''

\section{Example section}

Current level: \someCommand, should be section''

\subsection{Example subsection}

Current level: \someCommand, should be subsection''

\section{Example section}

Current level: \someCommand, should be section''

\end{document}


This version also works for unnumbered structure levels. The structure macro definitions \chapter, \section etc. are extended at the begin with the definition of the structure level name and number.

\documentclass{report}
\setcounter{secnumdepth}{-1}

\usepackage{etoolbox}
\newcommand*{\tmp}[3]{%
\pretocmd#1{%
\gdef\StructureLevelNumber{#2}%
\gdef\StructureLevelName{#3}%
}{}{%
\errmessage{Cannot patch \string#1}%
}%
}
\tmp\part{-1}{part}
\tmp\chapter{0}{chapter}
\tmp\section{1}{section}
\tmp\subsection{2}{subsection}
\tmp\subsubsection{3}{subsubsection}
\tmp\paragraph{4}{paragraph}
\tmp\subparagraph{5}{subparagraph}

\newcommand*{\test}{%
We are in structure level \StructureLevelName'' %
(\StructureLevelNumber).%
}

\begin{document}
\part{Part title}
\test
\chapter{Chapter title}
\test
\section{Section title}
\test
\subsection{Subsection title}
\test
\subsubsection{Subsubsection title}
\test
\paragraph{Paragraph title.}
\test
\subparagraph{Subparagraph title.}
\test
\end{document}


Third page:

# References with structure levels

Package zref supports the definition of new properties that can be stored in labels (\zlabel, \zref@label...) and referenced (\zref, \zref@extract...).

The following example only used integer numbers to identify the structure levels and generates the name for the structure level via macro \StructureLevelName.

\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{color}
\setcounter{secnumdepth}{-1}

\usepackage{etoolbox}
\newcommand*{\tmp}[2]{%
\pretocmd#1{%
\gdef\StructureLevel{#2}%
}{}{%
\errmessage{Cannot patch \string#1}%
}%
}
\tmp\part{-1}
\tmp\chapter{0}
\tmp\section{1}
\tmp\subsection{2}
\tmp\subsubsection{3}
\tmp\paragraph{4}
\tmp\subparagraph{5}
\newcommand*{\StructureLevel}{-2}

\newcommand*{\StructureLevelName}[1]{%
\ifcase\numexpr(#1)\relax
chapter%
\or
section%
\or
subsection%
\or
subsubsection%
\or
paragraph%
\or
subparagraph%
\else
\ifnum\numexpr(#1)\relax=-1 %
part%
\else
unknown%
\fi
\fi
}

\usepackage{zref-base, zref-titleref, zref-user}
\makeatletter
\zref@newprop{StructureLevel}[-2]{\StructureLevel}
\newcommand*{\zrefStructureLevelName}[1]{%
\zref@refused{#1}%
\StructureLevelName{%
\zref@extractdefault{#1}{StructureLevel}{-2}%
}%
}
\makeatother

\newcommand*{\test}{%
We are in structure level
\textcolor{red}{\StructureLevelName{\StructureLevel}}'' %
(\textcolor{blue}{\StructureLevel}).%
}
\newcommand*{\testref}[1]{%
\texttt{\textbackslash zlabel\{#1\}}:
Structure element is \textcolor{red}{\zrefStructureLevelName{#1}}'',
title: \textcolor{blue}{\ztitleref{#1}}\par
}

\begin{document}
\part{Part title}
\zlabel{a}
\test
\chapter{Chapter title}
\zlabel{b}
\test
\section{Section title}
\zlabel{c}
\test
\subsection{Subsection title}
\zlabel{d}
\test
$$\zlabel{eq} E=mc^2$$
\subsubsection{Subsubsection title}
\zlabel{e}
\test
\paragraph{Paragraph title.}
\zlabel{f}
\test
\subparagraph{Subparagraph title.}
\zlabel{g}
\test
\newpage
\testref{a}
\testref{b}
\testref{c}
\testref{d}
\testref{e}
\testref{f}
\testref{g}
\texttt{\textbackslash zlabel\{eq\}}:
Equation \zref{eq} is in structure element
\textcolor{red}{\zrefStructureLevelName{eq}}''.
\end{document}


Last page with the references:

While not being a unique new answer, I wanted to add this for reference:

After experimenting a little with the answer of @egreg, I also removed the "static" sectioning labels and replaced them by the ones hyperref uses, so it stays consistent with the use of \autoref in the document.

\documentclass[a4paper]{memoir}
\usepackage{hyperref}

\newcommand{\structuringLevel}{%
\ifnum\value{section}=0
\chapterautorefname%
\else
\ifnum\value{subsection}=0
\sectionautorefname%
\else
\subsectionautorefname%
\fi
\fi
}
\setsecnumdepth{subsection}

\begin{document}

\chapter{Example chapter}

Current level: \someCommand, should be chapter''

\section{Example section}

Current level: \someCommand, should be section''

\subsection{Example subsection}

Current level: \someCommand, should be subsection''

\section{Example section}

Current level: \someCommand, should be section''

\end{document}