I want to create a macro that dynamically returns the current chapter, section, subsection (etc.) I'm in. I've created a macro called \getCurrentSectionNumber
that does this, but it lacks the ability to figure out how many levels deep it is and shorten/lengthen the result accordingly. This macro should essentially return the full numbering text of the most recent subsection, section, or chapter where it was invoked.
This macro should produce the following text at the given places.
- A: "1"
- B: "1.1"
- C: "1.1.1"
- D: "1.1.2"
- E: "1.2"
- F: "1.2.1"
- G: "1.2.2"
Note that at A, it prints "1.??" because there is no section defined at that point. Locations C, D, F, and G all omit the subsection.
Example:
\documentclass{report}
\usepackage{etoolbox}
\newcommand\getcurrentref[1]{%
\ifnumequal{\value{#1}}{0}
{??}
{\the\value{#1}}%
}
\newcommand{\getCurrentSectionNumber}{\getcurrentref{chapter}.\getcurrentref{section}}
\begin{document}
\chapter{My Example}
A: Current number is \getCurrentSectionNumber
\section{Section One-point-One}
B: Current number is \getCurrentSectionNumber
\subsection{subsection one-point-one-point-one}
C: Current number is \getCurrentSectionNumber
\subsection{subsection one-point-one-point-two}
D: Current number is \getCurrentSectionNumber
\section{Section Two}
E: Current number is \getCurrentSectionNumber
\subsection{subsection one-point-two-point-one}
F: Current number is \getCurrentSectionNumber
\subsection{subsection one-point-two-point-two}
G: Current number is \getCurrentSectionNumber
\end{document}
Example's Output:
Any help would be appreciated. Thanks!