Expand description
The numbering feature: counters, counter styles, and the number that
every numbered reference point of a document carries; and the functions
that write a counter value as content, which a caller may use without the
feature.
A counter (CounterDecl) is a dotted name under which things are
counted in document order, declared by a feature together with the counter
it is numbered within; its value at a reference point is a number with
optional sub-numbers (CounterValue). A counter style
(CounterStyleDecl) says how a counter value is shown: (3), Eq.~(3),
2.13, Eqs.~(1–3 and 7). A reference point
(RefPoint) is one place in a document
that can be referred to, numbered, or given an anchor; a point that is to
carry a number states its number source
(NumberSource): the counter it
counts in and the counter style its number is shown in. The concepts
overview presents these terms together (counters and counter
styles;
reference points, labels and
anchors),
and the numbering guide describes the mechanism
for the author of a document and of an extension.
§What the feature provides to an author of documents
The numbering feature declares no command and no environment. It numbers
the constructs that other features declare as numbered — headings, the
lines of display mathematics, theorems, list items, floats, and the
footnotes and other notes of the xnotes
feature — and the number such a construct shows, beside itself
and in every reference to it, is written from the counter value this
feature records.
A document changes the numbering through its numbering options
(NumberingOptions). For each counter, the options say which counter it
is numbered within (the equations within the sections, so that an equation
reads (2.13)), or that it is numbered within nothing, and the value the
counter has before it is first advanced (the value 0 for the sections
makes a subsection written before the first section read 0.1). Whether a
construct is numbered at all is decided by the feature that owns the
construct (the headings feature’s numbering depth, the mathematics
feature’s switch for numbered equations), never by this feature.
§What the feature provides to the pipeline
The numbering feature (Numbering, an
FlmFeature) registers the following
into the registry:
-
Five service slots, through which other features declare what is numbered and how. A service slot is the declaration, by the feature that owns an interaction, of a dotted name, the type of the values the slot takes and its arity (
services); each of these five takes any number of values, in contribution order:- the service slot
flm.numbering.counters(COUNTERS_SLOT) takes the counters (CounterDecl); flm.numbering.styles(STYLES_SLOT) takes the counter styles (CounterStyleDecl);flm.numbering.within-joiners(WITHIN_JOINERS_SLOT) takes the within-joiners registered by name (WithinJoinerDecl);flm.numbering.number-writers(NUMBER_WRITERS_SLOT) takes the number writers (NumberWriterDecl);flm.numbering.parent-sets(PARENT_SETS_SLOT) takes the parent sets (ParentSetDecl).
A feature contributes to these service slots with
FeatureBuilder::peer_slot, a peer declaration (PeerName) of the service slot: in a registry without the numbering feature, the value is dropped with one Note and the contributing feature still registers (services and service slots). - the service slot
-
The stage
NUMBER(flm.numbering.number), the only stage of the library that advances a counter. It runs once per document: it reads every reference point of the document in document order from the tableflm.refpoints(REFPOINTS), reads the numbering options, and writes the counter table. Its behavior is described below, under “What the stage does with a document”. -
The counter table
COUNTERS(flm.counters, aCounters). For every reference point the stage read, the table holds aCounterRecord(the counter, its value, the counters it is numbered within with their values, the counter style) or nothing for a point that is not numbered. A record holds a value and never text: a lowering rule writes the number where it shows it, withCounters::format,Counters::format_in_styleorCounters::format_many, and splices the resultingNumberSegments withLowerer::splice. The table also carries every declared counter style, compiled (Counters::style). -
The option type
NumberingOptions, bound on the document; a render request may set it again, and its value then replaces the document’s for that render (Bind::Either). -
The configuration
NumberingConfig, the counter styles a host replaces by name. It is the feature’s configuration, fixed when the registry is built (settings, options and configuration), and it is how a host changes what a number looks like without writing a feature. The settings keys are the fields ofNumberingConfig(styles) and ofNumberingOptions(counters) (settings).
The feature registers no element and no lowering rule, so it has no rule on any backend. The number of a construct is shown by the lowering rules of the feature that owns the construct, on every backend, through the counter table.
§Checks made when the registry is built
The feature reads its five service slots once every feature has registered, and each of the following mistakes fails the registry build, with a message that gives the name concerned:
- two declarations of one counter, counter style, parent set, within-joiner or number writer;
- a counter or a parent set whose name begins with
_(RESERVED_PREFIX); - a counter whose default counter style no feature declares;
- a counter numbered within a counter no feature declares, within a parent set no feature registers, or within an empty list of counters;
- a parent set that holds a counter no feature declares;
- counters numbered within each other, through any of a counter’s parents;
- a counter numbered within another through a within-joiner that no feature registers, a nesting joiner that no feature registers, and a counter style written by a number writer that no feature registers;
- a text of a counter style, of a within-joiner or of a nesting joiner that is not valid FLM or does not compile;
- a within-joiner or a nesting joiner that writes a counter which, under one of the chains of counters the counter may be numbered within, is not in that chain;
- a replacement in
NumberingConfigfor a counter style that no feature declares.
The registry build compiles these texts once, through the feature’s build
hook (LateInit), which runs once the
language that documents are parsed with is built; nothing is parsed again
while a number is written.
§What the stage does with a document
- The options are applied first. Before it reads any reference point,
the stage applies the document’s numbering options to the declared
hierarchy, and reports every option it cannot apply: a counter no
feature declares (
UnknownCounter, a Warning), a parent set no feature registers (UnknownParentSet), a within-joiner that does not compile (BadWithinJoiner) or that no feature registers (UnknownWithinJoiner), counters numbered within each other (HierarchyCycle), and a within-joiner that writes a counter which, under the options, is not always one of the counters the counter is numbered within (NotAnAncestor). - Every reference point that states a number source is numbered. The
stage reads the points in document order and advances the levels of the
counter that each point’s number source lists. A counter has no value
until it is first advanced, or until the options give it an
initialvalue; a point whose number needs an ancestor with no value is not numbered and is reported (ParentUninitialized), once per counter and run. Where a counter has several possible parents (Parents), the parent that applies at a point is the one advanced most recently in document order. - A point that counts in a counter no feature declares is an error
(
UndeclaredCounter), reported once per run and per counter name. A point that gives a counter asks to be numbered, so a registry that cannot number it is in error; a feature whose points may go unnumbered gives its configuration a switch under which those points give no counter. A point whose counter style no feature declares is an error at that point (UndeclaredStyle), because the counter is declared and the counter style asked for is a mistake. A point that would carry a sub-number its counter style has no format for is an error too (UndeclaredSubLevel). - A point may count in a scoped instance of its counter. A scoped
instance is an independent sequence of the counter’s values attached to
an enclosing node, such as a list environment
(
NumberSource::instances). The stage keeps one count per instance; an instance nested in another is numbered within the enclosing instance’s current value and written with the counter’s nesting joiner (CounterDecl::nesting). Instances that do not enclose the point one within the next are an error (InstanceNotEnclosing). - A point may carry a number template of its own
(
NumberSource::number_format). The stage compiles each distinct template text once per run and counter style, and records the point with a counter style derived from the declared one (CounterStyle::with_number_format). A template that does not compile is an error (BadNumberFormat), and the point is numbered in the declared counter style.
Every diagnostic of the stage is an Error unless stated otherwise above.
§Interactions with other features
- The stage requires the table
flm.refpointsof therefpointsfeature: a plan that schedules the stage in a registry without that feature fails. The stage reads nothing of therefsfeature, so a registry without references still numbers. - The
headings,math,lists,theorems,floatsandxnotesfeatures declare their counters and counter styles into the service slots above, and their lowering rules read the counter table to show their numbers. The headings feature also contributes the parent setflm.headings.levels. - The
refsfeature reads the counter table to write the number of a reference’s target, and builds the anchor of a point whose anchor is derived from its number withidentifier_text. - Every text a counter style holds is FLM; number templates in the
Formatsform (NumberTemplate::Formats) and within templates are written in the template language that user definitions use (the template language shared by features).
§Writing a counter value without the feature
The functions that write a counter value hold no counter and count
nothing, so a caller may use them on values of its own. A counter style
declaration is compiled against the built language
(CounterStyleDecl::compile) into a CounterStyle; format()
writes one value with it and format_many writes several. Both return a
list of Segments, each a small tree of content with the position of the
value it links to among the values the caller passed in, or none.
§What a counter style is made of
- The number template (
NumberTemplate, the fieldnumber_format) says how the number itself is written: a text with a place for the number, which writes it in one of the eight number formats (NumberFormat).#{arabic}.writes3.for the number three, and the single-letter template(a)(NumberTemplate::Initial) writes(c). - The sub-levels (
SubLevelDecl) say how the sub-numbers of a value are written and what separates each from what comes before it. The value(3, 2)is written3bin a counter style whose first sub-level is written in letters with no separator. - The prefix words (
PrefixWords) are the word before the number, in one form for a single number and one for several (Eq.~,Eqs.~), and optional named variants (capital:Equation~,Equations~). - The delimiters (
Delimiters) are the texts around the number, such as(and). - The join texts (
JoinDecl) say how several numbers are written as one text, down to the word between the last two.
Every one of these texts is FLM, and every one is parsed once, when the counter style is compiled.
§Numbering within another counter
A counter may be numbered within another: an equation numbered within the
sections is written (2.13). The counter’s Within declares both its
parent (the counter whose advance restarts it) and its within-joiner,
what is written of its ancestors before its own number, in one of four
forms:
- a joining text:
Within::parent("flm.headings.section")writes the section’s number and a full stop, andjoined_byputs another text in place of the full stop; - a within template (
template): FLM text in which#{flm.headings.section}is the section’s number in the section’s own counter style, without word or delimiters. Markup may surround a substitution (\textbf{#{flm.headings.section}}.), and the keys in brackets of#[Roman]{…}and#[bare]{…}write the counter in a namedNumberFormatand without its own ancestors; - nothing (
displayed_bare): the counter still restarts under its parent, and its number is shown without its ancestors’ numbers; - a registered joiner (
joiner_named): aWithinJoinerthat a feature contributed toWITHIN_JOINERS_SLOT, which writes what its code writes.
The parent is one counter (Within::parent), any of several
(Within::parents) or any counter of a named ParentSetDecl
(Within::parent_set). Among several, the one advanced most recently in
document order applies at a point, and a within template writes it as
#{_parent}. no_restart writes the parent’s number
without letting the parent restart the count.
An ancestor is written together with its own ancestors, so a within-joiner
refers only to the counter immediately above: a theorem numbered within the
subsections, which are numbered within the sections, is joined to
flm.headings.subsection alone and is written 2.3.4.
A compiled within-joiner returns a list of Parts, written in order
before the point’s own number, which is never inside a part. Each part
lists the ancestors it shows, so a caller writing several numbers at once
can find which parts two numbers share. A joining text gives one part per
ancestor; a within template gives one part per substitution, with the text
after it; a registered joiner gives what its code gives.
A caller passes the within-joiner and the ancestors together as a
Parent; each ancestor is an Ancestor carrying its own value,
counter style and within-joiner. The ancestors are the whole chain,
outermost first, and the order tells which counters each ancestor may
itself be written within. parts returns the parts alone, without a
number around them.
§The number writer
A counter style says what a number is made of; the number writer puts those texts and numbers together into the content a reader sees. The default writer writes every counter style that asks for no other writer:
- for one value: the word, the opening delimiter, the parts, the number with its sub-numbers, and the closing delimiter;
- for several values of one counter style: the values grouped by the counters they are numbered within, each group sorted, its consecutive values collapsed into ranges, and the runs joined by the counter style’s join texts. The end of a range is written without the parts and without the leading components it shares with the start, unless the range holds exactly two consecutive values or the counter style repeats them, and a range never spans two groups.
A feature may register another writer, as it registers a within-joiner:
NumberWriter is the declaration and CompiledNumberWriter what it
compiles to, with one entry point for one value and one for several values
of one counter style. A NumberWriterDecl goes into
NUMBER_WRITERS_SLOT, and a counter style asks for the writer by name
with with_number_writer. A writer
receives each value as a NumberedValue, with the caller’s
FormatOptions, a WriteCx (the counter style’s data, the library’s
operations, and parts, which runs a value’s
within-joiner when asked and keeps the result for the rest of the call), and
a NumberOut to which it appends content or characters, each marked with
the value it links to or with none. WriteCx::default_one and
WriteCx::default_many write what the default writer would, so a writer
can add to that text instead of repeating it.
§Map of the items
- The feature:
Numbering,feature(),FEATURE,NumberingConfig; the optionsNumberingOptions,CounterOptions,WithinOption; the stage nameNUMBER. - Declarations:
CounterDecl,Within,Parents,WithinJoinerSpec,ParentSetDecl; the service slot namesCOUNTERS_SLOT,STYLES_SLOT,WITHIN_JOINERS_SLOT,NUMBER_WRITERS_SLOT,PARENT_SETS_SLOT;RESERVED_PREFIX,PARENT_PLACEHOLDER. - Counter styles:
CounterStyleDeclwithNumberTemplate,NumberFormat,SubLevelDecl,PrefixWords,PluralWords,Delimiters,JoinDecl; the compiledCounterStyle;StyleError. - Writing values:
CounterValue,format(),format_many,parts,FormatOptions,Segment,Ancestor,Parent,FormatError,identifier_text. - Within-joiners:
WithinJoiner,WithinJoinerDecl,CompiledWithinJoiner,Part,WithinCx,JoinCx. - Number writers:
NumberWriter,NumberWriterDecl,CompiledNumberWriter,NumberedValue,NumberOut,WriteCx,WriterCx,JoinText. - The counter table:
COUNTERS,Counters,CounterRecord,NumberSegment,ManyNumbers. - The stage’s diagnostic conditions:
UnknownCounter,UndeclaredCounter,UndeclaredStyle,UndeclaredSubLevel,ParentUninitialized,UnknownParentSet,NotAnAncestor,HierarchyCycle,BadWithinJoiner,UnknownWithinJoiner,InstanceNotEnclosing,BadNumberFormat,StageFailed.
§Examples
One equation number, with the word and the delimiters the counter style
declares. The text a segment holds is read back here with the source
recomposer (recompose_source),
which writes content back as the FLM it was parsed from.
use flm_core::lang::{build_language, LangInputs};
use flm_core::stdlib::backends::pres::read::recompose_source;
use flm_core::stdlib::features::numbering::{
format, CounterStyleDecl, CounterValue, Delimiters, FormatOptions, NumberTemplate,
PrefixWords, Segment,
};
/// The FLM source a list of segments writes, segment by segment.
fn written(segments: &[Segment]) -> String {
segments
.iter()
.map(|segment| {
let nodes: Vec<_> = segment.content().root().children().iter().collect();
recompose_source(&nodes)
})
.collect()
}
let lang = build_language(LangInputs::new());
let mut decl = CounterStyleDecl::new("example.style.equation");
decl.number_format = NumberTemplate::formats("#{arabic}");
decl.delimiters = Delimiters::new("(", ")");
decl.prefix_words = PrefixWords::new("Eq.~", "Eqs.~");
let style = decl.compile(&lang).expect("the style compiles");
let segments = format(&CounterValue::new(13), &style, None, &FormatOptions::new())?;
assert_eq!(written(&segments), "Eq.~(13)");
// Without the word, and without the delimiters.
let bare = FormatOptions::new().without_word().without_delimiters();
let segments = format(&CounterValue::new(13), &style, None, &bare)?;
assert_eq!(written(&segments), "13");Several numbers at once: they are sorted, consecutive ones are collapsed into a range, and the word takes its plural form.
let mut decl = CounterStyleDecl::new("example.style.equation");
decl.delimiters = Delimiters::new("(", ")");
decl.prefix_words = PrefixWords::new("Eq.~", "Eqs.~");
decl.join = JoinDecl::compact();
let style = decl.compile(&lang).expect("the style compiles");
let values = [
CounterValue::new(2),
CounterValue::new(9),
CounterValue::new(1),
CounterValue::new(3),
];
let written_values: Vec<NumberedValue<'_>> = values.iter().map(NumberedValue::new).collect();
let segments = format_many(&style, &written_values, &FormatOptions::new())?;
assert_eq!(written(&segments), "Eqs.~(1–3,9)");Structs§
- Ancestor
- An ancestor: one counter within which a number is numbered, as the numbering feature records it: the counter’s name, its value at the numbered point, the counter style in which that value is written, and the within-joiner that writes that counter’s own ancestors before it.
- BadNumber
Format - The error that is reported when the number template of a reference point
is not valid (id
flm.numbering.bad-number-format). - BadWithin
Joiner - The error that is reported when the
withinvalue that the numbering options give a counter cannot be used (idflm.numbering.bad-within-joiner). - Counter
Decl - A counter, as the feature that declares it contributes it to the service
slot
COUNTERS_SLOT. - Counter
Options - What a document’s numbering options say about one counter: the counter
within which it is numbered, and the value it has before it is first
advanced. A field left
Nonekeeps what the counter was declared with. - Counter
Record - What the numbering stage recorded for one numbered reference point: the counter the point counts in, that counter’s value at the point, the counters it is numbered within with their own values, and the counter style in which its number is shown.
- Counter
Style - A counter style compiled against the built language: what a number is written with.
- Counter
Style Decl - A counter style, as a feature declares it.
- Counter
Value - A counter value: the value a counter has at one numbered reference point, a number with optional sub-numbers under it.
- Counters
- The counter table,
flm.counters: every reference point the numbering stage read, each with its record (CounterRecord) or with nothing for a point that is not numbered. - Delimiters
- The delimiters of a counter style: the text before and the text after a
number, such as
(and)for an equation number shown as(3). - Format
Options - How a number is asked for: which variant of the prefix words, and whether the word and the delimiters are written.
- Hierarchy
Cycle - The error that is reported when the numbering options number counters
within each other (id
flm.numbering.hierarchy-cycle). - Instance
NotEnclosing - The error that is reported when the scoped instances of a reference point
do not enclose the reference point one within the next (id
flm.numbering.instance-not-enclosing). - JoinCx
- The context with which a within-joiner builds content: an ancestor’s number, the parts written before an ancestor, and text of its own.
- Join
Decl - The join texts of a counter style: the texts that join several numbers
into one text, such as
1–3, 7, and 9. - Many
Numbers - The numbers of several reference points, written in one group per
counter style (
Counters::format_many), and the points that have no number. - NotAn
Ancestor - The error that is reported when, under the numbering options, the number of
a counter includes the number of a counter that is not always one of its
ancestors (id
flm.numbering.not-an-ancestor). - Number
Out - The output to which a number writer appends what it writes.
- Number
Segment - One stretch of a number as the counter table writes it: a small tree of content, and the reference point it links to.
- Number
Writer Decl - A number writer as it is contributed to the service slot
NUMBER_WRITERS_SLOT: a value that holds the writer. - Numbered
Value - One value that a number writer is asked to write: the counter value, the counters it is numbered within, the scoped instance it was counted in if any, and its position among the values the caller passed in.
- Numbering
- The
numberingfeature, which numbers the reference points of a document, such as headings, equations and theorems, with counters. - Numbering
Config - The configuration of the
numberingfeature: counter styles to be used in place of declared counter styles. - Numbering
Options - The numbering options of a document: per counter, the counter within which it is numbered, and the value it has before it is first advanced.
- Parent
- What a number is numbered within: the within-joiner that writes the ancestors’ numbers, and the ancestors with their values.
- Parent
SetDecl - A parent set: a named list of counters, any one of which may be the parent
of a counter numbered within the set with
Within::parent_set. - Parent
Uninitialized - The error that is reported when the number of a reference point depends on
a counter that has no value yet (id
flm.numbering.parent-uninitialized). - Part
- A part: one unit of the text that a within-joiner writes before a point’s own number, with the ancestors it shows and its content.
- Plural
Words - The word written before a number, in two forms: one for a single number and one for several.
- Prefix
Words - The prefix words of a counter style: the words written before a number by default, and the named variants that a request may ask for instead.
- Segment
- A segment: a small tree of FLM content that is one stretch of a written number, together with the link mark of the value it links to.
- Stage
Failed - The error that is reported when the numbering stage
NUMBERcannot read its input or write the counter table (idflm.numbering.stage-failed). - SubLevel
Decl - A sub-level of a counter style: how the sub-numbers at one level are written, and the separator written before each.
- Undeclared
Counter - The error that is reported when a reference point is numbered with a
counter that is not declared in any feature (id
flm.numbering.undeclared-counter). - Undeclared
Style - The error that is reported when a reference point is numbered in a counter
style that is not declared in any feature (id
flm.numbering.undeclared-style). - Undeclared
SubLevel - The error that is reported when the number of a reference point has a
sub-number that its counter style cannot write (id
flm.numbering.undeclared-sub-level). - Unknown
Counter - The warning that is reported when the numbering options refer to a counter
that is not declared in any feature (id
flm.numbering.unknown-counter). - Unknown
Parent Set - The error that is reported when the numbering options number a counter
within a parent set that is not declared in any feature (id
flm.numbering.unknown-parent-set). - Unknown
Within Joiner - The error that is reported when the numbering options select a
within-joiner by a name that is not registered (id
flm.numbering.unknown-within-joiner). - Within
- The counter within which a counter is numbered, and how that counter’s number is written before the counter’s own: the counter’s place in the hierarchy of counters.
- Within
Cx - The context against which a within-joiner is compiled: the built language, the template language shared by every template of the numbering feature, the counters a within template may refer to, and the within-joiners registered by name.
- Within
Joiner Decl - A within-joiner as it is contributed to the service slot
WITHIN_JOINERS_SLOT: a value that holds the within-joiner. - WriteCx
- The writing context of a number writer: the counter style’s data, the library’s operations that build content, the within-joiner on demand, and the default writer to delegate to.
- Writer
Cx - The context against which a number writer is compiled: the built language, and the name of the counter style the writer is compiled for.
Enums§
- Format
Error - Why a number could not be written.
- Join
Text - The name of one of the join texts with which a counter style joins
several numbers (
WriteCx::join_text). - Number
Format - A number format: one way of writing a number as characters, as decimal digits, letters, roman numerals, footnote symbols, or superscript or subscript digits.
- Number
Template - A number template: how a counter style writes the number itself, as a text
with a place for the number, written in one of the eight
NumberFormats; a counter style holds one in its fieldnumber_format. - Parents
- Which counters a counter may be numbered within: one counter, any counter of a list, or any counter of a registered parent set.
- Style
Error - Why a counter style, a within-joiner or a number writer could not be compiled.
- Within
Joiner Spec - The form of a within-joiner, which writes a counter’s ancestors’ numbers
before the counter’s own number: one of the four forms a
Withinmay carry. - Within
Option - The place in the hierarchy of counters that a document’s numbering options give a counter, replacing the place the declaring feature gave it.
Constants§
- COUNTERS
- The typed name of the counter table,
flm.counters, whose value is aCounters. - COUNTERS_
SLOT - The name of the service slot into which a feature declares its counters,
flm.numbering.counters. - FEATURE
- The registered name of the numbering feature,
numbering. - NUMBER
- The name of the numbering stage,
flm.numbering.number. - NUMBER_
WRITERS_ SLOT - The name of the service slot into which a feature contributes a number
writer,
flm.numbering.number-writers. - PARENT_
PLACEHOLDER - The name,
_parent, under which a within template writes the parent that applies. - PARENT_
SETS_ SLOT - The name of the service slot into which a feature contributes a parent
set,
flm.numbering.parent-sets. - RESERVED_
PREFIX - The prefix,
_, that begins every name the numbering feature reserves for itself. - STYLES_
SLOT - The name of the service slot into which a feature declares its counter
styles,
flm.numbering.styles. - WITHIN_
JOINERS_ SLOT - The name of the service slot into which a feature contributes a
within-joiner registered by name,
flm.numbering.within-joiners.
Traits§
- Compiled
Number Writer - A compiled number writer: what writes the numbers of a counter style.
- Compiled
Within Joiner - A compiled within-joiner: what writes the ancestors’ numbers.
- Number
Writer - A number writer as a feature declares it: its name, and how it compiles itself once the language is built.
- Within
Joiner - A within-joiner registered by name, as a feature declares it: its name, and how it compiles itself once the language is built.
Functions§
- feature
- Returns the
Numberingfeature with the default configuration, which contains no replacement counter style. You can pass the result toPackageEntry::featureto add the feature to a package. - format
- Writes one counter value in
styleand returns the segments of the result: under the default writer, the word, the delimiters and the number, as one segment that links to the value. - format_
many - Writes several counter values in
styleas one text and returns the segments of that text: the word, the delimiters, and the values as the counter style’s number writer writes them. - identifier_
text - Returns the text that identifies a numbered reference point by its
counter value: the values of the ancestors first, outermost first as a
Parentcarries them, then the point’s own value, every component written in decimal and joined by hyphens. - parts
- Returns the parts written before a point’s own number: what
parent’s within-joiner writes of its chain of ancestors.