Difference between revisions of "Counters"
(a workaround of the "reset counter" bug; the names of the counters' commands) |
(some restructuring and cleanup) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 4: | Line 4: | ||
<texcode> | <texcode> | ||
− | \ | + | \setcounter[userpage][1] |
</texcode> | </texcode> | ||
Line 10: | Line 10: | ||
<texcode> | <texcode> | ||
− | \ | + | \resetcounter [page] |
− | \ | + | \incrementcounter[page] |
− | \ | + | \decrementcounter[page] |
− | \ | + | \savecounter [page] |
− | \ | + | \restorecounter [page] |
− | \ | + | %\getcounter [page] |
+ | \rawcountervalue [page] | ||
</texcode> | </texcode> | ||
+ | |||
+ | == Names of Commonly Used Counters == | ||
+ | |||
+ | * <code>userpage</code> for visible pagenumbers | ||
+ | * <code>page</code> for internal pagenumbers | ||
+ | * <code>formula</code> for formula numbers | ||
+ | * <code>table</code> for table numbers | ||
+ | * <code>figure</code> for figure numbers | ||
+ | |||
+ | |||
+ | == Testing the Value of a Counter == | ||
+ | |||
+ | If you need to test the value of a counter, use \rawcountervalue, not \getcounter: | ||
+ | |||
+ | <context source="yes" text="results in:"> | ||
+ | \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. | ||
+ | } | ||
+ | </context> | ||
+ | |||
+ | In LMTX, {{cmd|getcounter}} seems to be generally defunct. | ||
== Coupling Counters == | == Coupling Counters == | ||
Line 23: | Line 55: | ||
<texcode> | <texcode> | ||
− | \ | + | \definecounter[one] |
− | \ | + | \definecounter[two][one] |
</texcode> | </texcode> | ||
Line 30: | Line 62: | ||
<context source="yes"> | <context source="yes"> | ||
− | \ | + | \definecounter[one] |
− | \ | + | \definecounter[two][one] |
− | Default value of counter one: \ | + | Default value of counter one: \rawcountervalue[one] |
We increment counter two | We increment counter two | ||
− | \ | + | \incrementcounter[two] |
− | New value of counter one: \ | + | New value of counter one: \rawcountervalue[one] |
</context> | </context> | ||
− | By default counters are reset with each new chapter. To have a counter which isn’t reset | + | By default counters are reset with each new chapter. To have a counter which isn’t reset you need `way=bytext`, and to remove the chapter number from the \getcounter result you have to add `prefix=no`. |
− | + | Example definition: | |
<texcode> | <texcode> | ||
− | \ | + | \definecounter[examplecounter][way=bytext,prefix=no] |
</texcode> | </texcode> | ||
− | == | + | == Outdated information == |
− | + | ||
− | + | === Forcing the Reset of a Counter === | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | In [http://www.mail-archive.com/ntg-context%40ntg.nl/msg78847.html 2015], there was a bug that counters were reset (by chapter, page, etc.) only when they were 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 82: | Line 100: | ||
</texcode> | </texcode> | ||
− | == \definenumber or \definecounter ? == | + | === \definenumber or \definecounter ? === |
− | + | {{cmd|definenumber}} etc. are obsolete aliases for {{cmd|definecounter}} etc. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | [[Category: | + | [[Category:Programming and Databases]] |
− | [[Category: | + | [[Category:Tools]] |
Latest revision as of 06:16, 5 April 2024
< 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] \rawcountervalue [page]
Names of Commonly Used Counters
userpage
for visible pagenumberspage
for internal pagenumbersformula
for formula numberstable
for table numbersfigure
for figure numbers
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. }
results in:
In LMTX, \getcounter seems to be generally defunct.
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: \rawcountervalue[one] We increment counter two \incrementcounter[two] New value of counter one: \rawcountervalue[one]
By default counters are reset with each new chapter. To have a counter which isn’t reset you need way=bytext
, and to remove the chapter number from the \getcounter result you have to add prefix=no
.
Example definition:
\definecounter[examplecounter][way=bytext,prefix=no]
Outdated information
Forcing the Reset of a Counter
In 2015, there was a bug that counters were reset (by chapter, page, etc.) only when they were 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.