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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! Macro definition system for KaTeX
//!
//! This module provides the infrastructure for defining how macros are expanded
//! and processed, including the MacroContextInterface that provides context to
//! macro expansion functions.
use Arc;
use ;
use crate::;
/// Represents the result of consuming an argument from the token stream during
/// LaTeX macro expansion.
///
/// In KaTeX, macros can take arguments enclosed in braces `{}` or delimited by
/// specific tokens. This struct captures the tokens that form the argument,
/// along with the boundary tokens that define its start and end in the input
/// stream. Arguments are essential for parameterized macro expansion, allowing
/// macros to operate on variable content.
///
/// # Examples
///
/// When parsing a macro like `\frac{a}{b}`, each braced group becomes a
/// `MacroArg`:
/// - The first argument contains tokens for 'a', with start/end tokens being
/// `{` and `}`.
/// - The second argument contains tokens for 'b', similarly bounded.
///
/// This structure enables precise tracking of argument boundaries for error
/// reporting and nested macro processing.
///
/// # Fields
///
/// - `tokens`: The sequence of tokens that constitute the argument's content.
/// These tokens are extracted from the input stream and may include nested
/// structures, subscripts, or other LaTeX constructs.
/// - `start`: The token that marks the beginning of the argument. Typically an
/// opening delimiter like `{` or a specific command token that initiates
/// argument consumption.
/// - `end`: The token that marks the end of the argument. Usually a closing
/// delimiter like `}` or a matching token that terminates argument parsing.
///
/// # Error Handling
///
/// Argument consumption may fail if delimiters are mismatched or if the stream
/// ends unexpectedly. See [`MacroContextInterface::consume_arg`] for related
/// error conditions.
///
/// # Cross-references
///
/// - Used by [`MacroContextInterface::consume_arg`] to return parsed arguments.
/// - Related to token processing in [`crate::lexer`] and macro expansion in
/// [`crate::parser`].
/// Represents the expansion of a LaTeX macro into a sequence of tokens during
/// mathematical typesetting.
///
/// In KaTeX, macros are expanded to produce new token sequences that replace
/// the original macro invocation. This struct encapsulates the result of such
/// expansion, including the generated tokens, argument specifications, and
/// expansion properties. The tokens are stored in reverse order to facilitate
/// efficient stack-based processing during parsing.
///
/// Macro expansion is a core mechanism in LaTeX for defining reusable
/// constructs, such as mathematical symbols, formatting commands, or complex
/// expressions. For example, `\alpha` expands to the Greek letter α, while
/// parameterized macros like `\frac{numerator}{denominator}` expand based on
/// provided arguments.
///
///
/// # Fields
///
/// - `tokens`: The sequence of tokens resulting from macro expansion. These
/// tokens replace the original macro call in the input stream and are stored
/// in reverse order for efficient processing by the parser.
/// - `num_args`: The number of arguments this macro expects. For example,
/// `\frac` expects 2 arguments, while `\alpha` expects 0.
/// - `delimiters`: Optional delimiters for each argument. Each inner vector
/// specifies the opening and closing delimiters for an argument. If `None`,
/// arguments are typically braced.
/// - `unexpandable`: Whether this macro should not be further expanded. Used in
/// TeX's `\let` command to create aliases that preserve the original
/// behavior.
///
/// # Error Handling
///
/// Expansion may fail if arguments don't match the expected delimiters or if
/// the macro is undefined. See [`MacroContextInterface::expand_macro`] for
/// expansion error conditions.
///
/// # Cross-references
///
/// - Created during macro expansion in [`crate::parser`].
/// - Used by [`MacroContextInterface`] methods for token processing.
/// - Related to macro definitions in [`MacroDefinition`].
/// Type for function-based macro definitions in KaTeX.
pub type MacroFunction = ;
/// Type for static function-based macro definitions in KaTeX.
pub type StaticMacroFunction =
fn ;
/// Defines how a LaTeX macro is expanded and processed in KaTeX's mathematical
/// typesetting system.
///
/// Macro definitions specify the transformation rules for macro invocations,
/// supporting different expansion strategies to handle various LaTeX
/// constructs. This enum encapsulates the three primary ways macros can be
/// defined: simple string replacement, token-based expansion, and dynamic
/// function-based generation.
///
/// In LaTeX/KaTeX, macros enable the creation of reusable mathematical notation
/// and formatting commands. For instance, `\alpha` might be defined as a simple
/// string or token expansion, while more complex macros like `\frac` use
/// structured token expansions with arguments.
///
/// # Variants
///
/// - `String`: Direct string replacement for simple macros. Used for basic
/// symbol substitutions where no complex token processing is needed.
/// - `Expansion`: Structured token expansion with argument handling. Supports
/// parameterized macros with specific token sequences and delimiter rules.
/// - `Function`: Dynamic expansion using a closure that can access parsing
/// context. Enables macros that adapt their behavior based on the current
/// parsing state.
///
/// # Error Handling
///
/// Function-based macros can return errors during expansion, typically due to
/// invalid arguments or context-dependent failures. String and expansion
/// variants are generally error-free unless token processing fails.
///
/// # Cross-references
///
/// - Stored in [`Namespace`] for lookup during parsing.
/// - Processed by [`MacroContextInterface`] methods during expansion.
/// - Related to expansion results in [`MacroExpansionResult`].
/// Represents the outcome of expanding a function-based macro in KaTeX's
/// mathematical typesetting system.
///
/// When macros are defined using functions (via [`MacroDefinition::Function`]),
/// they return this enum to specify the result of their expansion. This allows
/// dynamic macros to produce either simple string output or complex token-based
/// expansions depending on context and arguments.
///
/// Function-based macros provide the most flexibility in LaTeX processing,
/// enabling conditional behavior, argument validation, and context-aware
/// generation of mathematical content. For example, a macro might return
/// different symbols based on the current math mode or generate different
/// structures based on argument types.
///
/// # Variants
///
/// - `String`: A simple string that will be inserted directly into the output.
/// Suitable for basic text or symbol substitutions that don't require further
/// token processing.
/// - `Expansion`: A full token expansion with all associated metadata. Enables
/// complex macros that need argument handling, delimiters, or special
/// expansion properties.
///
/// # Error Handling
///
/// Function-based macros can fail during expansion, returning a `String` error
/// message. The expansion process may encounter issues like invalid arguments,
/// undefined references, or context-dependent failures that prevent successful
/// macro resolution.
///
/// # Cross-references
///
/// - Returned by function-based macros defined in
/// [`MacroDefinition::Function`].
/// - Processed by [`MacroContextInterface`] during macro expansion.
/// - Contains either strings or [`MacroExpansion`] instances.
/// Interface providing context and utilities for LaTeX macro expansion in
/// KaTeX's mathematical typesetting system.
///
/// This trait defines the contract for objects that provide the necessary
/// context and operations for expanding macros during LaTeX parsing. It is
/// implemented by the macro expander and offers access to the current parsing
/// state, token stream manipulation, and macro definitions.
///
/// The interface supports the full range of TeX-like macro expansion
/// operations, including token peeking, argument consumption, recursive
/// expansion, and mode-aware processing. Function-based macros use this
/// interface to access parsing context and perform complex expansion logic.
///
/// # Cross-references
///
/// - Implemented by the macro expansion engine in [`crate::parser`].
/// - Used by function-based macros defined in [`MacroDefinition::Function`].
/// - Provides access to [`Namespace`] of macro definitions.