How to display the chapter number in the headers in ConTeXt?

The normal way to get the chapter number in the headers is to use \getmarking[chapternumber].

\setupheadertexts[{\getmarking[chapternumber]--\getmarking[chapter]}]

\starttext

\startchapter[title={First chapter}, marking={Mark for first chapter}]
  \input knuth
\stopchapter

\startchapter[title={Second chapter}, marking={Mark for second chapter}]
  \input knuth
\stopchapter

\stoptext

which gives

enter image description here


You can access the first, present, and final values of section counters for the currently active structure component by their name using the command \somenamedheadnumber.

\somenamedheadnumber takes two mandatory arguments:

  1. the name of a structure element, e.~g. section, chapter, and
  2. one of the identifiers first, current, last that determine the value to be retrieved.

Examples:

%% the initial value of the chapter counter
\somenamedheadnumber{chapter}{first}

%% the number of the final section in the current chapter
\somenamedheadnumber{section}{last}

%% the number of the currently active subsubsection;
%% in the page header this will retrieve the number of the
%% structural last active on the current page
\somenamedheadnumber{subsubsection}{current}

As a shorthand, \namedheadnumber{<name>} is equivalent to \somenamedheadnumber{<name>}{current}.

A more extensive example showing the various getters for structure counters in action:

\setuppagenumbering [location=footer,alternative=doublesided]

\startsetups header.outer
  Chapter
  \space
  \namedheadnumber{chapter}
  \space of \space
  \somenamedheadnumber{chapter}{last}
  \space --- \space
  {\cap\getmarking[chapter]}
\stopsetups

\startsetups header.inner
  Section
  \space
  \namedheadnumber{section}
  \space of \space
  \somenamedheadnumber{section}{last}
  \space --- \space
  {\italic\getmarking[section]}
\stopsetups

\setupheadertexts
  [\setups{header.outer}]
  [\setups{header.inner}]

\starttext
  \getrandomnumber\i25
  \completecontent\page
  \dorecurse{\i}{
    \getrandomnumber\j27
    \startchapter[title=foo]
      \input knuth
      \dorecurse{\j}{
        \startsection[title=bar]
          \input ward\par\input dawkins
        \stopsection
      }
    \stopchapter
  }
\stoptext