Skip to main content

Module math

Module math 

Source
Expand description

The math feature: mathematics written in LaTeX notation, its display-math environments, its equation lines and their numbers.

This feature introduces mathematics into the language and nothing more. It declares the few things FLM reads inside mathematics: the display-math environments and the lines they are made of, the commands that put document text inside a formula, and references to equations. It checks no formula and processes none: a processing feature does, a feature that reads the mathematical content and gives every formula its lowering rules (Processing mathematics, below). Its one rule on a formula is the rule of last resort of the display-math environments, which reports an environment that no processing feature lowers (unprocessed). The guide to the language describes mathematics for authors (mathematics).

§What an author writes

  • Inline and displayed mathematics, \(…\) and \[…\] (and $…$, $$…$$ when the parsing option FlmParsing::dollar_math is on). The language layer reads these as math groups, nodes of the element flm.math (MATH).
  • Six display-math environments: equation, align, gather and their starred forms equation*, align*, gather*. The unstarred forms number their lines.
  • Inside a display-math environment’s body, the four commands \\, \label{eq:…}, \tag{…} (or \tag*{…}) and \nonumber, which divide the body into equation lines and say how each line is labeled and numbered (lines).
  • Text inside a formula: \text{…}, \textbf{…}, \mbox{…} and the other text commands of math mode, whose argument is document content (text).
  • \ensuremath{…}, in both modes (ensuremath): in text, an inline formula, read as \(…\) is (its braces delimit a math group, a node of flm.math); inside a formula, its argument, as if written in its place.
  • References to equations: \eqref{eq:…}, which shows an equation line’s number without a word before it (eqref).

Displayed mathematics, a group \[…\] or $$…$$ and a display-math environment alike, is a displayed item: it stands inside the paragraph it is written in, set on lines of its own, and the text that follows it with no blank line between continues that paragraph. The environments declare that role here; the group’s element answers it from the group’s form (StructuralElementSpecs::math).

Every other command of mathematics (\alpha, \frac, a cases environment) is the processing feature’s to declare or accept.

A processing feature needs to know nothing of \ensuremath written in text: the formula is a math group like any other, the content of the command’s argument, which the feature’s own rule (ensuremath::RULE) lowers. On the recomposed LaTeX, latex::group_opening and latex::group_closing, which the feature’s rule for flm.math calls, write that formula \ensuremath{…} as written. Inside a formula, a writer of the formula’s TeX for a renderer writes the argument’s content in place of the command (ensuremath, In mathematics).

§Processing mathematics

A processing feature does four things, which this feature leaves to it:

  • it says what a command of mathematics is, for instance with a fallback (FallbackDef) for macros and environments in math mode, or with declared elements in math mode;
  • it provides the capability CONTENT (flm.math.content) with a check stage of its own, and adds its check to the collection flm.validated: CONTENT itself, or, for an extension, which adds to a collection only a capability named under its own stem, a capability of its own that the stage provides beside CONTENT;
  • it registers the lowering rules of flm.math and of ENVIRONMENT (flm.math.environment) on the backends it serves;
  • on the HTML backend, it marks the outermost element it writes for each formula with the class of the role html::FORMULA_ROLE (flm-math-formula) and the attributes data-flm-display and, for an environment, data-flm-env (html), whatever element it chooses, so that a stylesheet or a script of the page finds every formula alike.

The rules may be the library’s own: a processing feature that writes its formulas as LaTeX on a backend registers there the rules of math-passthrough under rule names of its own (math_rule_named and the other constructors; that module’s documentation, The rules under another feature’s names), as the math-techxt extension does on the two LaTeX backends. A feature that hands its formulas to a typesetter of TeX notation reads each formula’s TeX with the writer of tex, which leaves a hole for each text command for the feature to lower.

No standard feature processes mathematics: a registry adds such a feature from another package, for example the feature of the package math-passthrough, which accepts every formula and carries its source to the output as written, or an extension that typesets formulas. Every standard workflow demands the collection flm.validated, which holds the check of the mathematics exactly when a processing feature is registered. A registry that holds this feature and no processing feature therefore builds, and so does the plan of every standard workflow: a document without formulas renders without error, and each formula is an Error (MathUnprocessed, flm.math.unprocessed), reported where a backend lowers it through a rule of last resort: the lowering feature’s for a math group (MathGroupRule), this feature’s for a display-math environment (unprocessed::RULE).

§Equation lines, reference points and numbers

The body of a display-math environment is an opaque region in math mode: the rewrite passes leave it untouched. The feature reads it once as a list of equation lines, the parts of the body between two \\, with their labels, tags and no-number marks (lines::equation_lines).

