katex-rs 0.2.4

A Rust implementation of KaTeX - Fast math typesetting for anywhere, more than just the web.
Documentation
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
609
610
611
612
613
614
615
#![allow(clippy::non_ascii_literal)]

mod setup;

use katex::types::{Mode, ParseErrorKind};
use setup::*;

#[test]
fn parser_handle_infix_nodes() {
    it("rejects repeated infix operators", || {
        let error = expect!(r"1\over 2\over 3").parse_error(&strict_settings())?;
        assert!(matches!(
            error.kind.as_ref(),
            ParseErrorKind::MultipleInfixOperators
        ));
        Ok(())
    });

    it("rejects conflicting infix operators", || {
        let error = expect!(r"1\over 2\choose 3").parse_error(&strict_settings())?;
        assert!(matches!(
            error.kind.as_ref(),
            ParseErrorKind::MultipleInfixOperators
        ));
        Ok(())
    });
}

#[test]
fn parser_handle_sup_subscript() {
    it("rejects ^ at end of group", || {
        let error = expect!(r"{1^}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::ExpectedGroupAfterSymbol { symbol } => {
                assert_eq!(symbol, "^");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("rejects _ at end of input", || {
        let error = expect!("1_").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::ExpectedGroupAfterSymbol { symbol } => {
                assert_eq!(symbol, "_");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("rejects \\sqrt as argument to ^", || {
        let error = expect!(r"1^\sqrt{2}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::FunctionMissingArguments { func, context } => {
                assert_eq!(func, "\\sqrt");
                assert_eq!(context, "superscript");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn parser_parse_atom() {
    it("rejects \\limits without operator", || {
        let error = expect!(r"\alpha\limits\omega").parse_error(&strict_settings())?;
        assert!(matches!(
            error.kind.as_ref(),
            ParseErrorKind::LimitsMustFollowBase
        ));
        Ok(())
    });

    it("rejects \\limits at the beginning of the input", || {
        let error = expect!(r"\limits\omega").parse_error(&strict_settings())?;
        assert!(matches!(
            error.kind.as_ref(),
            ParseErrorKind::LimitsMustFollowBase
        ));
        Ok(())
    });

    it("rejects double superscripts", || {
        let error = expect!(r"1^2^3").parse_error(&strict_settings())?;
        assert!(matches!(
            error.kind.as_ref(),
            ParseErrorKind::DoubleSuperscript
        ));
        let error = expect!(r"1^{2+3}_4^5").parse_error(&strict_settings())?;
        assert!(matches!(
            error.kind.as_ref(),
            ParseErrorKind::DoubleSuperscript
        ));
        Ok(())
    });

    it("rejects double superscripts involving primes", || {
        for expr in [r"1'_2^3", r"1^2'", r"1^2_3'", r"1'_2'"] {
            let error = expect!(expr).parse_error(&strict_settings())?;
            assert!(matches!(
                error.kind.as_ref(),
                ParseErrorKind::DoubleSuperscript
            ));
        }
        Ok(())
    });

    it("rejects double subscripts", || {
        let error = expect!(r"1_2_3").parse_error(&strict_settings())?;
        assert!(matches!(
            error.kind.as_ref(),
            ParseErrorKind::DoubleSubscript
        ));
        let error = expect!(r"1_{2+3}^4_5").parse_error(&strict_settings())?;
        assert!(matches!(
            error.kind.as_ref(),
            ParseErrorKind::DoubleSubscript
        ));
        Ok(())
    });
}

#[test]
fn parser_parse_implicit_group() {
    it("reports unknown environments", || {
        let error = expect!(r"\begin{foo}bar\end{foo}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::NoSuchEnvironment { name } => assert_eq!(name, "foo"),
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("reports mismatched environments", || {
        let error =
            expect!(r"\begin{pmatrix}1&2\\3&4\end{bmatrix}+5").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::MismatchedEnvironmentEnd { begin, end } => {
                assert_eq!(begin, "pmatrix");
                assert_eq!(end, "bmatrix");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn parser_parse_function() {
    it("rejects math-mode functions in text mode", || {
        let error = expect!(r"\text{\sqrt2 is irrational}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::FunctionDisallowedInMode { func, mode } => {
                assert_eq!(func, "\\sqrt");
                assert_eq!(mode, &Mode::Text);
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("rejects text-mode-only functions in math mode", || {
        let error = expect!("$").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::FunctionDisallowedInMode { func, mode } => {
                assert_eq!(func, "$");
                assert_eq!(mode, &Mode::Math);
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it(
        "rejects strict-mode text-mode-only functions in math mode",
        || {
            let error = expect!("\\'echec").parse_error(&strict_settings())?;
            match error.kind.as_ref() {
                ParseErrorKind::StrictModeError { message, code } => {
                    assert!(message.contains("accent \\'"));
                    assert_eq!(code, "mathVsTextAccents");
                }
                other => panic!("Unexpected error kind: {other:?}"),
            }
            Ok(())
        },
    );
}

#[test]
fn parser_parse_arguments() {
    it("complains about missing argument at end of input", || {
        let error = expect!(r"2\sqrt").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::ExpectedGroupAs { context } => {
                assert_eq!(context, "argument to '\\sqrt'");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("complains about missing argument at end of group", || {
        let error = expect!(r"1^{2\sqrt}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::ExpectedGroupAs { context } => {
                assert_eq!(context, "argument to '\\sqrt'");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("complains about functions as arguments to others", || {
        let error = expect!(r"\sqrt\over2").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::FunctionMissingArguments { func, context } => {
                assert_eq!(func, "\\over");
                assert_eq!(context, "argument to '\\sqrt'");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn parser_parse_group() {
    it("complains about undefined control sequence", || {
        let error = expect!(r"\xyz").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::UndefinedControlSequence { name } => assert_eq!(name, "\\xyz"),
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn parser_verb() {
    it(
        "complains about mismatched \\verb with end of string",
        || {
            let error = expect!(r"\verb|hello").parse_error(&strict_settings())?;
            assert!(matches!(
                error.kind.as_ref(),
                ParseErrorKind::VerbMissingDelimiter
            ));
            Ok(())
        },
    );

    it("complains about mismatched \\verb with end of line", || {
        let error = expect!("\\verb|hello\nworld|").parse_error(&strict_settings())?;
        assert!(matches!(
            error.kind.as_ref(),
            ParseErrorKind::VerbMissingDelimiter
        ));
        Ok(())
    });
}

#[test]
fn parser_expect_calls_parse_input_expecting_eof() {
    it("complains about extra }", || {
        let error = expect!(r"{1+2}}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::ExpectedToken { expected, found } => {
                assert_eq!(expected, "EOF");
                assert_eq!(found, "}");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("complains about extra \\end", || {
        let error = expect!(r"x\end{matrix}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::ExpectedToken { expected, found } => {
                assert_eq!(expected, "EOF");
                assert_eq!(found, "\\end");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("complains about top-level &", || {
        let error = expect!("1&2").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::ExpectedToken { expected, found } => {
                assert_eq!(expected, "EOF");
                assert_eq!(found, "&");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn parser_expect_calls_parse_implicit_group_expecting_right() {
    it("rejects missing \\right", || {
        let error = expect!(r"\left(1+2)").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::ExpectedToken { expected, found } => {
                assert_eq!(expected, "\\right");
                assert_eq!(found, "EOF");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("rejects incorrectly scoped \\right", || {
        let error = expect!(r"{\left(1+2}\right)").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::ExpectedToken { expected, found } => {
                assert_eq!(expected, "\\right");
                assert_eq!(found, "}");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn parser_parse_special_group_expecting_braces() {
    it("complains about missing { for color", || {
        let error = expect!(r"\textcolor#ffffff{text}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::InvalidColor { color } => assert_eq!(color, "#"),
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("complains about missing { for size", || {
        let error = expect!(r"\rule{1em}[2em]").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::InvalidSize { size } => assert_eq!(size, "["),
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("complains about missing } for color", || {
        let error = expect!(r"\textcolor{#ffffff{text}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::UnexpectedEndOfMacroArgument { expected } => {
                assert_eq!(expected, "}");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("complains about missing ] for size", || {
        let error = expect!(r"\rule[1em{2em}{3em}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::UnexpectedEndOfMacroArgument { expected } => {
                assert_eq!(expected, "]");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("complains about missing ] for size at end of input", || {
        let error = expect!(r"\rule[1em").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::UnexpectedEndOfMacroArgument { expected } => {
                assert_eq!(expected, "]");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it(
        "complains about missing } for color at end of input",
        || {
            let error = expect!(r"\textcolor{#123456").parse_error(&strict_settings())?;
            match error.kind.as_ref() {
                ParseErrorKind::UnexpectedEndOfMacroArgument { expected } => {
                    assert_eq!(expected, "}");
                }
                other => panic!("Unexpected error kind: {other:?}"),
            }
            Ok(())
        },
    );
}

#[test]
fn parser_parse_group_expecting_rbrace() {
    it("at end of file", || {
        let error = expect!(r"\sqrt{2").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::ExpectedToken { expected, found } => {
                assert_eq!(expected, "}");
                assert_eq!(found, "EOF");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn parser_parse_optional_group_expecting_rbrack() {
    it("at end of file", || {
        let error = expect!(r"\sqrt[3").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::UnexpectedEndOfMacroArgument { expected } => {
                assert_eq!(expected, "]");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("before group", || {
        let error = expect!(r"\sqrt[3{2}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::UnexpectedEndOfMacroArgument { expected } => {
                assert_eq!(expected, "]");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn environments_parse_array() {
    it("rejects missing \\end", || {
        let error = expect!(r"\begin{matrix}1").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::ExpectedArrayDelimiter { found } => assert_eq!(found, "EOF"),
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("rejects incorrectly scoped \\end", || {
        let error = expect!(r"{\begin{matrix}1}\end{matrix}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::ExpectedArrayDelimiter { found } => assert_eq!(found, "}"),
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn environments_array_environment() {
    it("rejects unknown column types", || {
        let error = expect!(r"\begin{array}{cba}\end{array}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::UnknownColumnAlignment { alignment } => {
                assert_eq!(alignment, "b");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn functions_delimiter_functions() {
    it("reject invalid opening delimiters", || {
        let error = expect!(r"\bigl 1 + 2 \bigr").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::InvalidDelimiterAfter {
                delimiter,
                function,
            } => {
                assert_eq!(delimiter, "textord");
                assert_eq!(function, "\\bigl");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("reject invalid closing delimiters", || {
        let error = expect!(r"\bigl(1+2\bigr=3").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::InvalidDelimiterAfter {
                delimiter,
                function,
            } => {
                assert_eq!(delimiter, "atom");
                assert_eq!(function, "\\bigr");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("reject group opening delimiters", || {
        let error = expect!(r"\bigl{(}1+2\bigr)3").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::InvalidDelimiterTypeAfter { function } => {
                assert_eq!(function, "\\bigl");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("reject group closing delimiters", || {
        let error = expect!(r"\bigl(1+2\bigr{)}3").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::InvalidDelimiterTypeAfter { function } => {
                assert_eq!(function, "\\bigr");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn functions_begin_end() {
    it("reject invalid environment names", || {
        let error = expect!(r"\begin x\end y").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::NoSuchEnvironment { name } => assert_eq!(name, "x"),
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn lexer_inner_lex() {
    it("rejects lone backslash at end of input", || {
        let error = expect!("\\").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::UnexpectedCharacter { character } => {
                assert_eq!(character, "\\");
            }
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    // JavaScript test suite also covers the case "rejects lone surrogate
    // characters", but Rust strings must be valid UTF-8, so that scenario
    // cannot be represented directly here.
}

#[test]
fn lexer_inner_lex_color() {
    it("reject 3-digit hex notation without #", || {
        let error = expect!(r"\textcolor{1a2}{foo}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::InvalidColor { color } => assert_eq!(color, "1a2"),
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn lexer_inner_lex_size() {
    it("reject size without unit", || {
        let error = expect!(r"\rule{0}{2em}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::InvalidSize { size } => assert_eq!(size, "0"),
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("reject size with bogus unit", || {
        let error = expect!(r"\rule{1au}{2em}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::InvalidUnit { unit } => assert_eq!(unit, "au"),
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });

    it("reject size without number", || {
        let error = expect!(r"\rule{em}{2em}").parse_error(&strict_settings())?;
        match error.kind.as_ref() {
            ParseErrorKind::InvalidSize { size } => assert_eq!(size, "em"),
            other => panic!("Unexpected error kind: {other:?}"),
        }
        Ok(())
    });
}

#[test]
fn unicode_accents() {
    it(
        "should return error for invalid combining characters",
        || {
            let error = expect!("A\u{0328}").parse_error(&strict_settings())?;
            match error.kind.as_ref() {
                ParseErrorKind::UnknownAccent { accent } => assert_eq!(accent, "̨"),
                other => panic!("Unexpected error kind: {other:?}"),
            }
            Ok(())
        },
    );
}