1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//! The text commands of math mode: `\text{…}` and the related commands, which
//! put document text inside a formula.
//!
//! A **text command** is a command of math mode whose one argument is read in
//! text mode: inside a formula, its argument is document content rather than
//! mathematical notation. An author writes a word of text inside a formula, such
//! as "for all *x* such that", a unit or a short remark, with `\text{…}`,
//! `\textbf{…}`, `\mbox{…}` or another of the twelve commands of [`COMMANDS`].
//! Everything else in a formula is carried to the output as the author wrote it
//! (see the [`math`](super) feature).
//!
//! # Where the commands are declared
//!
//! This module declares the twelve commands **for math mode only**
//! (`[Mode::Math]`). In text mode they are not this module's: `\textbf`,
//! `\textit` and `\texttt` are the
//! [`textstyles`](crate::stdlib::features::textstyles) feature's, and `\text`,
//! `\mbox` and the others are unknown commands, an Error in text mode.
//!
//! The `textstyles` feature declares `\textbf`, `\textit` and `\texttt` in text
//! mode only, the default of a declaration ([`ElementDef::modes`]). The two
//! features therefore declare these names in modes that do not overlap: in text
//! mode the name is the `textstyles` feature's, in math mode this feature's.
//! Two declarations whose modes do not overlap never clash, so neither feature
//! declares an override, and the order in which the two features are registered
//! does not matter.
//!
//! # What the argument holds
//!
//! The argument of a text command is read in text mode, with the parsing rules of
//! text, so it holds ordinary FLM content: formatting commands (`\emph{…}`),
//! references (`\ref{…}`), uses of user definitions, and mathematics again
//! (`\(…\)`). The two modes differ at the argument's edge: a command that no
//! feature declares is kept as source in the mathematics around the argument,
//! but is an Error inside it, because the argument is text.
//!
//! Three later steps follow from this, each in its own module:
//!
//! - the [block-structure pass](crate::lang::rewrites::blocks) lays the argument
//! out as inline content (whitespace resolved, typographic substitution
//! applied), although the formula around it is an [opaque
//! region](crate::guide::concepts_overview#opaque-regions) that the pass leaves
//! untouched;
//! - on the `html` backend, and inside a display-math environment on the `text`
//! backend, the lowering puts the argument's content inside a
//! [`flm.pres.math-text`](crate::stdlib::backends::pres::MATH_TEXT) node among
//! the formula's source, the only part of the formula that is content (on the
//! `text` backend, `\(…\)` and `\[…\]` are copied whole as source);
//! - the `latex-recomposed` backend writes the command back as the LaTeX macro of
//! the same name, its argument recomposed as text ([`super::latex`]).
//!
//! # The element and its rules
//!
//! The twelve commands form one family, declared as one element type with one
//! element value per command ([elements and
//! aspects](crate::guide::concepts_overview#elements-and-aspects)): the element
//! type [`TEXT`] (`flm.math.text`), each element value carrying a
//! [`MathTextCommand`] with the command's name, which a backend writes back.
//!
//! - [`RULE`] (`flm.math.text.lower.default`) is the default rule on every
//! backend: it stages (builds into the lowered tree; see
//! [`Lowerer`]) a `flm.pres.math-text` node
//! that carries the command's name, over the argument lowered through the
//! rules. The rules of the [`math`](super) feature that write a formula on the
//! `text` and `html` backends lower each text command they find through it.
//! - On the `latex-recomposed` backend,
//! [`TEXT_LATEX_RULE`](super::latex::TEXT_LATEX_RULE) is the default instead.
//!
//! # Commands not declared here
//!
//! `\intertext` and `\shortintertext` also take text, but they set a paragraph
//! of text between two lines of a display-math environment, not content inside a
//! formula. FLM does not model them: the reader of equation lines
//! ([`super::lines::equation_lines`]) would leave such a command in the content
//! of the line that follows it. They are therefore unknown commands, kept as
//! source like any unknown command in mathematics.
// The argument's mode is part of each declaration: the argument is stated to be
// text once, where the command is declared, and every later step reads it off
// the parsed node without being taught about mathematics. `\fbox` is not
// declared: it draws a frame, a construct of its own, not a change of the text's
// face.
use Arc;
use NodeRef;
use Mode;
use crate;
use crate;
use crateFlm;
use crate;
use crate;
use crate;
use crate;
use cratelower_arg_or_nothing;
use crate;
/// The name of the element type of the text commands of math mode,
/// `flm.math.text`. The data of each element value is a
/// [`MathTextCommand`].
pub const TEXT: &str = "flm.math.text";
/// The name of the default lowering rule of [`TEXT`] on every backend,
/// `flm.math.text.lower.default`, whose body is [`TextRule`].
pub const RULE: &str = "flm.math.text.lower.default";
/// The name of the only argument of every text command, `content`.
pub const CONTENT_ARG: &str = "content";
/// The text commands declared for math mode, in the order they are registered:
/// `text`, `textnormal`, `textup`, `textrm`, `textmd`, `textbf`, `textit`,
/// `textsl`, `textsc`, `textsf`, `texttt`, `mbox`.
///
/// The names are written without the leading backslash. Each command takes one
/// mandatory argument read in text mode. The library does not apply what a
/// command asks of the output (a bold weight for `\textbf`, nothing for
/// `\text`): the command's name is carried by the element value
/// ([`MathTextCommand`]) and by the payload of the presentation node
/// ([`MathTextPayload::command`]), and the output decides.
pub const COMMANDS: = ;
/// The data of one element value of `flm.math.text`: which text command was
/// written.
///
/// A lowering rule or a stage reads it from a node with [`element_value`].
/// The element value of one text command: a `flm.math.text` element value
/// carrying the command's name, inline (the default block role).
/// The declaration of one text command: a macro, declared for math mode only,
/// with one mandatory argument read in text mode.
/// The body of [`RULE`]: it stages a
/// [`flm.pres.math-text`](crate::stdlib::backends::pres::MATH_TEXT) node that
/// carries the command's name, over the command's argument lowered through the
/// rules.
///
/// When the argument is missing (a parse recovery left it absent and the parse
/// reported it), the node has no content and nothing more is reported. The
/// rule returns a [`LowerError`] when the node carries no [`MathTextCommand`]
/// or when the argument or the node could not be lowered or staged.
;
/// Registers the text commands and their default lowering rule, as part of the
/// `math` feature.
///
/// # Errors
///
/// Returns the [`BuildError`] the feature builder raises, for instance for a
/// rule already registered under the same name.
pub