Each equation line is a reference point (RefPoint; see reference points, labels and anchors): a place in the document that can be referred to, numbered and given an anchor. The environment declares one point per line (MathEnvironment implements Referenceable), of the reference kind KIND (equation).

  • A line can be numbered when its environment is unstarred and the line carries no \nonumber. It keeps the labels written on it and accepts labels of the type LABEL_TYPES (eq), so \ref{eq:name} refers to the line that carries \label{eq:name}.
  • A line that can be numbered and carries no \tag is numbered, unless the feature’s configuration numbers no equations (MathConfig::numbered_equations). It counts in the counter COUNTER (flm.math.equation) and its number is shown in the counter style COUNTER_STYLE (flm.math.style.equation): (1), and Eq.~(1) in a reference (counters and counter styles).
  • A line with a \tag counts in nothing: a reference to it shows the tag instead of a number.
  • A line that cannot be numbered (every line of a starred environment, and a line marked \nonumber) keeps no label and accepts no label type, so nothing can refer to it. A \label or \tag written on such a line is an Error (lines::LabelWithoutNumber).
  • Every line has an anchor, the identifier a link points at: for a labeled line, the anchor of its first label (eq-euler for \label{eq:euler}), and otherwise ANCHOR_PREFIX (eq), a double hyphen, the line’s position among the document’s equation lines and four letters, as in eq--8kqzt (auto_anchor).

The counter and the counter style belong to the numbering feature (numbering): this feature contributes their declarations (counter_decl, counter_style) to that feature’s service slots as peer contributions, contributions to a service slot that another feature owns, which the registry build drops with one Note when that feature is not registered (services). The numbers are then not computed, and no line shows one.

The table references ANCHORS (the anchors table of the references feature) and COUNTERS (the counter table of the numbering feature) are where a processing feature’s rules read each line’s anchor and number; the library of frame reads them for it (frame::line_frames).

§What the feature’s own elements become on each backend

The feature’s rules lower the commands it declares outside the formulas themselves: a text command, \ensuremath and \eqref. Each stages (builds into the lowered tree; see Lowerer) presentation nodes, except the rule of \ensuremath, which lowers its argument through the rules and stages nothing of its own.

The feature also registers, on every backend, the rule of last resort of the display-math environments, unprocessed::RULE, which is chosen only where no processing feature has a rule for ENVIRONMENT on the backend (unprocessed).

A text command becomes content: its argument is document text, so text::RULE lowers it like inline content anywhere else, into a flm.pres.math-text node; a reference in it is a link, and a character the output format must escape is escaped. A processing feature decides whether a text command inside a formula is lowered through these rules (math-passthrough does).

The rendering guide describes the result under mathematics in HTML and mathematics in the recomposed LaTeX.

§The capabilities

A capability is a registered dotted name for a state of processing that a stage brings about (CapabilityDef). The feature registers two:

  • CONTENT (flm.math.content): the mathematics of the document has been checked. No stage of this feature provides it: a processing feature provides it with a check of its own (the feature of the package math-passthrough accepts every formula) and adds its check to the collection flm.validated (Processing mathematics, above), so that every workflow that validates a document, the check workflow among them, runs that check exactly when a processing feature is registered. Where two registered features provide it, a plan request prefers one of their two stages; otherwise the plan build fails (PlanError::Contested), so a registry usually holds one processing feature.
  • RENDERED (flm.math.rendered): some rendered form of the mathematics exists. No standard feature provides it. An extension that renders mathematics provides a refinement of it, named in the extension’s own namespace (the math-mathjax extension’s table math-mathjax.rendered, the MathML of each formula, in its mathml mode; the math-techxt extension’s table math-techxt.rendered, each formula as Unicode text), and a rule that needs rendered mathematics of any kind requires flm.math.rendered. A plan that demands it cannot be built until such an extension is registered.

§Settings

  • MathConfig, the feature’s configuration (fixed when the registry is built; see settings, options and configuration): whether the lines of the unstarred environments are numbered.
  • MathRecomposeOptions, an option type set per render request: what the recomposed LaTeX writes around a display-math environment, and whether it writes the environment through the LaTeX hook \flmMathEnvironment. The rules of a processing feature on the latex-recomposed backend read it.

Each field of the two types is a settings key under its own name (crate::core::settings).

