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:




and so on.

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


  % point (1)
    % point (2)
    % point (3)
      % 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").

3 Answers 3


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}

 Chapter: \getcurrentref{chapter}

 Chapter: \getcurrentref{chapter}\\
 Section: \getcurrentref{section}

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

 Section: \getcurrentref{section}

 Subsection: \getcurrentref{subsection}

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
    Commented Nov 13, 2011 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
    Commented Nov 13, 2011 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
    Commented Nov 13, 2011 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
    Commented Nov 13, 2011 at 21:38
  • 1
    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
    Commented Nov 14, 2011 at 0:34

Use \thechapter, \thesection, etc:




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


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

enter image description here


There's no notion of "current" level, unless one plugs in one into the sectioning commands. The best one can do is to see if the related counter is zero or not:

  \ifnum\value{#1}=0 ??\else\csname the#1\endcsname\fi

No packages. This is actually the same as Ryan's answer, but reduced to the essential.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .