ikat 0.12.0

Weave Markdown into camera-ready LaTeX: mermaid-to-TikZ, pgfplots figures, publisher templates
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
//! Calibrated display-math width estimation (M7.8 refinement).
//!
//! Glyph widths are probed from the Computer Modern math fonts at the
//! three math styles — display/text (10 pt), script (7 pt),
//! scriptscript (5 pt) — by `examples/auto-proof/math_probe_widths.py`;
//! the static table between the calibration markers is regenerated by
//! `math_emit_table.py`. The structural model reproduces TeX's spacing
//! rules as measured on composites:
//!
//! * scripts take their child-style glyph width plus `\scriptspace`
//!   (0.5 pt) and stack sub over sup (`max`, never the sum —
//!   `x_s^s` measures exactly `x^s`);
//! * fractions take the wider of numerator and denominator in the
//!   child style plus `\nulldelimiterspace` on both sides (2×1.2 pt);
//! * radicals add a style-dependent radical-symbol overhead;
//! * `\sum`-family operators stack their limits in display style
//!   (width = max of glyph and scripts) while `\int`-family and
//!   scripts in script styles place them beside the glyph;
//! * operator names (`\max`, `\lim`, …) widen only when their
//!   below-limits exceed the word;
//! * relations carry two thickmuskip glues, binaries two medmuskip
//!   glues, and `\left`/`\right` carry per-delimiter footprints
//!   (distinct from the plain delimiters, which use other font
//!   variants).
//!
//! The estimate decides column vs. strip vs. split (M7.8): a strip is
//! always legal, an overflow is not, so unknown constructs err on the
//! wide side.

/// Style index: 0 = display/text (10 pt), 1 = script (7 pt),
/// 2 = scriptscript (5 pt).
type Style = usize;

// BEGIN CALIBRATED TABLE (generated by examples/auto-proof/math_emit_table.py)
/// (key, [display, script, scriptscript]) width in cm.
static GLYPH_W: &[(&str, [f64; 3])] = &[
    ("a", [0.1858, 0.1525, 0.1361]),
    ("b", [0.1508, 0.1236, 0.1113]),
    ("c", [0.1521, 0.1256, 0.1141]),
    ("d", [0.1829, 0.1463, 0.1300]),
    ("e", [0.1636, 0.1333, 0.1190]),
    ("f", [0.2099, 0.1646, 0.1410]),
    ("g", [0.1802, 0.1459, 0.1294]),
    ("h", [0.2025, 0.1645, 0.1454]),
    ("i", [0.1211, 0.0994, 0.0938]),
    ("j", [0.1649, 0.1305, 0.1137]),
    ("k", [0.1940, 0.1554, 0.1361]),
    ("l", [0.1118, 0.0903, 0.0844]),
    ("m", [0.3086, 0.2494, 0.2158]),
    ("n", [0.2110, 0.1737, 0.1548]),
    ("o", [0.1704, 0.1387, 0.1235]),
    ("p", [0.1768, 0.1449, 0.1300]),
    ("q", [0.1695, 0.1376, 0.1221]),
    ("r", [0.1683, 0.1381, 0.1241]),
    ("s", [0.1647, 0.1327, 0.1178]),
    ("t", [0.1269, 0.1062, 0.0991]),
    ("u", [0.2012, 0.1662, 0.1487]),
    ("v", [0.1830, 0.1494, 0.1332]),
    ("w", [0.2611, 0.2099, 0.1820]),
    ("x", [0.2009, 0.1594, 0.1391]),
    ("y", [0.1849, 0.1514, 0.1347]),
    ("z", [0.1789, 0.1444, 0.1275]),
    ("A", [0.2636, 0.2114, 0.1821]),
    ("B", [0.2842, 0.2243, 0.1904]),
    ("C", [0.2763, 0.2188, 0.1870]),
    ("D", [0.3007, 0.2374, 0.2011]),
    ("E", [0.2797, 0.2199, 0.1860]),
    ("F", [0.2748, 0.2161, 0.1829]),
    ("G", [0.2763, 0.2188, 0.1870]),
    ("H", [0.3207, 0.2490, 0.2082]),
    ("I", [0.1821, 0.1437, 0.1236]),
    ("J", [0.2287, 0.1787, 0.1525]),
    ("K", [0.3236, 0.2534, 0.2121]),
    ("L", [0.2392, 0.1928, 0.1679]),
    ("M", [0.3793, 0.2944, 0.2448]),
    ("N", [0.3207, 0.2490, 0.2082]),
    ("O", [0.2778, 0.2213, 0.1902]),
    ("P", [0.2745, 0.2168, 0.1843]),
    ("Q", [0.2778, 0.2213, 0.1902]),
    ("R", [0.2696, 0.2130, 0.1813]),
    ("S", [0.2358, 0.1860, 0.1594]),
    ("T", [0.2542, 0.2038, 0.1768]),
    ("U", [0.2783, 0.2178, 0.1850]),
    ("V", [0.2831, 0.2265, 0.1943]),
    ("W", [0.3807, 0.3022, 0.2553]),
    ("X", [0.3188, 0.2496, 0.2090]),
    ("Y", [0.2821, 0.2257, 0.1937]),
    ("Z", [0.2651, 0.2087, 0.1777]),
    ("0", [0.1757, 0.1401, 0.1196]),
    ("1", [0.1757, 0.1401, 0.1196]),
    ("2", [0.1757, 0.1401, 0.1196]),
    ("3", [0.1757, 0.1401, 0.1196]),
    ("4", [0.1757, 0.1401, 0.1196]),
    ("5", [0.1757, 0.1401, 0.1196]),
    ("6", [0.1757, 0.1401, 0.1196]),
    ("7", [0.1757, 0.1401, 0.1196]),
    ("8", [0.1757, 0.1401, 0.1196]),
    ("9", [0.1757, 0.1401, 0.1196]),
    ("alpha", [0.2261, 0.1827, 0.1605]),
    ("beta", [0.2173, 0.1720, 0.1480]),
    ("gamma", [0.2015, 0.1628, 0.1435]),
    ("delta", [0.1695, 0.1367, 0.1206]),
    ("epsilon", [0.1427, 0.1171, 0.1078]),
    ("varepsilon", [0.1639, 0.1328, 0.1186]),
    ("zeta", [0.1797, 0.1438, 0.1265]),
    ("eta", [0.1871, 0.1536, 0.1370]),
    ("theta", [0.1748, 0.1414, 0.1251]),
    ("vartheta", [0.2079, 0.1697, 0.1503]),
    ("mu", [0.2118, 0.1709, 0.1493]),
    ("nu", [0.1960, 0.1573, 0.1375]),
    ("xi", [0.1699, 0.1363, 0.1204]),
    ("pi", [0.2130, 0.1732, 0.1524]),
    ("phi", [0.2094, 0.1690, 0.1479]),
    ("varphi", [0.2299, 0.1849, 0.1613]),
    ("rho", [0.1817, 0.1458, 0.1276]),
    ("sigma", [0.2134, 0.1717, 0.1496]),
    ("tau", [0.1934, 0.1580, 0.1402]),
    ("Gamma", [0.2197, 0.1738, 0.1460]),
    ("Delta", [0.2929, 0.2309, 0.1928]),
    ("Pi", [0.2636, 0.2075, 0.1723]),
    ("wp", [0.2237, 0.1796, 0.1563]),
    ("hbar", [0.1899, 0.1557, 0.1391]),
    ("infty", [0.3515, 0.2802, 0.2392]),
    ("Ra", [0.1757, 0.1401, 0.1196]),
    ("Rb", [0.1953, 0.1552, 0.1318]),
    ("Rc", [0.1562, 0.1250, 0.1074]),
    ("Rd", [0.1953, 0.1552, 0.1318]),
    ("Re", [0.1562, 0.1250, 0.1074]),
    ("Rf", [0.1059, 0.0858, 0.0788]),
    ("Rg", [0.1763, 0.1406, 0.1200]),
    ("Rh", [0.1953, 0.1552, 0.1318]),
    ("Ri", [0.0976, 0.0796, 0.0708]),
    ("Rj", [0.1074, 0.0871, 0.0769]),
    ("Rk", [0.1855, 0.1477, 0.1257]),
    ("Rl", [0.0976, 0.0796, 0.0708]),
    ("Rm", [0.2929, 0.2309, 0.1928]),
    ("Rn", [0.1953, 0.1552, 0.1318]),
    ("Ro", [0.1843, 0.1467, 0.1249]),
    ("Rp", [0.1953, 0.1552, 0.1318]),
    ("Rq", [0.1855, 0.1477, 0.1257]),
    ("Rr", [0.1377, 0.1098, 0.0952]),
    ("Rs", [0.1386, 0.1113, 0.0964]),
    ("Rt", [0.1367, 0.1098, 0.0952]),
    ("Ru", [0.1953, 0.1552, 0.1318]),
    ("Rv", [0.1861, 0.1481, 0.1261]),
    ("Rw", [0.2544, 0.2011, 0.1688]),
    ("Rx", [0.1855, 0.1477, 0.1257]),
    ("Ry", [0.1861, 0.1481, 0.1261]),
    ("Rz", [0.1562, 0.1250, 0.1074]),
    ("R0", [0.1757, 0.1401, 0.1196]),
    ("R1", [0.1757, 0.1401, 0.1196]),
    ("R2", [0.1757, 0.1401, 0.1196]),
    ("R3", [0.1757, 0.1401, 0.1196]),
    ("R4", [0.1757, 0.1401, 0.1196]),
    ("R5", [0.1757, 0.1401, 0.1196]),
    ("R6", [0.1757, 0.1401, 0.1196]),
    ("R7", [0.1757, 0.1401, 0.1196]),
    ("R8", [0.1757, 0.1401, 0.1196]),
    ("R9", [0.1757, 0.1401, 0.1196]),
    ("sim", [0.2734, 0.2197, 0.1904]),
    ("to", [0.3515, 0.2802, 0.2392]),
    ("leq", [0.2734, 0.2197, 0.1904]),
    ("geq", [0.2734, 0.2197, 0.1904]),
    ("in", [0.2343, 0.1894, 0.1660]),
    ("notin", [0.2343, 0.1894, 0.1660]),
    ("approx", [0.2734, 0.2197, 0.1904]),
    ("equiv", [0.2734, 0.2197, 0.1904]),
    ("neq", [0.2734, 0.2158, 0.1806]),
    ("otimes", [0.2734, 0.2197, 0.1904]),
    ("times", [0.2734, 0.2197, 0.1904]),
    ("ast", [0.1757, 0.1440, 0.1294]),
    ("cdot", [0.0976, 0.0835, 0.0805]),
    ("langle", [0.1367, 0.1137, 0.1050]),
    ("rangle", [0.1367, 0.1137, 0.1050]),
    ("cdots", [0.4686, 0.2984, 0.2847]),
    ("vdots", [0.0976, 0.0976, 0.0976]),
    ("Vert", [0.1757, 0.1440, 0.1294]),
    ("eq", [0.2734, 0.2158, 0.1806]),
    ("lt", [0.2734, 0.2197, 0.1904]),
    ("gt", [0.2734, 0.2197, 0.1904]),
    ("plus", [0.2734, 0.2158, 0.1806]),
    ("minus", [0.2734, 0.2197, 0.1904]),
    ("pipe", [0.0976, 0.0835, 0.0805]),
    ("slash", [0.1757, 0.1440, 0.1294]),
    ("lbrace", [0.1757, 0.1440, 0.1294]),
    ("rbrace", [0.1757, 0.1440, 0.1294]),
    ("lpar", [0.1367, 0.1098, 0.0952]),
    ("rpar", [0.1367, 0.1098, 0.0952]),
    ("lbrk", [0.0976, 0.0796, 0.0708]),
    ("rbrk", [0.0976, 0.0796, 0.0708]),
    ("sum", [0.5077, 0.2953, 0.2109]),
    ("int", [0.3515, 0.1894, 0.1353]),
    ("oint", [0.3515, 0.1894, 0.1353]),
    ("prod", [0.4491, 0.2651, 0.1893]),
    ("max", [0.6541, 0.5186, 0.4381]),
    ("min", [0.5858, 0.4657, 0.3954]),
    ("sup", [0.5291, 0.4218, 0.3600]),
    ("inf", [0.4276, 0.3416, 0.2950]),
    ("lim", [0.4881, 0.3900, 0.3344]),
    ("liminf", [0.9743, 0.7796, 0.6725]),
    ("limsup", [1.0759, 0.8598, 0.7375]),
    ("Pr", [0.3768, 0.2987, 0.2533]),
    ("cos", [0.4706, 0.3764, 0.3234]),
    ("sin", [0.4315, 0.3461, 0.2990]),
    ("tan", [0.5077, 0.4052, 0.3466]),
    ("log", [0.4540, 0.3635, 0.3130]),
    ("exp", [0.5370, 0.4279, 0.3649]),
    ("ker", [0.4696, 0.3749, 0.3222]),
];// END CALIBRATED TABLE

/// `\scriptspace` = 0.5 pt: right-bearing of a script over its base.
const SCRIPTSPACE: f64 = 0.0176;
/// 2 × `\nulldelimiterspace` (1.2 pt on each side of a fraction).
const NULLDELIM2: f64 = 0.0843;
/// 3 mu: thin space (`\,`); also the TeX class-glue unit.
const THIN: f64 = 0.0586;
/// 4 mu: medium class glue (binary).
const MED: f64 = 0.0781;
/// 5 mu: thick class glue (relation).
const THICK: f64 = 0.0976;
/// One em at the document size (`\quad`).
const QUAD: f64 = 0.3515;
/// Prime mark (`x'` = base + 2.806 pt).
const PRIME: f64 = 0.0989;
/// Radical-symbol overhead per style (`\sqrt{gggggg}` − 6·g).
const RAD_OVER: [f64; 3] = [0.2929, 0.2309, 0.1928];
/// Matrix inter-column glue: 2 × `\arraycolsep` = 10 pt.
const MAT_GAP: f64 = 0.3515;
/// Delimiter-pair overhead of matrix environments.
fn matrix_brack(env: &str) -> f64 {
    match env {
        "bmatrix" | "Bmatrix" => 0.371,
        "pmatrix" | "vmatrix" => 0.5174,
        _ => 0.0,
    }
}
/// Width inflation of bold math relative to the regular face.
const BOLD_FACTOR: f64 = 1.2;
/// Fallback for commands outside the probed inventory.
const UNKNOWN_CMD: f64 = 0.22;

/// Width of one `\tag`-stripped display span in cm: the widest of its
/// `\\`-separated rows. Public because the display splitter (M7.8)
/// and the emission path both consult it.
pub(crate) fn display_math_width_cm(tex: &str) -> f64 {
    // A display holds one equation; `\\` separates its rows.
    let mut tex = tex.to_string();
    // Excise the tag: it sits in the margin, never binds the width.
    while let Some(i) = tex.find("\\tag{") {
        // Inside the tag's brace already: depth starts at 1.
        let mut depth = 1usize;
        let mut j = i + 5;
        let bytes = tex.as_bytes();
        while j < bytes.len() {
            match bytes[j] {
                b'{' => depth += 1,
                b'}' => {
                    depth -= 1;
                    if depth == 0 {
                        j += 1;
                        break;
                    }
                }
                _ => {}
            }
            j += 1;
        }
        tex.replace_range(i..j.min(tex.len()), "");
    }
    split_display_rows(&tex)
        .iter()
        .map(|row| row_cm(row, 0, false))
        .reduce(f64::max)
        .unwrap_or(0.0)
}

/// Split a display on `\\\\` row breaks, ignoring `\\\\` inside
/// `matrix` environments (their rows size inside-out via `row_cm`;
/// splitting there would measure shards).
fn split_display_rows(tex: &str) -> Vec<&str> {
    let mut rows = Vec::new();
    let mut depth = 0i32;
    let mut start = 0usize;
    let mut i = 0usize;
    while i < tex.len() {
        if let Some((len, is_begin, _)) = matrix_token_at(tex, i) {
            depth = if is_begin { depth + 1 } else { (depth - 1).max(0) };
            i += len;
            continue;
        }
        if depth == 0 && tex[i..].starts_with("\\\\") {
            rows.push(&tex[start..i]);
            i += 2;
            start = i;
            continue;
        }
        i += 1;
    }
    rows.push(&tex[start..]);
    rows
}

/// If `s[i..]` opens or closes a matrix-flavored environment, return
/// the full token length, whether it is a `\begin`, and the bracket
/// overhead of the flavor.
fn matrix_token_at(s: &str, i: usize) -> Option<(usize, bool, f64)> {
    if !s[i..].starts_with('\\') {
        return None;
    }
    let rest = &s[i + 1..];
    let (head, is_begin) = if rest.starts_with("begin{") {
        ("begin{", true)
    } else if rest.starts_with("end{") {
        ("end{", false)
    } else {
        return None;
    };
    let after = &rest[head.len()..];
    let close = after.find('}')?;
    let env = &after[..close];
    let flavor = match env {
        "matrix" | "bmatrix" | "Bmatrix" | "pmatrix" | "vmatrix" => env,
        _ => return None,
    };
    Some((1 + head.len() + close + 1, is_begin, matrix_brack(flavor)))
}

/// Math atom classes for inter-atom glue (TeXbook ch. 18).
const C_START: u8 = 0;
const C_ORD: u8 = 1;
const C_OP: u8 = 2;
const C_BIN: u8 = 3;
const C_REL: u8 = 4;
const C_OPEN: u8 = 5;
const C_CLOSE: u8 = 6;
const C_PUNCT: u8 = 7;
const C_INNER: u8 = 8;

/// Inter-atom glue in units (0 none, 1 thin, 2 med, 3 thick), rows =
/// previous class (0 = list start), cols = current class. Values
/// measured on composites (`x\sum`, `x=x`, `\frac ab x`, ...): they
/// agree with the TeXbook table where the book is explicit and settle
/// the boundary cases (Ord→Open/Close are thin, Rel is thick on both
/// sides, a leading Bin loses its operand glue).
static GLUE: [[u8; 9]; 9] = [
    [0, 0, 0, 0, 0, 0, 0, 0, 0], // start of list
    [0, 0, 1, 2, 3, 1, 0, 0, 0], // Ord (nothing before a closing fence)
    [0, 1, 1, 2, 3, 1, 1, 1, 1], // Op
    [0, 2, 2, 0, 0, 2, 2, 2, 2], // Bin
    [0, 3, 3, 0, 0, 3, 3, 3, 3], // Rel
    [0, 0, 0, 0, 0, 0, 0, 0, 0], // Open
    [0, 1, 1, 2, 3, 1, 1, 1, 1], // Close
    [0, 1, 1, 0, 1, 1, 1, 1, 1], // Punct
    [0, 0, 1, 2, 3, 1, 1, 1, 0], // Inner
];

/// Class glue in cm: zero in script styles (TeX inserts no class
/// glue below display/text style), else thin/med/thick by units.
fn glue_cm(units: u8, style: Style) -> f64 {
    if style > 0 || units == 0 {
        return 0.0;
    }
    match units {
        1 => THIN,
        2 => MED,
        3 => THICK,
        _ => 0.0,
    }
}

/// Does this fragment hold an Inner atom (fraction, nested
/// radical, matrix) at any level? Coarse: nesting inside another
/// wrapper still counts (the thin-pad errs on the wide side).
fn inner_top(s: &str) -> bool {
    s.contains("\\frac") || s.contains("\\sqrt") || s.contains("\\begin{")
}

/// Width of one `\\`-free display row at the given math style.
/// `text_ctx` marks arguments of a fraction/radical: a fraction
/// found there is a text-style fraction whose own children descend
/// to script size (measured on `\frac{\frac{s}{t}}{u}`).
fn row_cm(row: &str, style: Style, text_ctx: bool) -> f64 {
    let mut style = style;
    // Matrices bank their width and leave a zero-width placeholder:
    // a matrix sits INLINE (text + matrix + text sum on one row),
    // so max() would undercount side-by-side content.
    let mut bank = 0.0;
    let mut s = row.to_string();
    loop {
        let Some((brack, mstart, inner, mend)) = find_matrix(&s) else { break };
        // Matrix columns ALIGN: the environment is as wide as the
        // sum of per-column maxima plus inter-column glue — not the
        // max over rows of row sums (a tall narrow column would
        // otherwise undercount the aligned grid).
        let rows: Vec<Vec<&str>> = split_display_rows(&inner)
            .iter()
            .map(|r| r.split('&').collect())
            .collect();
        let ncols = rows.iter().map(|r| r.len()).max().unwrap_or(0);
        let mut total = 0.0;
        for c in 0..ncols {
            total += rows
                .iter()
                .filter_map(|r| r.get(c))
                .map(|cell| row_cm(cell, style, text_ctx))
                .fold(0.0f64, f64::max);
        }
        let widest = total + MAT_GAP * ncols.saturating_sub(1) as f64 + brack;
        bank += widest;
        s = format!("{}\u{E000}{}", &s[..mstart], &s[mend..]);
    }
    let mut w = bank;
    // Previous atom's class for inter-atom glue (None = list start).
    let mut prev = C_START;
    let mut cs = s.chars().peekable();
    while let Some(c) = cs.next() {
        if c == '\\' {
            let mut name = String::new();
            while let Some(&d) = cs.peek() {
                if d.is_ascii_alphabetic() {
                    name.push(d);
                    cs.next();
                } else {
                    break;
                }
            }
            if name.is_empty() {
                // `\{`, `\}`, `\%`, `\,` ... two-char command.
                match cs.next() {
                    Some(',') => w += 0.1389,
                    Some(':') => w += 0.0308,
                    Some(';') => w += 0.0385,
                    Some('!') => w -= THIN,
                    Some('{') | Some('}') => {
                        w += glue_cm(GLUE[prev as usize][C_ORD as usize], style);
                        w += 0.1757;
                        prev = C_ORD;
                    }
                    Some(' ') | Some('\\') | Some('%') | Some('$') | Some('&') => {}
                    Some(_) => {
                        w += glue_cm(GLUE[prev as usize][C_ORD as usize], style);
                        w += UNKNOWN_CMD;
                        prev = C_ORD;
                    }
                    None => {}
                }
                continue;
            }
            // Style switches change the active math size.
            if matches!(
                name.as_str(),
                "displaystyle" | "textstyle" | "scriptstyle" | "scriptscriptstyle"
            ) {
                style = match name.as_str() {
                    "scriptstyle" => 1,
                    "scriptscriptstyle" => 2,
                    _ => 0,
                };
                continue;
            }
            // Operator names: limits stack below in display style
            // (the word only widens when the script exceeds it).
            if matches!(
                name.as_str(),
                "max" | "min" | "sup" | "inf" | "lim" | "liminf" | "limsup" | "Pr"
                    | "cos" | "sin" | "tan" | "log" | "exp" | "ker"
            ) {
                w += glue_cm(GLUE[prev as usize][C_OP as usize], style);
                let atom = glyph_cm(&name, style).unwrap_or(UNKNOWN_CMD);
                let sc = take_scripts(&mut cs, style, text_ctx);
                w += match sc {
                    Some(s) => atom.max(s),
                    None => atom,
                };
                prev = C_OP;
                continue;
            }
            // `\sum`-family: limits stack in display style, sit
            // beside the glyph in script styles.
            if matches!(
                name.as_str(),
                "sum" | "prod" | "coprod" | "bigcup" | "bigcap"
            ) {
                w += glue_cm(GLUE[prev as usize][C_OP as usize], style);
                let atom = glyph_cm(&name, style).unwrap_or(UNKNOWN_CMD);
                let sc = take_scripts(&mut cs, style, text_ctx);
                w += match (sc, style) {
                    (Some(s), 0) => atom.max(s),
                    (Some(s), _) => atom + s + SCRIPTSPACE,
                    (None, _) => atom,
                };
                prev = C_OP;
                continue;
            }
            // `\int`-family: scripts always beside the glyph.
            if matches!(name.as_str(), "int" | "iint" | "iiint" | "oint") {
                w += glue_cm(GLUE[prev as usize][C_OP as usize], style);
                let atom = glyph_cm(&name, style).unwrap_or(UNKNOWN_CMD);
                let sc = take_scripts(&mut cs, style, text_ctx);
                w += atom + sc.map(|s| s + SCRIPTSPACE).unwrap_or(0.0);
                prev = C_OP;
                continue;
            }
            match name.as_str() {
                "left" => {
                    w += glue_cm(GLUE[prev as usize][C_OPEN as usize], style);
                    // Per-delimiter footprint (auto-sized variant,
                    // including its `\nulldelimiterspace`): differs
                    // from the plain delimiter's width.
                    w += lr_delim(&mut cs);
                    prev = C_OPEN;
                }
                "right" => {
                    w += glue_cm(GLUE[prev as usize][C_CLOSE as usize], style);
                    w += lr_delim(&mut cs);
                    prev = C_CLOSE;
                }
                "frac" | "dfrac" | "tfrac" | "sqrt" => {
                    w += glue_cm(GLUE[prev as usize][C_INNER as usize], style);
                    if name == "sqrt" {
                        // Optional `[n]` root index adds nothing.
                        if cs.peek() == Some(&'[') {
                            take_bracket(&mut cs);
                        }
                        let a = take_group(&mut cs);
                        // A radical around an Inner (fraction,
                        // nested radical, matrix) carries one thin
                        // space (measured on `\sqrt{rac{a}{b}}`).
                        let thin_pad =
                            if inner_top(&a) { THIN } else { 0.0 };
                        w += row_cm(&a, style, text_ctx)
                            + RAD_OVER[style.min(2)]
                            + thin_pad;
                    } else {
                        let a = take_group(&mut cs);
                        let b = take_group(&mut cs);
                        // Display fractions set num/den in text
                        // style (same widths); script fractions go
                        // to scriptscript.
                        let child = if style == 0 {
                        if text_ctx { 1 } else { 0 }
                    } else {
                        2
                    };
                        w += row_cm(&a, child, true).max(row_cm(&b, child, true))
                            + NULLDELIM2;
                    }
                    prev = C_INNER;
                }
                "text" | "mathrm" | "textrm" | "operatorname" => {
                    w += glue_cm(GLUE[prev as usize][C_ORD as usize], style);
                    let a = take_group(&mut cs);
                    w += a.chars()
                        .filter(|c| !c.is_whitespace())
                        .map(|c| roman_cm(c, style))
                        .sum::<f64>();
                    prev = C_ORD;
                }
                "mathcal" => {
                    w += glue_cm(GLUE[prev as usize][C_ORD as usize], style);
                    let a = take_group(&mut cs);
                    w += a.chars()
                        .filter(|c| c.is_ascii_uppercase())
                        .map(|c| cal_cm(c, style))
                        .sum::<f64>();
                    prev = C_ORD;
                }
                "mathbb" => {
                    w += glue_cm(GLUE[prev as usize][C_ORD as usize], style);
                    let a = take_group(&mut cs);
                    w += a.chars()
                        .filter(|c| c.is_ascii_uppercase())
                        .map(|c| bb_cm(c, style))
                        .sum::<f64>();
                    prev = C_ORD;
                }
                "mathbf" | "boldsymbol" | "bm" | "bold" => {
                    w += glue_cm(GLUE[prev as usize][C_ORD as usize], style);
                    let a = take_group(&mut cs);
                    w += row_cm(&a, style, text_ctx) * BOLD_FACTOR;
                    prev = C_ORD;
                }
                "overset" | "underset" => {
                    w += glue_cm(GLUE[prev as usize][C_ORD as usize], style);
                    let a = take_group(&mut cs);
                    let b = take_group(&mut cs);
                    // Stacked over/under: max of the parts at the
                    // child style, no scriptspace (measured on
                    // `\overset{\sim}{\vartheta}`).
                    let child = script_style(style);
                    w += row_cm(&a, child, text_ctx).max(row_cm(&b, style, text_ctx));
                    prev = C_ORD;
                }
                "hat" | "bar" | "vec" | "tilde" | "widehat" | "widetilde"
                | "overline" | "underline" | "mathring" | "check" | "breve" => {
                    // Accents/rules add no width: widest arg decides.
                    let a = take_group(&mut cs);
                    w += row_cm(&a, style, text_ctx);
                }
                "quad" => w += QUAD,
                "qquad" => w += 2.0 * QUAD,
                "langle" | "rangle" | "Vert" | "vert" | "cdots" | "vdots"
                | "wp" | "hbar" | "infty" | "ldots" | "dots" => {
                    w += glue_cm(GLUE[prev as usize][C_ORD as usize], style);
                    w += glyph_cm(&name, style).unwrap_or(UNKNOWN_CMD);
                    prev = C_ORD;
                }
                _ => {
                    // Class of this command when bare: relations and
                    // binaries carry their mu-glue; everything else
                    // (Greek letters, dots) is Ord.
                    let bare_cls = if matches!(
                        name.as_str(),
                        "leq" | "geq" | "neq" | "approx" | "equiv" | "sim" | "simeq"
                        | "cong" | "to" | "gets" | "leftarrow" | "rightarrow"
                        | "leftrightarrow" | "Rightarrow" | "Longrightarrow"
                        | "implies" | "in" | "ni" | "notin" | "subset" | "supset"
                        | "subseteq" | "supseteq" | "propto" | "perp" | "parallel"
                        | "mid" | "ll" | "gg" | "prec" | "succ" | "subsetneq"
                    ) {
                        C_REL
                    } else if matches!(
                        name.as_str(),
                        "pm" | "mp" | "times" | "div" | "cdot" | "circ" | "ast"
                        | "star" | "cap" | "cup" | "oplus" | "otimes" | "ominus"
                        | "wedge" | "vee" | "setminus" | "sqcap" | "sqcup"
                    ) {
                        C_BIN
                    } else {
                        C_ORD
                    };
                    // Wrappers (fonts, extenders): the widest braced
                    // argument decides (sub/superscripts attach).
                    let mut best = 0.0f64;
                    let mut any = false;
                    while cs.peek() == Some(&'{') {
                        any = true;
                        let g = take_group(&mut cs);
                        best = best.max(row_cm(&g, style, text_ctx));
                    }
                    if any {
                        w += glue_cm(GLUE[prev as usize][C_ORD as usize], style);
                        w += best;
                        prev = C_ORD;
                    } else {
                        // Probed bare glyph (Greek letters, classed
                        // relations/binaries) or unknown command.
                        w += glue_cm(GLUE[prev as usize][bare_cls as usize], style);
                        w += glyph_cm(&name, style).unwrap_or(UNKNOWN_CMD);
                        prev = bare_cls;
                    }
                }
            }
        } else if c == '^' || c == '_' {
            // `c` is the first script marker (the outer loop consumed
            // it). Scripts stack (sub under sup): width = max of the
            // script contents plus one scriptspace, never the sum
            // (`x_s^s` measures exactly `x^s`). Class unchanged.
            let mut best = script_content(&mut cs, style, text_ctx);
            while cs.peek() == Some(&'_') || cs.peek() == Some(&'^') {
                cs.next();
                best = best.max(script_content(&mut cs, style, text_ctx));
            }
            w += best + SCRIPTSPACE;
        } else if c == '{' || c == '}' || c == '~' || c == ' ' || c == '\t' {
            // Grouping / non-breaking space: no width.
        } else if c == '&' {
            // Alignment tab: the next cell starts a fresh math list
            // (aligned continuations begin with `& = ...`).
            prev = C_START;
        } else if c == '=' || c == '<' || c == '>' {
            // Relation: glyph plus class glue both sides.
            w += glue_cm(GLUE[prev as usize][C_REL as usize], style);
            w += glyph_cm(rel_key(c), style).unwrap_or(UNKNOWN_CMD);
            prev = C_REL;
        } else if c == '+' || c == '-' {
            // Binary op: glyph plus class glue both sides (unary
            // slightly overcounts: safe side).
            w += glue_cm(GLUE[prev as usize][C_BIN as usize], style);
            w += glyph_cm(if c == '+' { "plus" } else { "minus" }, style)
                .unwrap_or(UNKNOWN_CMD);
            prev = C_BIN;
        } else if c == ',' || c == ';' || c == ':' {
            w += glue_cm(GLUE[prev as usize][C_PUNCT as usize], style);
            w += if c == ',' { 0.0976 } else { 0.08 };
            prev = C_PUNCT;
        } else if c == '!' {
            w -= THIN;
        } else if c == '\'' {
            w += PRIME;
        } else if c == '\u{E000}' {
            // Banked matrix placeholder: an Inner atom.
            w += glue_cm(GLUE[prev as usize][C_INNER as usize], style);
            prev = C_INNER;
        } else if c.is_ascii_alphanumeric() {
            w += glue_cm(GLUE[prev as usize][C_ORD as usize], style);
            w += glyph_cm(&c.to_string(), style).unwrap_or(UNKNOWN_CMD);
            prev = C_ORD;
        } else if "()[]|/".contains(c) {
            // Plain delimiters are Ord atoms.
            let key = match c {
                '(' => "lpar",
                ')' => "rpar",
                '[' => "lbrk",
                ']' => "rbrk",
                '|' => "pipe",
                _ => "slash",
            };
            w += glue_cm(GLUE[prev as usize][C_ORD as usize], style);
            w += glyph_cm(key, style).unwrap_or(UNKNOWN_CMD);
            prev = C_ORD;
        } else {
            w += glue_cm(GLUE[prev as usize][C_ORD as usize], style);
            w += UNKNOWN_CMD;
            prev = C_ORD;
        }
    }
    w
}

/// Child style for scripts: 10 pt base → 7 pt; 7/5 pt → 5 pt
/// (`x_s` measures with the sub at script size, not scriptscript).
fn script_style(style: Style) -> Style {
    (style + 1).min(2)
}

/// Relation key for a character.
fn rel_key(c: char) -> &'static str {
    match c {
        '=' => "eq",
        '<' => "lt",
        _ => "gt",
    }
}

/// `\left`/`\right` delimiter footprint including its
/// `\nulldelimiterspace` (1.2 pt): the auto-sized cmex variant is
/// measured separately from the plain delimiter.
fn lr_delim(cs: &mut std::iter::Peekable<std::str::Chars>) -> f64 {
    if cs.peek() == Some(&'\\') {
        cs.next();
        let mut name = String::new();
        while let Some(&d) = cs.peek() {
            if d.is_ascii_alphabetic() {
                name.push(d);
                cs.next();
            } else {
                break;
            }
        }
        return match name.as_str() {
            "langle" | "rangle" | "lceil" | "rceil" | "lfloor" | "rfloor" => 0.1367,
            "Vert" | "vert" | "lVert" | "lvert" => 0.176,
            "lbrace" | "rbrace" => 0.176,
            "uparrow" | "downarrow" | "updownarrow" | "Uparrow" | "Downarrow"
            | "Updownarrow" => 0.176,
            "." => 0.0423,
            _ => 0.1367,
        };
    }
    match cs.next() {
        Some('(') | Some(')') => 0.1367,
        Some('[') | Some(']') => 0.0976,
        Some('{') | Some('}') => 0.176,
        Some('|') | Some('/') => 0.176,
        _ => 0.0423, // `\left.` / `\right.`: nulldelimiter only
    }
}

/// Probed glyph width (cm) at a math style.
fn glyph_cm(key: &str, style: Style) -> Option<f64> {
    GLYPH_W
        .iter()
        .find(|(k, _)| *k == key)
        .map(|(_, v)| v[style.min(2)])
}

/// Roman (cmr) glyph width for `\text`/`\mathrm`: probed per
/// lowercase letter and digit under the `R` prefix.
fn roman_cm(c: char, style: Style) -> f64 {
    if !c.is_ascii_alphanumeric() {
        return 0.16;
    }
    let key = format!("R{}", c);
    glyph_cm(&key, style).unwrap_or(0.16)
}

/// Calligraphic width for `\mathcal`: probed (display style) for the
/// letters the corpus uses; average otherwise; scaled for script
/// styles.
fn cal_cm(c: char, style: Style) -> f64 {
    const CAL_W: &[(char, f64)] = &[
        ('A', 0.2806), ('D', 0.2809), ('F', 0.2875), ('L', 0.2424),
        ('R', 0.2979), ('S', 0.2392), ('T', 0.2807),
    ];
    let d = CAL_W
        .iter()
        .find(|(k, _)| *k == c)
        .map(|(_, v)| *v)
        .unwrap_or(0.2684);
    match style {
        0 => d,
        1 => d * 0.8,
        _ => d * 0.72,
    }
}

/// Blackboard width for `\mathbb`: probed for R/N/E/P; average
/// otherwise; scaled for script styles.
fn bb_cm(c: char, style: Style) -> f64 {
    const BB_W: &[(char, f64)] = &[
        ('R', 0.2538), ('N', 0.2538), ('E', 0.2343), ('P', 0.2148),
    ];
    let d = BB_W
        .iter()
        .find(|(k, _)| *k == c)
        .map(|(_, v)| *v)
        .unwrap_or(0.2391);
    match style {
        0 => d,
        1 => d * 0.8,
        _ => d * 0.72,
    }
}

/// Find the next matrix environment: returns (bracket overhead,
/// start of `\begin`, inner source, end offset past `\end{...}`).
fn find_matrix(s: &str) -> Option<(f64, usize, String, usize)> {
    let mut i = 0usize;
    while i < s.len() {
        let Some((len, true, brack)) = matrix_token_at(s, i) else {
            // Advance to the next backslash (byte-level: a backslash
            // byte never occurs inside a multi-byte UTF-8 char),
            // keeping it: tokens start WITH a backslash, so stepping
            // over it would miss every `\begin`.
            match s[i..].as_bytes().iter().position(|&b| b == b'\\') {
                // A non-matrix command under the cursor: step over
                // its backslash (off = 0) to avoid spinning.
                Some(0) => i += 1,
                Some(off) => i += off,
                None => return None,
            }
            continue;
        };
        let inner_start = i + len;
        // Depth-matched close: nested matrices (A=(...)) must pair
        // with their own end, not the first one in the row.
        let mut d = 1i32;
        let mut k = inner_start;
        let mut close_start = None;
        while k < s.len() {
            if let Some((l2, is_begin, _)) = matrix_token_at(s, k) {
                if is_begin {
                    d += 1;
                } else {
                    d -= 1;
                    if d == 0 {
                        close_start = Some(k);
                    }
                }
                k += l2;
                if close_start.is_some() {
                    break;
                }
                continue;
            }
            k += 1;
        }
        let close_start = close_start?;
        let inner = s[inner_start..close_start].to_string();
        return Some((brack, i, inner, k));
    }
    None
}

/// Consume one `{...}` group (caller ate the `{`), nested-aware.
fn take_group(cs: &mut std::iter::Peekable<std::str::Chars>) -> String {
    let mut out = String::new();
    if cs.peek() != Some(&'{') {
        return out;
    }
    cs.next();
    let mut depth = 1usize;
    for c in cs.by_ref() {
        match c {
            '{' => {
                depth += 1;
                out.push(c);
            }
            '}' => {
                depth -= 1;
                if depth == 0 {
                    break;
                }
                out.push(c);
            }
            _ => out.push(c),
        }
    }
    out
}

/// Consume one `[...]` optional argument.
fn take_bracket(cs: &mut std::iter::Peekable<std::str::Chars>) {
    if cs.peek() == Some(&'[') {
        for c in cs.by_ref() {
            if c == ']' {
                break;
            }
        }
    }
}

