[Tex/LaTex] LaTeX-internal definition of \thesection


I am using a redefinition of the equation-numbering as in the MWE below.




\chapter{First chapter}

\section{First section}

I noticed that \thesection gives "current chapter number"."current section number". Respectively all minor structure level numbers have their major level numbers before their own number. In fact that is not a problem for me; this MWE achieves exactly the behaviour I want to have. I just want to understand why LaTeX does this. Is there any advantage in it (except of shortening this redefinition command)? And what are the disadvantages?

P.S.: How can I call then the counter for just my current section?

Best Answer

The last question how to use the section counter in conjunction with the equation counter:


will print the equation number with the section number as prefix only, not the full nine yards with chapter.section.X.

Now the more generic answer about the \thefoo - like commands.

In principle such commands like \thesection are recursively defined!

Depending on the real setup (with chngcntr's \counterwithin etc. commands) and the underlying class the structure is usually


In this case, foolevelone would be chapter, fooleveltwo is section and foolevelthree will be subsection etc.

The precise setup will differ most likely in the usage of \arabic, \alph, \Alph, \roman,\Roman mainly.

In order to get the current section number, just use \arabic{section} or \number\value{section}

The advantage of this numbering scheme is a definition mapping which equation/section/figure etc. belongs to which chapter or structure level.

The disadvantage is that you can't use something like


in most cases, because \thesection may have non-numbers (like periods or roman figures) in its output.

Some more details -- from latex.ltx

\newcounter{foo} will call \@definecounter effectively, here is its definition:

\def\@definecounter#1{\expandafter\newcount\csname c@#1\endcsname
     \global\expandafter\let\csname cl@#1\endcsname\@empty
     \global\expandafter\let\csname p@#1\endcsname\@empty
     \gdef\csname the#1\expandafter\endcsname\expandafter
          {\expandafter\@arabic\csname c@#1\endcsname}}

At the end the \gdef\.... will define \thefoo being a command that prints the counter value with arabic numbers -- this is the default setup, so right from the start, any \the... (as long as related to counters) will print arabic numbers.

The final output behaviour is defined by the class (in most cases), like here in article.cls

\newcounter {part}
\newcounter {section}
\newcounter {subsection}[section]
\newcounter {subsubsection}[subsection]
\newcounter {paragraph}[subsubsection]
\newcounter {subparagraph}[paragraph]
\renewcommand \thepart {\@Roman\c@part}
\renewcommand \thesection {\@arabic\c@section}
\renewcommand\thesubsection   {\thesection.\@arabic\c@subsection}
\renewcommand\theparagraph    {\thesubsubsection.\@arabic\c@paragraph}
\renewcommand\thesubparagraph {\theparagraph.\@arabic\c@subparagraph}

As can be seen by the row of \renewcommand{\the...} macros, the commands are recursively defined, where \@arabic\c@foo is the internal representation of \arabic{foo} actual (\c@foo the 'real' counter macro name)

Related Question