Difference between revisions of "Counters"

From Wiki
Jump to navigation Jump to search
(change "number" to "counter")
Line 4: Line 4:
  
 
<texcode>
 
<texcode>
  \setnumber[page][1]
+
  \setcounter[userpage][1]
 
</texcode>
 
</texcode>
  
Line 10: Line 10:
  
 
<texcode>
 
<texcode>
     \resetnumber   [page]
+
     \resetcounter   [page]
     \incrementnumber[page]
+
     \incrementcounter[page]
     \decrementnumber[page]
+
     \decrementcounter[page]
     \savenumber     [page]
+
     \savecounter     [page]
     \restorenumber [page]
+
     \restorecounter [page]
     \getnumber     [page]
+
     \getcounter     [page]
 
</texcode>
 
</texcode>
  
Line 23: Line 23:
  
 
<texcode>
 
<texcode>
  \definenumber[one]
+
  \definecounter[one]
  \definenumber[two][one]
+
  \definecounter[two][one]
 
</texcode>
 
</texcode>
  
Line 30: Line 30:
  
 
<context source="yes">
 
<context source="yes">
\definenumber[one]
+
\definecounter[one]
\definenumber[two][one]
+
\definecounter[two][one]
  
  
Default value of counter one: \getnumber[one]
+
Default value of counter one: \getcounter[one]
  
 
We increment counter two
 
We increment counter two
\incrementnumber[two]
+
\incrementcounter[two]
  
New value of counter one: \getnumber[one]
+
New value of counter one: \getcounter[one]
  
 
</context>
 
</context>
  
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”.
+
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 \getcounter result one has to add “prefix=no”.
  
 
An example definition is
 
An example definition is
 
<texcode>
 
<texcode>
\definenumber[examplecounter][way=bytext,prefix=no]
+
\definecounter[examplecounter][way=bytext,prefix=no]
 
</texcode>
 
</texcode>
  
 
== Testing the Value of a Counter ==
 
== Testing the Value of a Counter ==
If you need to test the value of a counter, use \rawcountervalue, not \getnumber:
+
If you need to test the value of a counter, use \rawcountervalue, not \getcounter:
 
<texcode>
 
<texcode>
\definenumber[mynumber][]
+
\definecounter[mynumber][]
\setnumber[mynumber][42]
+
\setcounter[mynumber][42]
\doifelse{\getnumber[mynumber]}{42}{
+
\doifelse{\getcounter[mynumber]}{42}{
  Strange, \type{\getnumber} worked.
+
  Strange, \type{\getcounter} worked.
 
}{
 
}{
  Using \type{\getnumber} does not work.
+
  Using \type{\getcounter} does not work.
 
}
 
}
  
Line 69: Line 69:
  
 
== Forcing the Reset of a Counter ==
 
== Forcing the Reset of a Counter ==
Due to a current bug[http://www.mail-archive.com/ntg-context%40ntg.nl/msg78847.html], counters are reset by chapter, page, etc. only when they are incremented.
+
Due to a current bug [http://www.mail-archive.com/ntg-context%40ntg.nl/msg78847.html], 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.
 
To force the reset, you can increment and then decrement a counter before using it.
 
<texcode>
 
<texcode>
Line 83: Line 83:
  
 
== \definenumber or \definecounter ? ==
 
== \definenumber or \definecounter ? ==
In ConTeXt MkIV {{cmd|definecounter}}, {{cmd|incrementcounter}}, etc. seem to be preferred to {{cmd|definenumber}}, {{cmd|incrementnumber}}, etc.
+
{{cmd|definenumber}} etc. are obsolete aliases for {{cmd|definecounter}} etc.
  
 
== Names of Commonly Used Counters ==
 
== Names of Commonly Used Counters ==
  
* <code>page</code> for pagenumbers
+
* <code>userpage</code> for visible pagenumbers
* <code>formula</code> for formulanumbers
+
* <code>page</code> for internal pagenumbers
* <code>table</code> for tables
+
* <code>formula</code> for formula numbers
* <code>figure</code> for figures
+
* <code>table</code> for table numbers
 +
* <code>figure</code> for figure numbers
  
  
 
[[Category:Programming and Databases]]
 
[[Category:Programming and Databases]]
 
[[Category:Tools]]
 
[[Category:Tools]]

Revision as of 21:50, 28 October 2023

< 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

 \setcounter[userpage][1]

Other useful commands for working with counters are

    \resetcounter    [page]
    \incrementcounter[page]
    \decrementcounter[page]
    \savecounter     [page]
    \restorecounter  [page]
    \getcounter      [page]

Coupling Counters

Two counters can be coupled by

 \definecounter[one]
 \definecounter[two][one]

For example

\definecounter[one]
\definecounter[two][one]


Default value of counter one: \getcounter[one]

We increment counter two
\incrementcounter[two]

New value of counter one: \getcounter[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 \getcounter result one has to add “prefix=no”.

An example definition is

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

Testing the Value of a Counter

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

\definecounter[mynumber][]
\setcounter[mynumber][42]
\doifelse{\getcounter[mynumber]}{42}{
 Strange, \type{\getcounter} worked.
}{
 Using \type{\getcounter} 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 ?

\definenumber etc. are obsolete aliases for \definecounter etc.

Names of Commonly Used Counters

  • userpage for visible pagenumbers
  • page for internal pagenumbers
  • formula for formula numbers
  • table for table numbers
  • figure for figure numbers