/// Measure one script's content: the marker (`^`/`_`) is already
/// consumed; the content is a braced group, a `\command`, or a
/// single character, set at the child style.
fn script_content(
    cs: &mut std::iter::Peekable<std::str::Chars>,
    style: Style,
    text_ctx: bool,
) -> f64 {
    let child = script_style(style);
    if cs.peek() == Some(&'{') {
        let inner = take_group(cs);
        row_cm(&inner, child, text_ctx)
    } else {
        match cs.peek() {
            Some('\\') => {
                cs.next();
                let mut nm = String::new();
                while let Some(&d) = cs.peek() {
                    if d.is_ascii_alphabetic() {
                        nm.push(d);
                        cs.next();
                    } else {
                        break;
                    }
                }
                glyph_cm(&nm, child).unwrap_or(UNKNOWN_CMD)
            }
            Some(_) => {
                let ch = cs.next().unwrap();
                if ch.is_ascii_alphanumeric() {
                    glyph_cm(&ch.to_string(), child).unwrap_or(UNKNOWN_CMD)
                } else {
                    UNKNOWN_CMD
                }
            }
            None => 0.0,
        }
    }
}

/// Take consecutive `^`/`_` script groups (markers still in the
/// stream); returns the widest script content at the child style
/// (scripts stack, so max — not sum).
fn take_scripts(
    cs: &mut std::iter::Peekable<std::str::Chars>,
    style: Style,
    text_ctx: bool,
) -> Option<f64> {
    let mut best: Option<f64> = None;
    while cs.peek() == Some(&'_') || cs.peek() == Some(&'^') {
        cs.next();
        let g = script_content(cs, style, text_ctx);
        best = Some(best.map_or(g, |b: f64| b.max(g)));
    }
    best
}

#[cfg(test)]
mod tests {
    use super::display_math_width_cm as w;

    /// Assert the estimate against TeX-measured truth (cm) within
    /// 0.01: the probe composites in glyphs_raw.json, generated by
    /// examples/auto-proof/math_probe_widths.py.
    fn eq(name: &str, tex: &str, truth_cm: f64) {
        let got = w(tex);
        assert!(
            (got - truth_cm).abs() < 0.01,
            "{name}: got {got:.4}, TeX says {truth_cm:.4}"
        );
    }

    #[test]
    fn calibrated_constructs() {
        // Fractions: max(num, den) + 2 nulldelimiterspace.
        eq("frac-st", r"\frac{s}{t}", 0.249);
        eq("frac-ab", r"\frac{a}{b}", 0.2706);
        // Radicals: content + per-style overhead.
        eq("sqrt-6g", r"\sqrt{gggggg}", 1.3741);
        eq("sqrt-2g", r"\sqrt{gg}", 0.6531);
        // Scripts: base + child glyph + scriptspace; stacked max.
        eq("x-sup-s", "x^{s}", 0.3512);
        eq("x-sub-s", "x_{s}", 0.3512);
        eq("x-subsup-s", "x_{s}^{s}", 0.3512);
        eq("x-sup-g", "x^{g}", 0.3644);
        eq("x-prime", "x'", 0.2995);
        // Relations: thick glue both sides.
        eq("x-eq-x", "x=x", 0.8703);
        eq("x-leq-x", r"x\leq x", 0.8703);
        // Binaries: med glue both sides.
        eq("x-plus-x", "x+x", 0.8313);
        eq("x-otimes-y", r"x\otimes y", 0.8154);
        // Class glue in script styles: none.
        eq("script-eq", r"\scriptstyle x=x", 0.5345);
        eq("script-plus", r"\scriptstyle x+x", 0.5345);
        // Operator names: stacked limits never exceed the word here.
        eq("max-lim", r"\max_{x\in A}", 0.6541);
        eq("lim-lim", r"\lim_{x\in A}", 0.5601);
        eq("sum-lim", r"\sum_{k=0}^{\beta}", 0.5112);
        eq("int-lim", r"\int_{0}^{\zeta}", 0.5129);
        // Op -> Inner takes a thin space.
        eq("sum-frac", r"\sum\frac{a}{b}", 0.8364);
        // Ord -> Inner: none.
        eq("x-frac", r"x\frac{a}{b}", 0.4710);
        eq("frac-x", r"\frac{a}{b}x", 0.4710);
        eq("frac-frac", r"\frac{a}{b}\frac{c}{d}", 0.5374);
        // Ord/Inner -> Open: thin; Ord -> Close: none; Close -> Ord: thin.
        eq("x-lr", r"x\left(x\right)", 0.7337);
        eq("frac-lr", r"\frac{a}{b}\left(x\right)", 0.8029);
        eq("close-x", r"\left(x\right)x", 0.7337);
        // Nested fraction: num/den at text style.
        eq("frac-nest", r"\frac{\frac{s}{t}}{u}", 0.302);
        // Overset: max of the parts, no scriptspace.
        eq("overset-sim", r"\overset{\sim}{\vartheta}", 0.2197);
        // Punctuation: comma glyph; Punct -> Ord thin.
        eq("x-comma", "x,", 0.2985);
        eq("comma-x", ",x", 0.357);
        // Matrices: aligned columns + gap + bracket pair.
        eq("bmatrix", r"\begin{bmatrix} a & b \\ c & d \end{bmatrix}", 1.0906);
        eq("pmatrix", r"\begin{pmatrix} a & b \\ c & d \end{pmatrix}", 1.2372);
        eq("matrix", r"\begin{matrix} a & b \\ c & d \end{matrix}", 0.7198);
    }

    #[test]
    fn calibrated_composites() {
        // Whole-span truths from the probe (composite section).
        eq("vfrac", r"\frac{\vartheta(\zeta)}{\Gamma}", 0.7453);
        eq("vsumlim2", r"\sum_{k=0}^{\beta}\frac{\zeta}{k}", 0.8482);
        eq("vlimfrac", r"\lim_{n\to\infty}\frac{\sigma}{n}", 1.0905);
        eq("vmatmix", r"\begin{bmatrix} \vartheta & \zeta \\ \rho & \sigma \end{bmatrix}", 1.1438);
        eq("vlrainbow", r"\frac{\alpha}{\beta}+\sqrt{\gamma}\cdot\mathcal{S}\leq\Vert\varphi\Vert", 2.7774);
        eq("vunderset", r"\underset{\rho}{\text{max}}", 0.6541);
    }
}