Counters

From ConTeXt wiki

< Main Page | Inside ConTeXt >

ConTeXt provides a higher layer of API to access various counters. For example, to set the page number to a certain value use

 \setnumber[page][1]

Other useful commands for working with counters are

    \resetnumber    [page]
    \incrementnumber[page]
    \decrementnumber[page]
    \savenumber     [page]
    \restorenumber  [page]
    \getnumber      [page]

Coupling Counters

Two counters can be coupled by

 \definenumber[one]
 \definenumber[two][one]

For example

\definenumber[one]
\definenumber[two][one]


Default value of counter one: \getnumber[one]

We increment counter two
\incrementnumber[two]

New value of counter one: \getnumber[one]

By default counters are reset with each new chapter. To have a counter which isn’t reset one needs “way=bytext" and to remove the chapter number from the \getnumber result one has to add “prefix=no”.

An example definition is

\definenumber[examplecounter][way=bytext,prefix=no]

Testing the Value of a Counter

If you need to test the value of a counter, use \rawcountervalue, not \getnumber:

\definenumber[mynumber][]
\setnumber[mynumber][42]
\doifelse{\getnumber[mynumber]}{42}{
 Strange, \type{\getnumber} worked.
}{
 Using \type{\getnumber} does not work.
}

\doifelse{\rawcountervalue[mynumber]}{42}{
 Ok, the test with \type{\rawcountervalue} worked.
}{
 Strange, \type{\rawcountervalue} failed.
}

Forcing the Reset of a Counter

Due to a current bug[1], counters are reset by chapter, page, etc. only when they are incremented. To force the reset, you can increment and then decrement a counter before using it.

\definecounter[mycounter][way=bypage]
\setcounter[mycounter][10]
\rawcountervalue[mycounter]
\page                       % new page, mycounter should be reset
\rawcountervalue[mycounter] % no, it's not reset: you still get '10'

\incrementcounter[mycounter]\decrementcounter[mycounter]
\rawcountervalue[mycounter] % now you should get '0' because it has been reset

\definenumber or \definecounter ?

In ConTeXt MkIV \definecounter, \incrementcounter, etc. seem to be preferred to \definenumber, \incrementnumber, etc.

Names of Commonly Used Counters

  • page for pagenumbers
  • formula for formulanumbers
  • table for tables
  • figure for figures