[Tex/LaTex] How to reference the current (sub)section number

countersmacrosnumberingsectioning

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:

output of the example code given above

Any help would be appreciated. Thanks!

Best Answer

You mean something like this? We simply use the fact that the "lower" section number is always 0 when that sectioning level is not used at the moment.

\documentclass{report}

\makeatletter
\newcommand{\getCurrentSectionNumber}{%
  \ifnum\c@section=0 %
  \thechapter
  \else
  \ifnum\c@subsection=0 %
  \thesection
  \else
  \ifnum\c@subsubsection=0 %
  \thesubsection
  \else
  \thesubsubsection
  \fi
  \fi
  \fi
}

\makeatother

\begin{document}

\chapter{My Example}
A: Current number is \getCurrentSectionNumber %1

\section{Section One-point-One}
    B: Current number is \getCurrentSectionNumber % 1.1
    \subsection{subsection one-point-one-point-one}
        C: Current number is \getCurrentSectionNumber % 1.1.1
    \subsection{subsection one-point-one-point-two}
        D: Current number is \getCurrentSectionNumber % 1.1.2
\section{Section Two}
    E: Current number is \getCurrentSectionNumber % 1.2
    \subsection{subsection one-point-two-point-one}
        F: Current number is \getCurrentSectionNumber % 1.2.1
    \subsection{subsection one-point-two-point-two}
        G: Current number is \getCurrentSectionNumber % 1.2.2

\end{document}