§Map of the module

  • Math — the feature, configured by MathConfig; feature returns it with the default configuration.
  • ENVIRONMENT, MathEnvironment, ENVIRONMENTS — the element and the data of an environment’s element values.
  • KIND, LABEL_TYPES, ANCHOR_PREFIX, COUNTER, COUNTER_STYLE, counter_decl, counter_style — the reference points of the equation lines and their numbering.
  • CONTENT, RENDERED — the capabilities.
  • lines — the four commands of an environment’s body and the reader of equation lines.
  • text — the text commands of math mode.
  • ensuremath — \ensuremath, an inline formula in text and its argument inside a formula.
  • eqref — \eqref.
  • unprocessed — the rule of last resort of the display-math environments.
  • frame — the library a processing feature computes a formula’s frame with: whether a math group is displayed, and each equation line’s anchor and number or tag.
  • latex — the recomposition rule of the text commands, the LaTeX names of the environments, MathRecomposeOptions, the library a processing feature writes a formula’s frame and an environment’s lines as LaTeX with, and the newline rule that every LaTeX writer of the lines applies.
  • tex — the writer of a formula’s TeX with a hole for each text command, for a processing feature that hands its formulas to a typesetter: a math group’s content, an equation line’s content under the newline rule.

Re-exports§

pub use latex::MathRecomposeOptions;

Modules§

ensuremath
\ensuremath{…}: mathematics wherever it is written.
eqref
\eqref{eq:euler}: a reference to an equation line that shows its number and nothing else, (13) where \ref{eq:euler} shows Eq.~(13).
frame
The frame of a formula: what the math feature knows of a formula without reading its content, computed by plain functions that a processing feature’s stages and rules call.
html
The mark of a formula on the HTML backend: what tells a stylesheet or a script of the page that an element is a formula, whichever processing feature wrote it.
latex
What the math feature contributes to the latex-recomposed backend: the recomposition rule of the text commands, the names a processing feature writes a display-math environment with, the options that change how an environment is written, the parts and the line writer a processing feature writes a formula with, and the newline rule that every LaTeX writer of an environment’s lines applies.
lines
Equation lines: the four commands an author writes inside a display-math environment’s body (\\, \label, \tag and \nonumber), and the reader that splits such a body into lines.
tex
The TeX of a formula, with holes: what a renderer of mathematics converts, written from the formula’s nodes.
text
The text commands of math mode: \text{…} and the related commands, which put document text inside a formula.
unprocessed
The rule of last resort of the display-math environments: a display-math environment that no feature processing mathematics lowers is an Error.

Structs§

Math
The math feature: it registers the six display-math environments and the commands of their bodies, the text commands of math mode, \ensuremath, \eqref, the counter and counter style of equations, and the two capabilities. It registers no fallback, no check and no rule on a formula: a feature that processes mathematics does (the module documentation, Processing mathematics).
MathConfig
The configuration of the math feature: whether the lines of the unstarred display-math environments are numbered.
MathEnvironment
The data of one element value of flm.math.environment: which display-math environment was written, and whether its lines are numbered.

Constants§

ANCHOR_PREFIX
The text that the anchor of an equation line without a label begins with, eq: the first of the LABEL_TYPES.
CONTENT
The name of the capability flm.math.content: the mathematics of the document has been checked. No stage of this feature provides it: a feature that processes mathematics provides it with a check of its own and adds that check to the collection flm.validated, so that a workflow that validates a document runs it exactly when such a feature is registered (the feature of the package math-passthrough accepts every formula). See the module documentation, The capabilities.
COUNTER
The name of the counter that a numbered equation line counts in, flm.math.equation: its counter value at the line is the line’s number (CounterDecl).
COUNTER_STYLE
The name of the counter style that an equation line’s number is shown in, flm.math.style.equation (CounterStyleDecl): the number format, the words a reference writes before the number and the parentheses around it.
ENVIRONMENT
The name of the element type of the six display-math environments, flm.math.environment. The data of each element value is a MathEnvironment.
ENVIRONMENTS
The display-math environments the feature declares, as (name, numbered), where numbered is the value of MathEnvironment::numbered.
FEATURE
The feature’s registered name, math.
KIND
The reference kind of every equation line’s reference point, equation: the name that says what sort of place a reference point is (RefPoint).
LABEL_TYPES
The label types that an equation line that can be numbered accepts: eq.
RENDERED
The name of the capability flm.math.rendered: some rendered form of the mathematics exists.

Functions§

counter_decl
The declaration of the counter COUNTER (flm.math.equation), shown by default in the counter style COUNTER_STYLE and numbered within no other counter.
counter_style
The declaration of the counter style COUNTER_STYLE (flm.math.style.equation): the number in decimal digits between parentheses, (3); the prefix words Eq.~ before one number and Eqs.~ before several, and Equation~ and Equations~ in the variant capital; several numbers joined compactly ((1,2), (1–3)).
feature
The Math feature with the default configuration, as a shared value ready to be listed in a package entry.