Skip to main content

Module ensuremath

Module ensuremath 

Source
Expand description

\ensuremath{…}: mathematics wherever it is written.

LaTeX’s \ensuremath{X} typesets X as mathematics: in text it is an inline formula, and inside a formula it is X itself. The math feature declares the command in both parsing modes, with one element type, ENSUREMATH (flm.math.ensuremath), and one default rule on every backend, RULE.

§In text: an inline formula

In text mode, \ensuremath{X} is read as the inline formula \(X\). Its one argument must be written in braces, and the braces delimit a math group: a node of the structural element flm.math, inline (MathGroupForm::Inline), whose content X is parsed in math mode, as the content of \(…\) is. The group is the argument’s content (argument_content_nodes answers it alone); the \ensuremath node around it holds nothing else.

A feature that processes mathematics therefore meets this formula as it meets \(X\): the same element, the same form (not displayed, so no number), the same content, with its text commands. Its rule for flm.math lowers the group, reached through RULE, which lowers the argument; its stages find the group among the tree’s descendants. The one difference is the delimiters written in the source: the group’s delimiters are { and } (what group_delimiters answers), so an output that writes a formula’s delimiters takes them from the group’s form, as every output of the standard backends does, not from the source.

The recomposed LaTeX writes this formula as written, \ensuremath{X}: group_opening and group_closing, which a processing feature’s rule for flm.math calls on that backend, write \ensuremath{ and } for it in place of \( and \). The command keeps the formula valid LaTeX where it ends up inside another formula: a kept user definition whose template writes \ensuremath, used inside a formula, or an argument that a template places in mathematics.

Without braces (\ensuremath x) the argument is missing, an Error.

§In mathematics: the argument in its place

In math mode, \ensuremath{X} stands for X as if X were written in its place. The command takes one mandatory argument, read in math mode like the formula around it. RULE lowers the argument’s content, so a formula lowered node by node writes X. A writer that writes a formula’s TeX from its nodes for a renderer writes the argument’s content and leaves out the command: the source walk of math-passthrough, whose TeX reaches the HTML page’s MathJax, does, and so does the math-mathjax extension. The recomposed LaTeX writes a formula as the author wrote it, so it keeps the command, which LaTeX defines, as it does in text.

§The element and its rule

The two declarations share the element type ENSUREMATH; the parsing mode of the node tells them apart. RULE (flm.math.ensuremath.lower.default) is the default rule on every backend: it lowers the argument’s content through the rules, the inline formula in text mode, X in math mode.

Structs§

EnsureMathRule
The body of RULE: it lowers the argument’s content through the rules, the inline formula in text mode and the mathematics in math mode, and stages nothing of its own.

Constants§

COMMAND
The command’s name, ensuremath, written without the leading backslash.
ENSUREMATH
The name of the element type of \ensuremath, flm.math.ensuremath, in both parsing modes.
FORMULA_ARG
The name of the command’s only argument, formula in text mode and in math mode alike.
RULE
The name of the default lowering rule of ENSUREMATH on every backend, flm.math.ensuremath.lower.default, whose body is EnsureMathRule.