Getting name of current structure level

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}

enter image description here


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:

Page 3

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}
\zref@addprop{main}{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
\begin{equation}
  \zlabel{eq}
  E=mc^2
\end{equation}
\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:

Last page


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}

Tags:

Sectioning