# Get reference to “current” section

I'd like to write a command to get a reference to the "current" section provided (the term is all wrong, but the example will clarify).

The idea is something along the lines of:

\getcurrentref{chapter}


or

\getcurrentref{subsubsection}


and so on.

A section is considered "current" at a given point if said point is within the "scope" of said section.

Example:

\chapter{A}
% point (1)
\section{A.A}
% point (2)
\section{A.B}
% point (3)
\subsection{A.B.A}
% point (4)


the only "living" section at point (1) is chapter, the only "living" sections at points (2) and (3) are chapter and section, and the only "living" sections at point (4) are chapter, section and subsection.

I hope I could explain myself (it's tricky).

PS: in case of trying to get the reference to a non-"living" section, the command should return ?? (this is just to have a standard graceful way of saying "no such section alive").

-

Assuming that subordinate "sections" are reset to 0 by larger ones (so \chapter resets \section and so on), you just have to look at the counters chapter, section, etc., and check whether they're nonzero:

\documentclass{book} % For {chapter}
\usepackage{etoolbox}

\newcommand\getcurrentref[1]{%
\ifnumequal{\value{#1}}{0}
{??}
{\the\value{#1}}%
}
\begin{document}
Chapter: \getcurrentref{chapter}

\chapter{I}
\tracingmacros=1\tracingonline=1
Chapter: \getcurrentref{chapter}\\
\tracingmacros=0
Section: \getcurrentref{section}

\section{i}
Chapter: \getcurrentref{chapter}\\
Section: \getcurrentref{section}\\
Subsection: \getcurrentref{subsection}

\section{ii}
Section: \getcurrentref{section}

\subsection{a}
Subsection: \getcurrentref{subsection}
\end{document}


This should be expandable, by the way. I only use etoolbox for the handy conditional, which in a more complicated situation might be simpler than a TeX primitive (e.g. with respect to nesting or expansion order). Here, it's optional.

Edit: I now use \value{#1} rather than \csname c@#1\endcsname for the number, as one would like. As egreg says, it is expandable (actually, it expands just to this). However, it does expand rather unpredictably in the sense that it is equivalent to a count register, not to an actual number, and so the part of the macro that actually prints the value needs a \the to avoid weird errors. In my case, I was seeing "Missing number inserted" in the expansion of \\, which (upon tracing) appeared to be related to \protect.

-
You can say \ifnumeq{\value{section}}{0}{true}{false}; \ifnumeq will eventually do \ifnum\value{section}=0 which is perfectly legal; \value is expandable, since it is a macro and \ifnum will trigger its expansion, which is \csname c@section\endcsname; \ifnum will continue and find \c@section which is a legal (unexpandable) token after \ifnum. The fact that \c@section is unexpandable has nothing to do with the complete expandability of the macro \getcurrentref: it will disappear when TeX performs the conditional test. –  egreg Nov 13 '11 at 20:48
@egreg: really? That's bizarre, since I was getting some error involving \protect when I tried to do just that. I wonder why. –  Ryan Reich Nov 13 '11 at 21:30
I get no error if I plug in \value{#1} instead of \the\csname c@#1\endcsname. By the way also \the is useless: when TeX is looking for a <number>, a count register is good. –  egreg Nov 13 '11 at 21:34
@egreg: Must have been a typo, I guess? Anyway, thanks for the tip: I am sort of fuzzy on what role registers play. –  Ryan Reich Nov 13 '11 at 21:38
Well, it's necessary \the for printing the value of a counter; better \arabic{cnt} than \the\value{cnt}, though, from a LaTeXian point of view. –  egreg Nov 14 '11 at 0:34

Use \thechapter, \thesection, etc:

\documentclass{report}
\pagestyle{empty}
\begin{document}

\chapter{First}
\label{chap:first}

\section{First}
\label{sec:first}

This is the chapter number~\thechapter, section~\thesection.

\section{Second}
\label{sec:second}

This is the chapter number~\thechapter, section~\thesection.
\end{document}


-
\newcommand{\getcurrentref}[1]{%