kakemono 0.20.0

Convert PDF, Word and EPUB documents and web pages to HTML, Markdown, EPUB, LaTeX and JSON in Rust
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
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
//! LaTeX output: the document as one `.tex` file that compiles with
//! pdfLaTeX, XeLaTeX or LuaLaTeX, built from the same [`Document`] as the
//! HTML modes. Headings become starred sections, the abstract and lists
//! their environments, rebuilt tables `tabular`s (with `booktabs` rules,
//! `\multicolumn` and `\multirow`), formulas whose structure is known
//! LaTeX math, links `\hyperref` and `\href`; figures (and the other
//! formulas) are pictures of their area of the page, written to an image
//! folder next to the file.
//!
//! The text keeps the order of the PDF: figures and tables are not floats,
//! and their captions keep the label of the paper ("Figure 3:").

pub(crate) mod math;
mod text;
mod vietnamese;

use super::markdown::{linked_anchors, strip_label};
use super::sequence::{AuthorMarkup, Step, author_lines, linked_anchor, push_line, steps, title_spans, write_authors};
use super::{
    CAP_LABEL, Equations, HtmlOptions, ImageStore, Mode, PageImage, caption_first, code, doc_title, equation_math,
    float_class, heading_tag, is_scene_break,
};
use crate::analysis::pipeline::Analysis;
use crate::backend::PdfFile;
use crate::model::{BOLD, Block, BlockKind, Document, FloatKind, ImageSpec, MONO, Span, Table, TableCell};
use crate::render::Pictures;
use math::latex;
use std::cell::RefCell;
use std::collections::BTreeSet;
use text::{LAST_LATIN, LATIN_GAPS, Text, escape, label};

/// The packages and commands that every document uses.
const PREAMBLE: &str = include_str!("preamble.tex");
/// The end of the opening comment of [`PREAMBLE`], where [`CJK_NOTE`] goes.
const PREAMBLE_HEAD_END: &str = "folder next to this one.\n";
/// Said at the top of a document with CJK text.
const CJK_NOTE: &str = "% Its Chinese, Japanese or Korean text needs LuaLaTeX or XeLaTeX: pdfLaTeX\n\
                        % has no font for it and writes a ? for each of its characters.\n";
/// The symbols that the escape of text writes as math, which CJK text uses
/// as characters (△ is the minus sign of Japanese statistics) and the CJK
/// fonts have: in a document with CJK text they are `\kakemonotext`, the
/// character under XeLaTeX and LuaLaTeX, math under pdfLaTeX.
const CJK_TEXT_SYMBOLS: &[(char, &str)] = &[
    ('△', "\\triangle"),
    ('□', "\\square"),
    ('〈', "\\langle"),
    ('〉', "\\rangle"),
];
/// The end of the choice of fonts in [`PREAMBLE`], where [`CJK_FONTS`] go.
const FONTS_END: &str = "  \\usepackage{fontspec}\n\\fi\n";
/// The fonts of CJK text, for a document that has some. Noto Serif and
/// Noto Sans CJK, when installed, have the letters of the four languages;
/// otherwise Harano Aji, which TeX Live ships (LuaTeX-ja's own default,
/// and given to xeCJK by file name so that it is found in any TeX Live),
/// has Japanese only. Bold, sans-serif and monospaced text is in Gothic,
/// as is usual in Japanese; the CJK fonts follow the Latin ones into
/// sans-serif and monospaced text (`match`), and xeCJK keeps the spaces
/// between CJK characters (Korean words are separated by spaces).
/// xeCJK takes the symbols of Japanese text (①, ―, ≦, ●, ☆, box drawing,
/// arrows) for Latin ones and looks for them in Latin Modern, which lacks
/// them: those of the dash, Roman numerals, arrows, mathematical operators,
/// enclosed alphanumerics, box drawing, geometric shapes and miscellaneous
/// symbols blocks that both Noto Serif CJK and Harano Aji Mincho have and
/// Latin Modern has not (checked with `fc-query`) are declared CJK. LuaTeX-ja
/// sets them in its Japanese font by itself.
const CJK_FONTS: &str = r#"\ifLuaTeX
  \usepackage[match]{luatexja-fontspec}
  \IfFontExistsTF{Noto Serif CJK JP}{\setmainjfont{Noto Serif CJK JP}}{}
  \IfFontExistsTF{Noto Sans CJK JP}{\setsansjfont{Noto Sans CJK JP}}{}
\fi
\ifXeTeX
  \usepackage{xeCJK}
  \xeCJKsetup{CJKspace=true}
  \xeCJKDeclareCharClass{CJK}{"2015, "2160->"216B, "2170->"217B, "2194, "2196->"2199,
    "21C4->"21C6, "21CB->"21CC, "21D0, "21D2, "21D4, "21E6->"21E9, "21F5, "2200,
    "2202->"2203, "2205->"220B, "220F, "2211, "2213, "221D, "221F->"2220, "2225->"222E,
    "2234->"2235, "223D, "2243, "2245, "2248, "2260->"2262, "2264->"2267, "226A->"226B,
    "2272->"2273, "2276->"2277, "2282->"2287, "228A->"228B, "2295->"2298, "22A0, "22A5,
    "22BF, "22DA->"22DB, "22EF, "2460->"2492, "249C->"254B, "2550, "255E, "2561, "256A,
    "256D->"2573, "25A0->"25A2, "25AA->"25AB, "25B1->"25B3, "25B6->"25B7, "25BC->"25BD,
    "25C0->"25C1, "25C6->"25C7, "25C9->"25CC, "25CE->"25D3, "25E2->"25E5, "25EF,
    "2600->"2603, "2605->"2606, "260E, "2616->"2617, "261C->"261F, "2640, "2642,
    "2660->"2669, "266B->"266F, "2672->"267D, "26A0, "26BD->"26BE}
  \IfFontExistsTF{Noto Serif CJK JP}
    {\setCJKmainfont{Noto Serif CJK JP}}
    {\setCJKmainfont[BoldFont=HaranoAjiGothic-Bold.otf]{HaranoAjiMincho-Regular.otf}}
  \IfFontExistsTF{Noto Sans CJK JP}
    {\setCJKsansfont{Noto Sans CJK JP}\setCJKmonofont{Noto Sans CJK JP}}
    {\setCJKsansfont[BoldFont=HaranoAjiGothic-Bold.otf]{HaranoAjiGothic-Medium.otf}%
     \setCJKmonofont{HaranoAjiGothic-Medium.otf}}
\fi
% A symbol of CJK text: the character #1, or the math symbol #2 under pdfLaTeX.
\newcommand{\kakemonotext}[2]{\ifPDFTeX\ensuremath{#2}\else#1\fi}
"#;
/// Most body rows of a table set as one box, shrunk to the width of the
/// text; a table with more is a `longtable`, broken across pages with its
/// header repeated: a box never breaks, so a table taller than the page is
/// cut at its bottom, and a huge one exhausts TeX's memory. 30 rows of
/// 11 pt text are about 410 pt high, under two thirds of the text height
/// (650 pt): a table of at most 30 rows, with its caption, always fits on
/// a page.
const MAX_BOXED_TABLE_BODY_ROWS: usize = 30;
/// The font and column spacing of a long table, which is not shrunk to the
/// width of the text: (most columns, size command, font size in pt,
/// `\tabcolsep` in pt). The first that the table's column count fits is
/// tried first; when the table is then wider than the text, the next ones
/// in turn. `\tiny` is only such a last resort. The sizes are those of the
/// 11 pt `article` class, whose `\tabcolsep` is 6 pt.
const LONG_TABLE_SIZES: &[(usize, &str, f32, f32)] = &[
    (4, "", 10.95, 6.0),
    (6, "\\small", 10.0, 4.0),
    (9, "\\footnotesize", 9.0, 3.0),
    (usize::MAX, "\\scriptsize", 8.0, 2.0),
    (usize::MAX, "\\tiny", 6.0, 1.0),
];
/// A table with no geometry (read from a workbook or a Word document) too
/// wide for the text even in `\scriptsize`: shrunk to the width of the text
/// in a box, it would be unreadable, so it is a `longtable` (turned, or cut
/// into groups of columns, when it must be). A paper's table, which fitted
/// its page, is boxed as before.
fn too_wide_to_box(table: &Table) -> bool {
    let readable = &LONG_TABLE_SIZES[..LONG_TABLE_SIZES.len() - 1];
    table.geom.col_bounds.is_empty()
        && !readable
            .iter()
            .any(|size| Columns::fitted(table, size.2, size.3, TEXT_WIDTH_PT).fits())
}

/// The height of the text in pt: letter paper with the 2.5 cm margins of
/// [`PREAMBLE`]. Only used to guess whether the header of a long table is
/// too tall to repeat.
const TEXT_HEIGHT_PT: f32 = 650.0;
/// The distance between the lines of a table, for its font size (that of
/// LaTeX's sizes).
const LINE_HEIGHT_RATIO: f32 = 1.2;
/// The share of the height of the text above which the header of a long
/// table is only on its first page, not repeated on each: a header of
/// narrow wrapped columns can take most of a page.
const LONG_TABLE_MAX_HEAD_RATIO: f32 = 0.25;
/// The widest a wrapped column of a long table is kept for its typical
/// cell, in em: it is at least as wide as the mean of its body cells (or
/// its longest word) up to this, so that a short label (a name, a CJK
/// term of up to 6 characters) is on one line. A table that then does not
/// fit is set smaller, turned or cut into groups of columns.
const TYPICAL_CELL_MAX_EM: f32 = 6.0;
/// The width of the text in pt: letter paper with the 2.5 cm margins of
/// [`PREAMBLE`]. Only used to guess whether a long table fits the line.
const TEXT_WIDTH_PT: f32 = 472.0;
/// The width guessed for a CJK character (fullwidth) in em.
const CJK_CHAR_EM: f32 = 1.0;
/// The width guessed for a capital letter, `m` or `w` in em (those of
/// Latin Modern are 0.68 to 0.83 em).
const WIDE_CHAR_EM: f32 = 0.75;
/// The width guessed for `f i j l r t`, punctuation and a space in em
/// (0.28 to 0.39 em in Latin Modern).
const NARROW_CHAR_EM: f32 = 0.33;
/// The width guessed for any other character in em (a figure is 0.5 em).
const OTHER_CHAR_EM: f32 = 0.5;
/// How much wider bold text is (Latin Modern bold against regular, 1.15).
const BOLD_WIDTH_RATIO: f32 = 1.15;
/// How much wider, in em, the text of a small size is than at 10 pt: Latin
/// Modern draws small sizes from wider designs. (smallest font size in pt
/// it applies to, ratio), largest first; measured on the alphabet and the
/// figures with pdfLaTeX (`\tiny`: 0.61 em a letter against 0.49 em).
const SIZE_WIDTH_RATIOS: &[(f32, f32)] = &[(10.0, 1.0), (9.0, 1.03), (8.0, 1.06), (0.0, 1.22)];
/// The narrowest a column of a long table is ever made, in em of its font:
/// the room for one CJK character.
const MIN_COLUMN_EM: f32 = 1.0;
/// The share of the width of the text that the guessed widths of the
/// columns of a long table may fill: the guesses run short by up to a
/// tenth (bold cells, wide letters), and a table wider than the text runs
/// off the page.
const LONG_TABLE_FILL_RATIO: f32 = 0.9;
/// How much more than [`LONG_TABLE_FILL_RATIO`] of the width of the text
/// the columns of a long table may be guessed to take and still be taken
/// to fit (rounding).
const FIT_TOLERANCE_RATIO: f32 = 0.001;
/// The share of the width of the text that the guessed widths of a group
/// of columns may fill (see `column_groups`): the guesses run short in the
/// smallest fonts, whose figures are relatively wider.
const COLUMN_GROUP_FILL_RATIO: f32 = 0.85;
/// The narrowest a wrapped column of a long table is made, in em of its font.
const MIN_WRAPPED_COLUMN_EM: f32 = 4.0;
/// The packages of a long table, loaded when the document has one.
const LONG_TABLE_PACKAGES: &str = "\\usepackage{longtable}\n\\usepackage{array}\n";
/// The package of a table on landscape pages, loaded when the document has
/// one.
const LANDSCAPE_PACKAGE: &str = "\\usepackage{pdflscape}\n";
/// Where [`LONG_TABLE_PACKAGES`] go in [`PREAMBLE`].
const TABLE_PACKAGES_END: &str = "\\usepackage{multirow}\n";
/// Lists LaTeX can nest inside a list (it allows four levels in all).
const MAX_LIST_NESTING: u8 = 3;
/// The encoding of pdfLaTeX's fonts, in [`PREAMBLE`].
const T1_FONTENC: &str = "\\usepackage[T1]{fontenc}";
/// The encodings of a document with Vietnamese letters (see
/// [`vietnamese`]): T5 for those letters, T1 (the last, the main one) for
/// the rest.
const T5_FONTENC: &str = "\\usepackage[T5,T1]{fontenc}";
/// Indentation of a table of contents, per level, in em.
const CONTENTS_INDENT_EM: f32 = 1.5;
/// Hanging indentation of a bibliography entry.
const REFERENCE_INDENT: &str = "1.5em";
/// Share of the characters of a paragraph that are monospaced from which
/// it is set ragged right.
const RAGGED_MONO_RATIO: f32 = 0.5;
/// Space between the author cards that stand side by side.
const AUTHOR_GAP: &str = "2em";
/// Spaces that a tab stands for in a code listing (`verbatim` writes a tab
/// as one space).
const TAB_SPACES: &str = "    ";

/// Everything that controls how a document is written as LaTeX.
#[derive(Debug, Clone)]
pub struct LatexOptions {
    /// Directory (relative to the `.tex` file) of the picture files. LaTeX
    /// cannot embed pictures: they are always files. Characters other than
    /// ASCII letters, digits, `.`, `_`, `-` and `/` are replaced by `_`,
    /// since TeX gives some of them a meaning.
    pub asset_dir: String,
    /// Pixels per PDF point of the pictures of figures and formulas.
    pub scale: f32,
    /// Worker threads for drawing the pictures (0 = one per CPU).
    pub jobs: usize,
    /// How displayed formulas are written: [`Equations::MathMl`] gives LaTeX
    /// math rebuilt from the glyphs where the structure of the formula is
    /// clear (and inside the text), pictures elsewhere.
    pub equations: Equations,
}

impl Default for LatexOptions {
    /// Pictures written to `images/` at 3 pixels per point, formulas as
    /// LaTeX where their structure is clear, one worker thread per CPU.
    fn default() -> Self {
        Self {
            asset_dir: "images".into(),
            scale: 3.0,
            jobs: 0,
            equations: Equations::MathMl,
        }
    }
}

/// The result of the LaTeX output: the source and the picture files it
/// refers to.
pub struct LatexOutput {
    /// The LaTeX document (`.tex`).
    pub latex: String,
    /// Files to write next to the `.tex` file: (relative path, contents).
    pub files: Vec<(String, Vec<u8>)>,
}

/// Writes an analysed document as LaTeX.
///
/// The text is written first, with placeholders for the pictures; the
/// pictures are then drawn from `pdf` (on `opts.jobs` threads) and returned
/// in [`LatexOutput::files`].
pub fn render_latex(analysis: &Analysis, pdf: Option<&PdfFile>, opts: &LatexOptions) -> LatexOutput {
    // The footnotes go to the foot of the page of their calls.
    let attached = crate::render::notes::footnotes_attached(analysis);
    let analysis = attached.as_ref().unwrap_or(analysis);
    let html_opts = HtmlOptions {
        mode: Mode::Friendly,
        inline_images: false,
        asset_dir: tex_path(&opts.asset_dir),
        scale: opts.scale,
        page_image: PageImage::Svg,
        jobs: opts.jobs,
        embed_fonts: false,
        equations: opts.equations,
    };
    let mut images = ImageStore {
        opts: &html_opts,
        jobs: Vec::new(),
        svg_as_pdf: true,
    };
    let doc = &analysis.doc;
    let anchors = linked_anchors(doc);
    let mut writer = Writer {
        opts,
        text: Text {
            anchors: &anchors,
            math: opts.equations == Equations::MathMl,
            table_notes: RefCell::default(),
        },
        images: &mut images,
        out: String::new(),
        group: Group::None,
        item_open: false,
        nested: 0,
        after_text: false,
    };
    writer.write(doc);
    let body = writer.out.trim_end().to_string();
    let out = images.finish(Pictures::new(pdf, &analysis.doc), body);
    LatexOutput {
        latex: document(doc, &out.html),
        files: out.files,
    }
}

/// The whole `.tex` file: the preamble (naming the format the document
/// was read from, with the CJK fonts of XeLaTeX and LuaLaTeX when
/// the text has CJK characters), the characters that pdfLaTeX has no glyph
/// for, the title of the PDF, and `body`.
fn document(doc: &Document, body: &str) -> String {
    let title = escape(&doc_title(doc));
    let cjk = title.chars().chain(body.chars()).any(is_cjk);
    // The symbols of CJK text written as math are characters of the CJK
    // fonts under XeLaTeX and LuaLaTeX: as text, they stay in the text of
    // the PDF (math symbols are lost to its text extraction).
    let (title, body) = if cjk {
        let as_text = |text: &str| {
            CJK_TEXT_SYMBOLS
                .iter()
                .fold(text.to_string(), |text, (symbol, command)| {
                    text.replace(
                        &format!("\\ensuremath{{{command}}}"),
                        &format!("\\kakemonotext{{{symbol}}}{{{command}}}"),
                    )
                })
        };
        (as_text(&title), as_text(body))
    } else {
        (title, body.to_string())
    };
    let body = body.as_str();
    let (vietnamese, unknown): (Vec<char>, Vec<char>) = unknown_chars(&title, body)
        .into_iter()
        .partition(|&ch| vietnamese::t5_letter(ch).is_some());
    let mut out = if vietnamese.is_empty() {
        String::from(PREAMBLE)
    } else {
        // T1 stays the main encoding (the last one loaded).
        PREAMBLE.replacen(T1_FONTENC, T5_FONTENC, 1)
    };
    out = out.replacen(
        "Converted from a PDF",
        &format!("Converted from {}", crate::render::source_phrase(doc)),
        1,
    );
    if body.contains("\\begin{longtable}") {
        let landscape = if body.contains("\\begin{landscape}") {
            LANDSCAPE_PACKAGE
        } else {
            ""
        };
        out = out.replacen(
            TABLE_PACKAGES_END,
            &format!("{TABLE_PACKAGES_END}{LONG_TABLE_PACKAGES}{landscape}"),
            1,
        );
    }
    if cjk {
        out = out.replacen(PREAMBLE_HEAD_END, &format!("{PREAMBLE_HEAD_END}{CJK_NOTE}"), 1);
        out = out.replacen(FONTS_END, &format!("{FONTS_END}{CJK_FONTS}"), 1);
    }
    if !vietnamese.is_empty() {
        out.push_str(&format!(
            "% Vietnamese letters: pdfLaTeX sets them in the T5 encoding (vntex).\n\\ifPDFTeX\n  \\newcommand{{{cmd}}}[1]{{{{\\fontencoding{{T5}}\\selectfont#1}}}}\n",
            cmd = vietnamese::T5_COMMAND
        ));
        for ch in vietnamese {
            let letter = vietnamese::t5_letter(ch).expect("partitioned on t5_letter");
            out.push_str(&format!(
                "  \\DeclareUnicodeCharacter{{{:04X}}}{{{}{{{letter}}}}}\n",
                u32::from(ch),
                vietnamese::T5_COMMAND
            ));
        }
        out.push_str("\\fi\n");
    }
    if !unknown.is_empty() {
        out.push_str("% Characters that the fonts of pdfLaTeX do not have.\n\\ifPDFTeX\n");
        for ch in unknown {
            out.push_str(&format!(
                "  \\DeclareUnicodeCharacter{{{:04X}}}{{\\kakemonomissing}}\n",
                u32::from(ch)
            ));
        }
        out.push_str("\\fi\n");
    }
    out.push_str(&format!(
        "\\hypersetup{{pdftitle={{{title}}}}}\n\n\\begin{{document}}\n\n"
    ));
    out.push_str(body);
    out.push_str("\n\n\\end{document}\n");
    out
}

/// Whether `ch` is Chinese, Japanese or Korean: a Han ideograph, kana,
/// Hangul, Bopomofo, CJK punctuation or a fullwidth form. The fonts of
/// [`PREAMBLE`] have none of them; [`CJK_FONTS`] gives them fonts.
fn is_cjk(ch: char) -> bool {
    matches!(
        u32::from(ch),
        0x1100..=0x11FF // Hangul Jamo
            | 0x2E80..=0x2FDF // CJK and Kangxi radicals
            | 0x3000..=0x303F // CJK symbols and punctuation
            | 0x3040..=0x30FF // Hiragana, Katakana
            | 0x3100..=0x312F // Bopomofo
            | 0x3130..=0x31FF // Hangul compatibility Jamo, kanbun, Bopomofo extended, CJK strokes, Katakana extension
            | 0x3200..=0x33FF // Enclosed CJK, CJK compatibility
            | 0x3400..=0x4DBF // Han, extension A
            | 0x4E00..=0x9FFF // Han
            | 0xA960..=0xA97F // Hangul Jamo extended A
            | 0xAC00..=0xD7FF // Hangul syllables, Jamo extended B
            | 0xF900..=0xFAFF // CJK compatibility ideographs
            | 0xFE10..=0xFE1F // Vertical forms
            | 0xFE30..=0xFE4F // CJK compatibility forms
            | 0xFF00..=0xFFEF // Halfwidth and fullwidth forms
            | 0x20000..=0x3FFFF // Han, extensions B and after
    )
}

/// The characters of `title` and `body` that pdfLaTeX's text fonts lack:
/// those left as they are by [`escape`] and by the math, outside the Latin
/// blocks.
fn unknown_chars(title: &str, body: &str) -> BTreeSet<char> {
    title
        .chars()
        .chain(body.chars())
        .filter(|&ch| !ch.is_ascii() && (ch > LAST_LATIN || LATIN_GAPS.contains(&ch)))
        .collect()
}

/// `dir` with the characters that TeX file names cannot safely hold
/// replaced by `_`, and without a trailing slash.
fn tex_path(dir: &str) -> String {
    dir.trim_end_matches(['/', '\\'])
        .chars()
        .map(|ch| {
            if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '_' | '-' | '/') {
                ch
            } else {
                '_'
            }
        })
        .collect()
}

/// The environment that is open around the blocks being written.
#[derive(PartialEq, Clone, Copy)]
enum Group {
    /// None.
    None,
    /// A list (`itemize`).
    List,
    /// The abstract (`abstract`).
    Abstract,
}

impl Group {
    /// The group of `block`, given the one that is open: equations do not
    /// interrupt an abstract.
    fn of(block: &Block, current: Self) -> Self {
        match &block.kind {
            BlockKind::ListItem { .. } | BlockKind::ListContinuation => Self::List,
            BlockKind::Paragraph { in_abstract: true } => Self::Abstract,
            BlockKind::Equation { .. } if current == Self::Abstract => current,
            _ => Self::None,
        }
    }

    /// The name of the environment of the group.
    fn environment(self) -> Option<&'static str> {
        match self {
            Self::None => None,
            Self::List => Some("itemize"),
            Self::Abstract => Some("abstract"),
        }
    }
}

/// Writes the LaTeX of a document, block by block.
struct Writer<'a, 'r> {
    /// The output options.
    opts: &'a LatexOptions,
    /// Writes the text of the blocks.
    text: Text<'a>,
    /// The pictures; each file name is a placeholder until the end.
    images: &'a mut ImageStore<'r>,
    /// The LaTeX so far.
    out: String,
    /// The environment that is open.
    group: Group,
    /// An `\item` of the open list has been written.
    item_open: bool,
    /// Lists open inside the open list.
    nested: u8,
    /// The block written last is a paragraph of text: a displayed formula
    /// after it continues it (no blank line, which would start a new
    /// paragraph above the formula).
    after_text: bool,
}

impl Writer<'_, '_> {
    /// Writes every block of `doc`, in the steps of [`steps`]. The heading
    /// of the abstract is left to the `abstract` environment.
    fn write(&mut self, doc: &Document) {
        let blocks: Vec<&Block> = doc.blocks.iter().collect();
        for step in steps(&blocks, doc.meta.arxiv_id.is_some()) {
            match step {
                Step::Source => self.arxiv_link(doc),
                Step::Code(run) => {
                    self.set_group(Group::None);
                    self.anchor(run[0]);
                    self.verbatim(&code::listing_lines(run));
                }
                Step::Title(run) => {
                    self.set_group(Group::of(run[0], self.group));
                    self.title(doc, run);
                }
                Step::Authors(run) => {
                    self.set_group(Group::of(run[0], self.group));
                    write_authors(self, run);
                }
                Step::AbstractHeading(block) => {
                    self.set_group(Group::of(block, self.group));
                    self.anchor(block);
                }
                Step::NumberedEquation(block, number) => {
                    self.set_group(Group::of(block, self.group));
                    if let BlockKind::Equation { image, .. } = &block.kind {
                        self.equation(block, image.as_ref(), Some(&number));
                    }
                }
                Step::Contents(run) => {
                    for block in run {
                        self.set_group(Group::of(block, self.group));
                        self.block(block);
                    }
                }
                Step::Block(block) => {
                    self.set_group(Group::of(block, self.group));
                    self.block(block);
                }
            }
        }
        self.set_group(Group::None);
    }

    /// Closes the open environment and opens that of `group`, when they
    /// differ.
    fn set_group(&mut self, group: Group) {
        if group == self.group {
            return;
        }
        self.nest(0);
        if let Some(env) = self.group.environment() {
            self.paragraph(&format!("\\end{{{env}}}"));
        }
        if let Some(env) = group.environment() {
            self.paragraph(&format!("\\begin{{{env}}}"));
        }
        self.group = group;
        self.item_open = false;
    }

    /// Writes one block.
    fn block(&mut self, block: &Block) {
        if !matches!(
            block.kind,
            BlockKind::ListItem { .. } | BlockKind::ListContinuation | BlockKind::Equation { .. }
        ) {
            self.anchor(block);
        }
        match &block.kind {
            BlockKind::Title => self.title_text(&self.text.spans(&block.spans)),
            BlockKind::Authors => write_authors(self, std::slice::from_ref(&block)),
            BlockKind::Note => self.text_paragraph(&block.spans),
            BlockKind::Heading { level } => self.heading(block, *level),
            BlockKind::Paragraph { .. } if is_scene_break(&block.spans) => {
                self.paragraph("\\begin{center}\n* * *\n\\end{center}");
            }
            BlockKind::Paragraph { .. } => self.text_paragraph(&block.spans),
            BlockKind::ListItem { marker, level } => self.list_item(block, marker.as_deref(), *level),
            BlockKind::ListContinuation => self.list_continuation(block),
            BlockKind::Reference { .. } => {
                let text = self.text.spans(&block.spans);
                if !text.is_empty() {
                    self.paragraph(&format!(
                        "{{\\small\\noindent\\hangindent={REFERENCE_INDENT}\\relax {text}\\par}}"
                    ));
                }
            }
            BlockKind::Footnote { marker } => self.footnote(block, marker),
            BlockKind::Equation { image, number } => self.equation(block, image.as_ref(), number.as_deref()),
            BlockKind::Figure { kind, image, .. } => self.figure(block, *kind, image.as_ref()),
            BlockKind::ContentsEntry { level, page } => self.contents_entry(block, *level, page),
            BlockKind::Code => {
                let lines: Vec<String> = block
                    .rows
                    .iter()
                    .map(|row| row.iter().map(|span| span.text.as_str()).collect())
                    .collect();
                self.verbatim(&lines);
            }
        }
    }

    /// Writes `text` as a block, followed by a blank line.
    fn paragraph(&mut self, text: &str) {
        self.out.push_str(text);
        self.out.push_str("\n\n");
        self.after_text = false;
    }

    /// Writes spans as a paragraph of running text. A paragraph that is
    /// mostly monospaced is set ragged right: the spaces of a monospaced
    /// font do not stretch to justify its lines.
    fn text_paragraph(&mut self, spans: &[Span]) {
        let text = self.text.spans(spans);
        if text.is_empty() {
            return;
        }
        if mono_ratio(spans) >= RAGGED_MONO_RATIO {
            self.paragraph(&format!("{{\\raggedright {text}\\par}}"));
        } else {
            self.paragraph(&text);
        }
        self.after_text = true;
    }

    /// The `\label` that marks the place of a block that some text links
    /// to, or nothing.
    fn anchor_label(&self, block: &Block) -> String {
        linked_anchor(block, self.text.anchors)
            .map(|anchor| format!("\\phantomsection\\label{{{}}}", label(anchor)))
            .unwrap_or_default()
    }

    /// Writes the `\label` of a block that some text links to, on a line
    /// of its own.
    fn anchor(&mut self, block: &Block) {
        let label = self.anchor_label(block);
        push_line(&mut self.out, &label);
    }

    /// Writes a link to the arXiv page of the paper, after the front matter.
    fn arxiv_link(&mut self, doc: &Document) {
        if let Some(id) = &doc.meta.arxiv_id {
            let link = format!("\\href{{https://arxiv.org/abs/{}}}{{arXiv:{}}}", escape(id), escape(id));
            self.paragraph(&format!("\\begin{{center}}\n{link}\n\\end{{center}}"));
        }
    }

    /// Writes a run of title blocks as one title.
    fn title(&mut self, doc: &Document, run: &[&Block]) {
        for block in run {
            self.anchor(block);
        }
        let text = self.text.spans(&title_spans(run));
        if text.is_empty() {
            self.title_text(&escape(&doc_title(doc)));
        } else {
            self.title_text(&text);
        }
    }

    /// Writes the title of the document, centred.
    fn title_text(&mut self, text: &str) {
        self.paragraph(&format!(
            "\\begin{{center}}\n{{\\LARGE\\bfseries {text}\\par}}\n\\end{{center}}"
        ));
    }

    /// Writes a heading as a starred section (the number, if any, is part
    /// of its text): `\section*` for the sections of the paper.
    fn heading(&mut self, block: &Block, level: u8) {
        let text = self.text.spans(&block.spans);
        if text.is_empty() {
            return;
        }
        let command = match heading_tag(level) {
            "h2" => "section",
            "h3" => "subsection",
            "h4" => "subsubsection",
            _ => "paragraph",
        };
        self.paragraph(&format!("\\{command}*{{{text}}}"));
    }

    /// Writes a list item: `\item[3.]` with the marker of the paper, its
    /// text after it.
    fn list_item(&mut self, block: &Block, marker: Option<&str>, level: u8) {
        self.nest(level.min(MAX_LIST_NESTING));
        let rest = marker.and_then(|marker| strip_label(&block.spans, marker));
        let item = match (marker, &rest) {
            (Some(marker), Some(_)) => format!("\\item[{{{}}}]", escape(marker)),
            _ => "\\item".to_string(),
        };
        let text = self.text.spans(rest.as_deref().unwrap_or(&block.spans));
        let text = if rest.is_some() { text } else { protect_start(&text) };
        let label = self.anchor_label(block);
        self.paragraph(format!("{item} {label}{text}").trim_end());
        self.item_open = true;
    }

    /// Opens or closes lists inside the open list until `level` are open.
    fn nest(&mut self, level: u8) {
        while self.nested > level {
            self.paragraph("\\end{itemize}");
            self.nested -= 1;
            self.item_open = true;
        }
        while self.nested < level {
            if !self.item_open {
                self.out.push_str("\\item[]\n");
            }
            self.paragraph("\\begin{itemize}");
            self.nested += 1;
            self.item_open = false;
        }
    }

    /// Writes a paragraph that continues a list item.
    fn list_continuation(&mut self, block: &Block) {
        if !self.item_open {
            self.out.push_str("\\item[]\n");
            self.item_open = true;
        }
        let text = self.text.spans(&block.spans);
        let label = self.anchor_label(block);
        if !text.is_empty() || !label.is_empty() {
            self.paragraph(&format!("{label}{text}"));
        }
    }

    /// Writes a footnote, small, with its mark raised; a note without a
    /// mark (a note in the margin of a book) as a quotation.
    fn footnote(&mut self, block: &Block, marker: &str) {
        let text = self.text.spans(&block.spans);
        if text.is_empty() {
            return;
        }
        if marker.is_empty() {
            self.paragraph(&format!("\\begin{{quote}}\n\\small {text}\n\\end{{quote}}"));
        } else {
            self.paragraph(&format!(
                "{{\\footnotesize\\noindent\\textsuperscript{{{}}}\\,{text}\\par}}",
                escape(marker)
            ));
        }
    }

    /// Writes an entry of a table of contents: indented by its level, with
    /// a leader to its page number.
    fn contents_entry(&mut self, block: &Block, level: u8, page: &str) {
        let text = self.text.spans(&block.spans);
        if text.is_empty() {
            return;
        }
        let indent = f32::from(level.saturating_sub(1)) * CONTENTS_INDENT_EM;
        let page = if page.is_empty() {
            String::new()
        } else {
            format!("\\dotfill {}", escape(page))
        };
        self.paragraph(&format!("\\noindent\\hspace*{{{indent}em}}{text}{page}\\par"));
    }

    /// Writes code lines as a `verbatim` listing.
    fn verbatim(&mut self, lines: &[String]) {
        let body: Vec<String> = lines
            .iter()
            .map(|line| {
                line.replace('\t', TAB_SPACES)
                    .replace("\\end{verbatim}", "\\end {verbatim}")
            })
            .collect();
        let body = body.join("\n");
        let body = body.trim_end();
        if body.trim().is_empty() {
            return;
        }
        self.paragraph(&format!(
            "\\begin{{small}}\n\\begin{{verbatim}}\n{body}\n\\end{{verbatim}}\n\\end{{small}}"
        ));
    }

    /// Writes a displayed formula: as LaTeX math when its structure is
    /// known (and wanted), as a picture otherwise; with its number. It
    /// continues the paragraph before it.
    fn equation(&mut self, block: &Block, image: Option<&ImageSpec>, number: Option<&str>) {
        let body = match (equation_math(block, image, self.opts.equations), image) {
            (Some(math), _) => latex(math).replace(['\n', '\r'], " "),
            (None, Some(image)) => format!("\\vcenter{{\\hbox{{{}}}}}", self.picture(image, "eq")),
            (None, None) => format!("\\text{{{}}}", self.text.spans(&block.spans)),
        };
        let tag = number
            .map(|number| number.trim().trim_start_matches('(').trim_end_matches(')').trim())
            .filter(|tag| !tag.is_empty());
        let display = match tag {
            Some(tag) => format!(
                "\\begin{{equation*}}\n{body}\n\\tag{{{}}}\n\\end{{equation*}}",
                escape(tag)
            ),
            None => format!("\\[\n{body}\n\\]"),
        };
        if self.after_text && self.out.ends_with("\n\n") {
            self.out.pop();
        }
        let label = self.anchor_label(block);
        if label.is_empty() {
            self.paragraph(&display);
        } else {
            self.paragraph(&format!("{label}\n{display}"));
        }
    }

    /// Writes a float: the rebuilt table or the picture, and the caption
    /// on the side it had in the paper. A picture and its caption stay on
    /// one page. A float whose text is all code is a listing.
    fn figure(&mut self, block: &Block, kind: FloatKind, image: Option<&ImageSpec>) {
        let caption = (!block.spans.is_empty()).then(|| self.caption(&block.spans));
        if kind == FloatKind::Algorithm
            && block.table.is_none()
            && let Some(lines) = code::float_listing_lines(block)
        {
            if let Some(caption) = &caption {
                self.paragraph(caption);
            }
            return self.verbatim(&lines);
        }
        let caption_first = caption_first(block, image);
        let content = if let Some(table) = block.table.as_ref().filter(|table| !table.rows.is_empty()) {
            Some(
                if table.rows.len().saturating_sub(table.header_rows) > MAX_BOXED_TABLE_BODY_ROWS
                    || too_wide_to_box(table)
                {
                    return self.paragraph(&self.longtable(table, caption.as_deref(), caption_first));
                } else {
                    // Footnotes are lost inside a table: its cells get the
                    // marks, and the notes follow the table.
                    *self.text.table_notes.borrow_mut() = Some(Vec::new());
                    let tabular = self.tabular(table);
                    let notes = self.text.table_notes.borrow_mut().take().unwrap_or_default();
                    format!(
                        "\\begin{{center}}\n\\begin{{kakemonotable}}\n{tabular}\n\\end{{kakemonotable}}\n\\end{{center}}{}",
                        footnote_texts(&notes)
                    )
                },
            )
        } else {
            image.map(|spec| {
                format!(
                    "\\begin{{center}}\n{}\n\\end{{center}}",
                    self.picture(spec, float_class(kind))
                )
            })
        };
        let mut parts: Vec<String> = Vec::new();
        if caption_first && let Some(caption) = &caption {
            parts.push(caption.clone());
        }
        parts.extend(content);
        if !caption_first && let Some(caption) = caption {
            parts.push(caption);
        }
        if parts.is_empty() {
            return;
        }
        if block.table.is_none() && image.is_some() {
            self.paragraph(&format!(
                "\\noindent\\begin{{minipage}}{{\\linewidth}}\n{}\n\\end{{minipage}}\n\\medskip",
                parts.join("\n\n")
            ));
        } else {
            for part in parts {
                self.paragraph(&part);
            }
        }
    }

    /// A caption, small, with its label ("Figure 3:") in bold.
    fn caption(&self, spans: &[Span]) -> String {
        let text = if let Some(first) = spans.first()
            && let Some(found) = CAP_LABEL.find(&first.text)
        {
            let label = first.text[..found.end()].trim_end();
            let mut rest = spans.to_vec();
            rest[0].text = first.text[label.len()..].to_string();
            format!("\\textbf{{{}}} {}", escape(label), self.text.spans(&rest))
                .trim_end()
                .to_string()
        } else {
            self.text.spans(spans)
        };
        format!("{{\\small {text}\\par}}")
    }

    /// The picture of a page area, at its size in the PDF, whose file name
    /// (with `hint` in it) is filled in later.
    fn picture(&mut self, spec: &ImageSpec, hint: &str) -> String {
        let width = spec.rect.width().max(1.0);
        let src = self.images.region(spec, hint);
        format!("\\kakemonopicture{{{width:.1}bp}}{{{src}}}")
    }

    /// A rebuilt table as a `tabular`, with `booktabs` rules above, below
    /// and under the header, and its spanning cells.
    fn tabular(&self, table: &Table) -> String {
        let cols = Columns::plain(table);
        let spec: String = (0..cols.align.len()).map(|col| cols.spec(col)).collect();
        let mut lines = vec![format!("\\begin{{tabular}}{{{spec}}}"), "\\toprule".to_string()];
        lines.extend(self.rows(table, &cols, false));
        lines.push("\\bottomrule".to_string());
        lines.push("\\end{tabular}".to_string());
        lines.join("\n")
    }

    /// A rebuilt table as a `longtable`, which breaks across pages with
    /// its header rows repeated at the top of each, in a smaller font when
    /// it has many columns ([`LONG_TABLE_SIZES`]); its long columns wrap
    /// when it would be wider than the text. No column is ever cut off:
    /// a table still too wide in the smallest font is set on landscape
    /// pages, and one too wide even there is cut into groups of columns,
    /// each a table of its own with the first column repeated. Its
    /// `caption` (before it when `caption_first`) is kept on its page.
    fn longtable(&self, table: &Table, caption: Option<&str>, caption_first: bool) -> String {
        let n_cols = table.n_cols.max(1);
        let start = LONG_TABLE_SIZES
            .iter()
            .position(|(most, ..)| n_cols <= *most)
            .expect("the last sizes take any count");
        let portrait = LONG_TABLE_SIZES[start..].iter().find_map(|&size| {
            let cols = Columns::fitted(table, size.2, size.3, TEXT_WIDTH_PT);
            cols.fits().then_some((size, cols))
        });
        let (above, below) = match caption {
            Some(caption) if caption_first => (Some(caption), None),
            Some(caption) => (None, Some(caption)),
            None => (None, None),
        };
        let (tables, landscape) = match portrait {
            Some((size, cols)) => (
                self.longtable_part(table, &cols, size, TEXT_HEIGHT_PT, (above, below)),
                false,
            ),
            None => {
                // Turned, in the smallest font: the width of the text is
                // the height of the page.
                let size = *LONG_TABLE_SIZES.last().expect("sizes");
                let cols = Columns::fitted(table, size.2, size.3, TEXT_HEIGHT_PT);
                let tables = if cols.fits() {
                    self.longtable_part(table, &cols, size, TEXT_WIDTH_PT, (above, below))
                } else {
                    let groups = column_groups(&cols);
                    groups
                        .iter()
                        .enumerate()
                        .map(|(i, group)| {
                            let part = table_columns(table, group);
                            let cols = Columns::fitted(&part, size.2, size.3, TEXT_HEIGHT_PT);
                            // The caption above the first group, or below
                            // the last.
                            let captions = (above.filter(|_| i == 0), below.filter(|_| i + 1 == groups.len()));
                            self.longtable_part(&part, &cols, size, TEXT_WIDTH_PT, captions)
                        })
                        .collect::<Vec<_>>()
                        .join("\n\n")
                };
                (tables, true)
            }
        };
        if landscape {
            format!("\\begin{{landscape}}\n{tables}\n\\end{{landscape}}")
        } else {
            tables
        }
    }

    /// One `longtable` of `table` in the columns `cols`, at `size` (a row of
    /// [`LONG_TABLE_SIZES`]), on pages whose text is `page_height_pt` high,
    /// with the captions `(above, below)`. A caption is a row of the table,
    /// over all its columns: above, in the head of its first page; below,
    /// in the foot of its last. Outside the table, it could be left alone
    /// at the bottom of a page (longtable breaks the page before the head
    /// when the head and the first row do not fit).
    fn longtable_part(
        &self,
        table: &Table,
        cols: &Columns,
        size: (usize, &str, f32, f32),
        page_height_pt: f32,
        (above, below): (Option<&str>, Option<&str>),
    ) -> String {
        let (_, size, size_pt, tabcolsep_pt) = size;
        let n_cols = table.n_cols.max(1);
        let spec: String = (0..n_cols).map(|col| cols.spec(col)).collect();
        let mut lines = Vec::new();
        if !size.is_empty() {
            lines.push(format!(
                "\\begingroup{size}\\setlength{{\\tabcolsep}}{{{tabcolsep_pt}pt}}"
            ));
        }
        let captioned = above.is_some() || below.is_some();
        if captioned {
            // A captioned table starts at the left margin, as its caption.
            lines.push("\\begingroup\\setlength{\\LTleft}{0pt}\\setlength{\\LTright}{\\fill}".to_string());
        }
        lines.push(format!("\\begin{{longtable}}{{{spec}}}"));
        // A caption is a paragraph as wide as the text, in a box of no
        // width: it does not widen the table.
        let caption_row = |caption: &str| {
            format!(
                "\\multicolumn{{{n_cols}}}{{@{{}}l@{{}}}}{{\\makebox[0pt][l]{{\\parbox[t]{{\\linewidth}}{{{caption}}}}}}} \\\\*"
            )
        };
        if let Some(caption) = above {
            lines.push(caption_row(caption));
        }
        lines.push("\\toprule".to_string());
        let rows = self.rows(table, cols, true);
        let header = table.header_rows.min(table.rows.len());
        let (head, body) = if header > 0 && header < table.rows.len() {
            // The header rows and the rule under them: `rows` puts it
            // after the last header row.
            (&rows[..=header], &rows[header + 1..])
        } else {
            (&rows[..0], &rows[..])
        };
        // LaTeX sets a head apart from the body on its first run: a row of
        // empty cells, not printed (`\kill`), gives each wrapped column its
        // width there, so that a `\multicolumn` of the head does not widen
        // the last column it spans.
        let mut head = head.to_vec();
        let head_spans = table.rows[..header].iter().flatten().any(|cell| cell.colspan > 1);
        if head_spans && !head.is_empty() && cols.wrap.iter().any(|&wrap| wrap) {
            head.insert(0, format!("{}\\kill", " & ".repeat(n_cols - 1)));
        }
        lines.extend(head.iter().cloned());
        // A header too tall to repeat is only on the first page.
        let repeat = !head.is_empty()
            && cols.header_lines(table) as f32 * size_pt * LINE_HEIGHT_RATIO
                <= LONG_TABLE_MAX_HEAD_RATIO * page_height_pt;
        if above.is_some() || (!head.is_empty() && !repeat) {
            lines.push("\\endfirsthead".to_string());
            lines.push("\\toprule".to_string());
            if repeat {
                lines.extend(head.iter().cloned());
            }
        }
        if above.is_some() || !head.is_empty() {
            lines.push("\\endhead".to_string());
        }
        lines.push("\\bottomrule".to_string());
        if let Some(caption) = below {
            lines.push(caption_row(caption));
        }
        lines.push("\\endlastfoot".to_string());
        lines.extend(body.iter().cloned());
        lines.push("\\end{longtable}".to_string());
        if captioned {
            lines.push("\\endgroup".to_string());
        }
        if !size.is_empty() {
            lines.push("\\endgroup".to_string());
        }
        lines.join("\n")
    }

    /// The rows of `table` set in the columns `cols`, one line each, with a
    /// `\midrule` line after the header rows. With `split_header`, a cell
    /// of the header spans no row of the body (the header is repeated on
    /// each page of a long table, apart from its body).
    fn rows(&self, table: &Table, cols: &Columns, split_header: bool) -> Vec<String> {
        let n_cols = table.n_cols.max(1);
        let n_rows = table.rows.len();
        // For each position covered by a cell spanning from an earlier
        // row: the number of columns that cell spans and its column
        // specification, at its first column.
        let mut covered: Vec<Vec<Option<(usize, String)>>> = vec![vec![None; n_cols]; n_rows];
        let mut lines = Vec::new();
        for (row_idx, row) in table.rows.iter().enumerate() {
            let last_row = if split_header && row_idx < table.header_rows {
                table.header_rows.min(n_rows)
            } else {
                n_rows
            };
            let mut cells: Vec<String> = Vec::new();
            let mut col = 0;
            let mut row_cells = row.iter().filter(|cell| cell.col < n_cols).peekable();
            while col < n_cols {
                while row_cells.next_if(|cell| cell.col < col).is_some() {}
                if let Some((width, spec)) = covered[row_idx][col].take() {
                    cells.push(multicolumn(width, &spec, String::new()));
                    col += width;
                } else if let Some(cell) = row_cells.next_if(|cell| cell.col == col) {
                    let colspan = cell.colspan.clamp(1, n_cols - col);
                    let rowspan = cell.rowspan.clamp(1, last_row - row_idx);
                    let spec = cols.cell_spec(col, colspan, &cell.spans);
                    for below in covered.iter_mut().take(row_idx + rowspan).skip(row_idx + 1) {
                        below[col] = Some((colspan, spec.clone()));
                    }
                    cells.push(self.cell(cell, colspan, rowspan, &spec));
                    col += colspan;
                } else {
                    cells.push(String::new());
                    col += 1;
                }
            }
            lines.push(format!("{} \\\\", cells.join(" & ")));
            if row_idx + 1 == table.header_rows && row_idx + 1 < n_rows {
                lines.push("\\midrule".to_string());
            }
        }
        lines
    }

    /// One cell of a table, in `\multirow` and `\multicolumn` (of the column
    /// specification `spec`, from [`Columns::cell_spec`]) when it spans
    /// several rows or columns.
    fn cell(&self, cell: &TableCell, colspan: usize, rowspan: usize, spec: &str) -> String {
        let text = self.text.spans(&cell.spans);
        let text = if rowspan > 1 {
            // In a `p` column, `=` sets the text at the column's width.
            let width = if spec.contains("p{") { "=" } else { "*" };
            format!("\\multirow{{{rowspan}}}{{{width}}}{{{text}}}")
        } else {
            text
        };
        multicolumn(colspan, spec, protect_start(&text))
    }
}

/// The columns of a table as LaTeX sets them: each column's alignment, and
/// whether its text wraps in a `p` column, with its share of the width of
/// the text (its two `\tabcolsep` included).
struct Columns {
    /// The alignment letter (`l`, `c`, `r`) of each column.
    align: Vec<char>,
    /// The text of the column wraps.
    wrap: Vec<bool>,
    /// The share of the width of the text each column takes (guessed for
    /// one that does not wrap).
    share: Vec<f32>,
    /// The width of an em of the table's font, and the space on each side
    /// of a column, as shares of the width of the text.
    em_share: f32,
    /// See [`Columns::em_share`].
    pad_share: f32,
    /// The least share of the width each column can take: its longest word
    /// when it wraps, its text otherwise; at least 1 em either way.
    least: Vec<f32>,
}

impl Columns {
    /// Columns as wide as their text: none wraps.
    fn plain(table: &Table) -> Self {
        let n_cols = table.n_cols.max(1);
        Self {
            align: (0..n_cols).map(|col| align_letter(table, col)).collect(),
            wrap: vec![false; n_cols],
            share: vec![0.0; n_cols],
            em_share: 0.0,
            pad_share: 0.0,
            least: vec![0.0; n_cols],
        }
    }

    /// Whether the columns fit the width of the text they were fitted to.
    fn fits(&self) -> bool {
        self.share.iter().sum::<f32>() <= LONG_TABLE_FILL_RATIO + FIT_TOLERANCE_RATIO
    }

    /// The lines the header rows of `table` take, guessed: in each row,
    /// those of its tallest cell, a cell of wrapped columns taking as many
    /// as its text is wider than they are.
    fn header_lines(&self, table: &Table) -> usize {
        let n_cols = self.align.len();
        table
            .rows
            .iter()
            .take(table.header_rows)
            .map(|row| {
                row.iter()
                    .filter(|cell| cell.col < n_cols)
                    .map(|cell| {
                        let width = cell.colspan.clamp(1, n_cols - cell.col);
                        if !self.wraps(cell.col, width) {
                            return 1;
                        }
                        let text: String = cell.spans.iter().map(|span| span.text.as_str()).collect();
                        let share: f32 = self.share[cell.col..cell.col + width].iter().sum();
                        let room_em = ((share - 2.0 * self.pad_share) / self.em_share).max(1.0);
                        (text_em(&text) / room_em).ceil().max(1.0) as usize
                    })
                    .max()
                    .unwrap_or(1)
            })
            .sum()
    }

    /// The columns of a long table set at `size_pt`: as wide as their text
    /// when the table then fits the width of the text ([`TEXT_WIDTH_PT`]).
    /// Otherwise the columns wider than an even share of the line wrap, and
    /// share what the others leave in proportion to the mean width of
    /// their body cells, so that a typical row takes as many lines in each
    /// of them; each is at least as wide as its typical cell
    /// ([`TYPICAL_CELL_MAX_EM`]) and never under [`MIN_COLUMN_EM`], even
    /// when the table then does not fit. Once any column wraps, every
    /// column is set in its width ([`Columns::paragraph`]), so that the
    /// table is as wide as the shares say. The columns are `tabcolsep_pt`
    /// apart on each side, the text `width_pt` wide.
    fn fitted(table: &Table, size_pt: f32, tabcolsep_pt: f32, width_pt: f32) -> Self {
        let mut cols = Self::plain(table);
        let n_cols = cols.align.len();
        let TextWidths {
            natural,
            body,
            word,
            breaks,
            spans,
        } = TextWidths::of(table, n_cols);
        let em_pt = em_width_pt(size_pt);
        cols.em_share = em_pt / width_pt;
        cols.pad_share = tabcolsep_pt / width_pt;
        let share_of = |em: f32| (em * em_pt + 2.0 * tabcolsep_pt) / width_pt;
        cols.share = natural.iter().map(|&em| share_of(em)).collect();
        let mean = |col: usize| {
            let (sum, count) = body[col];
            if count == 0 { 0.0 } else { sum / count as f32 }
        };
        // The width of a wrapped column's typical cell, in em.
        let typical = |col: usize| mean(col).max(word[col]).clamp(MIN_COLUMN_EM, TYPICAL_CELL_MAX_EM);
        cols.least = (0..n_cols)
            .map(|col| {
                if breaks[col] {
                    share_of(typical(col))
                } else {
                    // An empty column, or one of narrow characters, still
                    // keeps an em inside its `\tabcolsep`: a `p` column of
                    // no width (or less) runs its text into the next.
                    share_of(natural[col].max(MIN_COLUMN_EM))
                }
            })
            .collect();
        if cols.share.iter().sum::<f32>() <= LONG_TABLE_FILL_RATIO {
            return cols;
        }
        // The columns under a cell over several of them are together as
        // wide as its typical text.
        let floors = span_floors(&spans, &cols.least, |em| share_of(em.min(TYPICAL_CELL_MAX_EM)));
        for (col, floor) in floors.into_iter().enumerate() {
            cols.least[col] = cols.least[col].max(floor);
        }
        let even = 1.0 / n_cols as f32;
        cols.wrap = (0..n_cols).map(|col| cols.share[col] > even && breaks[col]).collect();
        for col in (0..n_cols).filter(|&col| !cols.wrap[col]) {
            cols.share[col] = cols.share[col].max(cols.least[col]);
        }
        let wrapped: Vec<usize> = (0..n_cols).filter(|&col| cols.wrap[col]).collect();
        let kept: f32 = (0..n_cols)
            .filter(|&col| !cols.wrap[col])
            .map(|col| cols.share[col])
            .sum();
        let left = LONG_TABLE_FILL_RATIO - kept;
        let weight = |col: usize| mean(col).max(MIN_WRAPPED_COLUMN_EM);
        if left <= 0.0 {
            // The others leave nothing: the table is wider than the text.
            for &col in &wrapped {
                cols.share[col] = cols.least[col];
            }
            return cols;
        }
        // Each wrapped column gets at least its floor, the width of its
        // typical cell (when the floors do not fit, the table does not
        // either); the others share the rest in proportion to their weights.
        let floor = |col: usize| cols.least[col];
        let mut floored = vec![false; n_cols];
        loop {
            let free: Vec<usize> = wrapped.iter().copied().filter(|&col| !floored[col]).collect();
            let rest = left
                - wrapped
                    .iter()
                    .filter(|&&col| floored[col])
                    .map(|&col| floor(col))
                    .sum::<f32>();
            let total: f32 = free.iter().map(|&col| weight(col)).sum();
            let mut changed = false;
            for &col in &free {
                cols.share[col] = rest * weight(col) / total;
                if cols.share[col] < floor(col) {
                    floored[col] = true;
                    changed = true;
                }
            }
            for &col in wrapped.iter().filter(|&&col| floored[col]) {
                cols.share[col] = floor(col);
            }
            if !changed || free.is_empty() {
                break;
            }
        }
        cols
    }

    /// Whether any of the `width` columns from `col` wraps.
    fn wraps(&self, col: usize, width: usize) -> bool {
        self.wrap[col..col + width].iter().any(|&wrap| wrap)
    }

    /// The column specification of column `col`.
    fn spec(&self, col: usize) -> String {
        self.span_spec(col, 1)
    }

    /// Whether the `width` columns from `col` are set as one `p` column:
    /// when one of them wraps, or when they are a `\multicolumn` of a long
    /// table, whose text could be wider than they are (its widths are not
    /// counted in theirs).
    fn paragraph(&self, col: usize, width: usize) -> bool {
        let fitted = self.share.iter().any(|&share| share > 0.0);
        let wrapping = self.wrap.iter().any(|&wrap| wrap);
        self.wraps(col, width) || (fitted && (width > 1 || wrapping))
    }

    /// The column specification of a cell of the text `spans` over the
    /// `width` columns from `col`: [`Columns::span_spec`], but the
    /// alignment of the first column for a cell over several columns of a
    /// table none of whose columns wraps, when its text fits on one line in
    /// them. A `p` column there is as wide as its columns are guessed to be,
    /// wider than they are when the guess runs long: LaTeX sets a long
    /// table's head apart from its body on its first run, and a `p` cell of
    /// the head over columns as narrow as their header then widens the last
    /// of them, and the table, by the difference.
    fn cell_spec(&self, col: usize, width: usize, spans: &[Span]) -> String {
        let wrapping = self.wrap.iter().any(|&wrap| wrap);
        if width > 1 && !wrapping && self.paragraph(col, width) {
            let text: String = spans.iter().map(|span| span.text.as_str()).collect();
            let share: f32 = self.share[col..col + width].iter().sum();
            if text_em(&text) * BOLD_WIDTH_RATIO * self.em_share + 2.0 * self.pad_share <= share {
                return self.align[col].to_string();
            }
        }
        self.span_spec(col, width)
    }

    /// The column specification of the `width` columns from `col` taken
    /// together (a `\multicolumn`): the alignment of the first, or a `p`
    /// column as wide as all of them ([`Columns::paragraph`]).
    fn span_spec(&self, col: usize, width: usize) -> String {
        let align = self.align[col];
        if !self.paragraph(col, width) {
            return align.to_string();
        }
        let share: f32 = self.share[col..col + width].iter().sum();
        let ragged = match align {
            'c' => "\\centering",
            'r' => "\\raggedleft",
            _ => "\\raggedright",
        };
        format!(">{{{ragged}\\arraybackslash}}p{{\\dimexpr{share:.4}\\linewidth-2\\tabcolsep\\relax}}")
    }
}

/// The widths of the text of each column of a table, guessed in em.
struct TextWidths {
    /// The width of a column's longest cell on one line (a header cell is
    /// bold).
    natural: Vec<f32>,
    /// The total width and the count of a column's body cells with text.
    body: Vec<(f32, usize)>,
    /// The longest word of a column's cells.
    word: Vec<f32>,
    /// One of a column's cells has more than one word (a column of numbers
    /// or codes under one-word headers has none: it could not wrap).
    breaks: Vec<bool>,
    /// The cells over several columns: (first column, columns, width of
    /// the text on one line).
    spans: Vec<(usize, usize, f32)>,
}

impl TextWidths {
    /// The widths of the `n_cols` columns of `table`; a cell over several
    /// columns counts in none.
    fn of(table: &Table, n_cols: usize) -> Self {
        let mut widths = Self {
            natural: vec![0.0; n_cols],
            body: vec![(0.0, 0); n_cols],
            word: vec![0.0; n_cols],
            breaks: vec![false; n_cols],
            spans: Vec::new(),
        };
        for (row, cells) in table.rows.iter().enumerate() {
            for cell in cells.iter().filter(|cell| cell.colspan > 1 && cell.col < n_cols) {
                let text: String = cell.spans.iter().map(|span| span.text.as_str()).collect();
                let bold = row < table.header_rows || cell.spans.iter().any(|span| span.style & BOLD != 0);
                let ratio = if bold { BOLD_WIDTH_RATIO } else { 1.0 };
                let width = cell.colspan.min(n_cols - cell.col);
                widths.spans.push((cell.col, width, text_em(&text) * ratio));
            }
            for cell in cells.iter().filter(|cell| cell.colspan <= 1 && cell.col < n_cols) {
                let text: String = cell.spans.iter().map(|span| span.text.as_str()).collect();
                let col = cell.col;
                // A header cell is bold, and so may be a body cell.
                let bold = row < table.header_rows || cell.spans.iter().any(|span| span.style & BOLD != 0);
                let ratio = if bold { BOLD_WIDTH_RATIO } else { 1.0 };
                let (em, word) = (text_em(&text) * ratio, word_em(&text) * ratio);
                widths.natural[col] = widths.natural[col].max(em);
                widths.word[col] = widths.word[col].max(word);
                widths.breaks[col] |= word < em;
                if row >= table.header_rows && em > 0.0 {
                    widths.body[col].0 += em;
                    widths.body[col].1 += 1;
                }
            }
        }
        widths
    }
}

/// The width in pt of the em that the widths of text are guessed in, at
/// the font size `size_pt` ([`SIZE_WIDTH_RATIOS`]).
fn em_width_pt(size_pt: f32) -> f32 {
    size_pt
        * SIZE_WIDTH_RATIOS
            .iter()
            .find(|(least, _)| size_pt >= *least)
            .map_or(1.0, |&(_, ratio)| ratio)
}

/// The least share of each column that the cells over several columns
/// `spans` (first column, columns, width in em) ask for: when the columns
/// under a cell are together narrower (by `least`) than `share_of` its
/// width, each gets an even part of what is missing.
fn span_floors(spans: &[(usize, usize, f32)], least: &[f32], share_of: impl Fn(f32) -> f32) -> Vec<f32> {
    let mut floors = vec![0.0f32; least.len()];
    for &(col, width, em) in spans {
        let have: f32 = least[col..col + width].iter().sum();
        let missing = share_of(em) - have;
        if missing > 0.0 {
            for column in col..col + width {
                floors[column] = floors[column].max(least[column] + missing / width as f32);
            }
        }
    }
    floors
}

/// The columns of a table too wide for the text, in groups that each fit
/// with its first column (repeated in each), by the least width of each
/// column ([`Columns::least`]) and [`COLUMN_GROUP_FILL_RATIO`]; a column
/// too wide even alone with the first is a group of its own.
fn column_groups(cols: &Columns) -> Vec<Vec<usize>> {
    let mut groups: Vec<Vec<usize>> = Vec::new();
    let mut width = 0.0;
    for col in 1..cols.least.len() {
        match groups.last_mut() {
            Some(group) if width + cols.least[col] <= COLUMN_GROUP_FILL_RATIO => group.push(col),
            _ => {
                groups.push(vec![0, col]);
                width = cols.least[0];
            }
        }
        width += cols.least[col];
    }
    if groups.is_empty() {
        groups.push(vec![0]);
    }
    groups
}

/// The table of the columns `keep` (in order) of `table`: a cell is at its
/// first kept column and spans the kept columns it covered; a cell that
/// covers none is left out.
fn table_columns(table: &Table, keep: &[usize]) -> Table {
    let rows = table
        .rows
        .iter()
        .map(|row| {
            row.iter()
                .filter_map(|cell| {
                    let covered: Vec<usize> = (0..keep.len())
                        .filter(|&new| (cell.col..cell.col + cell.colspan.max(1)).contains(&keep[new]))
                        .collect();
                    let first = *covered.first()?;
                    Some(TableCell {
                        col: first,
                        colspan: covered.len(),
                        rowspan: cell.rowspan,
                        spans: cell.spans.clone(),
                    })
                })
                .collect()
        })
        .collect();
    Table {
        n_cols: keep.len(),
        header_rows: table.header_rows,
        col_align: keep
            .iter()
            .map(|&col| table.col_align.get(col).copied().unwrap_or("left"))
            .collect(),
        rows,
        rule_above: Vec::new(),
        rule_bottom: Vec::new(),
        rule_left: Vec::new(),
        rule_right: Vec::new(),
        geom: table.geom.clone(),
    }
}

/// The width of the longest part of `text` that cannot be broken across
/// lines, guessed in em: a word, a CJK character (a line may break after
/// any).
fn word_em(text: &str) -> f32 {
    text.split(|ch: char| ch.is_whitespace() || is_cjk(ch))
        .map(text_em)
        .fold(if text.chars().any(is_cjk) { CJK_CHAR_EM } else { 0.0 }, f32::max)
}

/// The width of `text` set on one line, guessed in em.
fn text_em(text: &str) -> f32 {
    text.chars()
        .map(|ch| match ch {
            _ if is_cjk(ch) => CJK_CHAR_EM,
            'A'..='Z' | 'm' | 'w' => WIDE_CHAR_EM,
            'f' | 'i' | 'j' | 'l' | 'r' | 't' | ' ' | '.' | ',' | ':' | ';' | '\'' | '(' | ')' => NARROW_CHAR_EM,
            _ => OTHER_CHAR_EM,
        })
        .sum()
}

/// The author groups, centred: each card a `tabular` (the name in bold,
/// then the affiliation lines), the cards side by side.
impl AuthorMarkup for Writer<'_, '_> {
    fn anchor(&mut self, block: &Block) {
        Writer::anchor(self, block);
    }

    fn lines(&self, block: &Block) -> Vec<String> {
        author_lines(
            block,
            |row| self.text.spans(row),
            "\\textbf{",
            |name| format!("\\textbf{{{name}}}"),
            |line| protect_start(&line),
        )
    }

    fn card(&self, lines: &[String]) -> String {
        format!(
            "\\begin{{tabular}}[t]{{@{{}}c@{{}}}}\n{}\n\\end{{tabular}}",
            lines.join("\\\\\n")
        )
    }

    fn cards(&mut self, cards: &[String]) {
        let row = cards.join(&format!("\n\\hspace{{{AUTHOR_GAP}}}\n"));
        self.paragraph(&format!("\\begin{{center}}\n{row}\n\\end{{center}}"));
    }

    fn byline(&mut self, lines: &[String]) {
        self.paragraph(&format!("\\begin{{center}}\n{}\n\\end{{center}}", lines.join("\\\\\n")));
    }
}

/// The share of the characters of `spans` (spaces apart) that are
/// monospaced.
fn mono_ratio(spans: &[Span]) -> f32 {
    let count = |mono_only: bool| {
        spans
            .iter()
            .filter(|span| !mono_only || span.style & MONO != 0)
            .map(|span| span.text.chars().filter(|ch| !ch.is_whitespace()).count())
            .sum::<usize>()
    };
    let total = count(false);
    if total == 0 {
        return 0.0;
    }
    count(true) as f32 / total as f32
}

/// The alignment letter (`l`, `c`, `r`) of column `col` of `table`.
fn align_letter(table: &Table, col: usize) -> char {
    match table.col_align.get(col).copied() {
        Some("center") => 'c',
        Some("right") => 'r',
        _ => 'l',
    }
}

/// `text` in a `\multicolumn` of `width` columns set as `spec`, or alone
/// when `width` is 1.
fn multicolumn(width: usize, spec: &str, text: String) -> String {
    if width > 1 {
        format!("\\multicolumn{{{width}}}{{{spec}}}{{{text}}}")
    } else {
        text
    }
}

/// `text` with an empty group before a `[` or `*` that starts it, which a
/// `\\` or an `\item` before it would read as its option.
fn protect_start(text: &str) -> String {
    if text.starts_with(['[', '*']) {
        format!("{{}}{text}")
    } else {
        text.to_string()
    }
}

/// The texts of the notes whose marks are in a table (`\footnotemark`),
/// numbered as their marks: the counter goes back by their number, then
/// steps once per note. Empty without notes.
fn footnote_texts(notes: &[String]) -> String {
    if notes.is_empty() {
        return String::new();
    }
    let texts: String = notes
        .iter()
        .map(|note| format!("\\stepcounter{{footnote}}\\footnotetext{{{note}}}"))
        .collect();
    format!("\n\\addtocounter{{footnote}}{{-{}}}{texts}", notes.len())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn picture_folder_is_safe_for_tex() {
        assert_eq!(
            tex_path("my images/1706.03762v7_assets/"),
            "my_images/1706.03762v7_assets"
        );
        assert_eq!(tex_path("a%b#c"), "a_b_c");
    }

    #[test]
    fn unknown_characters_are_outside_latin() {
        let chars: Vec<char> = unknown_chars("Erdős", "é ž 中 ſ").into_iter().collect();
        assert_eq!(chars, vec!['ſ', '中']);
    }

    /// A document without pages whose only paragraph is `text`.
    fn doc_of(text: &str) -> Document {
        let mut block = Block::new(0, BlockKind::Paragraph { in_abstract: false });
        block.spans = vec![Span::plain(text)];
        Document {
            meta: crate::model::Meta::default(),
            pages: Vec::new(),
            body_size: 10.0,
            leading: 12.0,
            body_font: String::new(),
            math_font: String::new(),
            blocks: vec![block],
            pictures: Vec::new(),
        }
    }

    #[test]
    fn cjk_text_gets_cjk_fonts_for_lualatex_and_xelatex() {
        for text in ["一貫性のある操作", "ひらがな", "カタカナ", "한국어", "全角,"] {
            let tex = document(&doc_of(text), text);
            assert!(tex.contains("\\usepackage[match]{luatexja-fontspec}"), "{text}: {tex}");
            assert!(tex.contains("\\usepackage{xeCJK}"), "{text}");
            assert!(tex.contains("\\setCJKmainfont"), "{text}");
            assert!(tex.contains("needs LuaLaTeX or XeLaTeX"), "{text}");
        }
    }

    #[test]
    fn xelatex_sets_the_symbols_of_cjk_text_in_the_cjk_font() {
        let tex = document(&doc_of("①②の手順 ≦ ― ●"), "①②の手順 ≦ ― ●");
        let declared = tex
            .lines()
            .skip_while(|line| !line.contains("\\xeCJKDeclareCharClass{CJK}"))
            .take_while(|line| !line.contains('}') || line.contains("DeclareCharClass"))
            .collect::<String>();
        for code in ["\"2015", "\"2264->\"2267", "\"2460->", "\"25CE->\"25D3"] {
            assert!(declared.contains(code), "{code} not declared: {declared}");
        }
        // Kanbun and vertical forms are CJK text.
        assert!(is_cjk('\u{3190}') && is_cjk('\u{319F}') && is_cjk('\u{FE10}') && is_cjk('\u{FE19}'));
    }

    #[test]
    fn symbols_of_cjk_text_are_characters_under_lualatex_and_xelatex() {
        let body = escape("△11 523 □ 〈注〉");
        let tex = document(&doc_of("人口 △11 523"), &body);
        for (symbol, command) in [
            ("△", "\\triangle"),
            ("□", "\\square"),
            ("〈", "\\langle"),
            ("〉", "\\rangle"),
        ] {
            assert!(
                tex.contains(&format!("\\kakemonotext{{{symbol}}}{{{command}}}")),
                "{symbol}: {tex}"
            );
        }
        // Without CJK text, the math symbol stays.
        let latin = document(&doc_of("x"), &escape("△ x"));
        assert!(latin.contains("\\ensuremath{\\triangle}") && !latin.contains("kakemonotext"));
    }

    #[test]
    fn text_without_cjk_keeps_the_plain_preamble() {
        let text = "Erdős café ž";
        let tex = document(&doc_of(text), text);
        // A document without pages is a Word document's.
        let plain = PREAMBLE.replacen("Converted from a PDF", "Converted from a Word document", 1);
        assert!(tex.starts_with(&plain), "{tex}");
        assert!(!tex.contains("luatexja") && !tex.contains("xeCJK"));
    }

    /// A table of `n_cols` columns: a header row, then `body` rows, each
    /// cell's text given by `text(row, col)`.
    fn table_of(n_cols: usize, body: usize, text: impl Fn(usize, usize) -> String) -> Table {
        let row = |row: usize| {
            (0..n_cols)
                .map(|col| TableCell {
                    col,
                    colspan: 1,
                    rowspan: 1,
                    spans: vec![Span::plain(text(row, col))],
                })
                .collect()
        };
        Table {
            n_cols,
            header_rows: 1,
            col_align: vec!["left"; n_cols],
            rows: (0..=body).map(row).collect(),
            rule_above: Vec::new(),
            rule_bottom: Vec::new(),
            rule_left: Vec::new(),
            rule_right: Vec::new(),
            geom: crate::model::TableGeom::default(),
        }
    }

    /// The LaTeX of a table block holding `table`.
    fn table_latex(table: Table) -> String {
        let mut block = Block::new(
            0,
            BlockKind::Figure {
                kind: FloatKind::Table,
                image: None,
                label: None,
            },
        );
        block.table = Some(table);
        figure_latex(&block)
    }

    /// The LaTeX of the figure or table `block`.
    fn figure_latex(block: &Block) -> String {
        let anchors = std::collections::HashSet::new();
        let html = HtmlOptions::default();
        let mut images = ImageStore {
            opts: &html,
            jobs: Vec::new(),
            svg_as_pdf: true,
        };
        let opts = LatexOptions::default();
        let mut writer = Writer {
            opts: &opts,
            text: Text {
                anchors: &anchors,
                math: true,
                table_notes: std::cell::RefCell::default(),
            },
            images: &mut images,
            out: String::new(),
            group: Group::None,
            item_open: false,
            nested: 0,
            after_text: false,
        };
        writer.figure(block, FloatKind::Table, None);
        writer.out
    }

    #[test]
    fn a_short_table_is_one_box() {
        let latex = table_latex(table_of(3, 30, |row, col| format!("r{row}c{col}")));
        assert!(latex.contains("\\begin{kakemonotable}"), "{latex}");
        assert!(!latex.contains("longtable"));
    }

    #[test]
    fn a_long_table_breaks_across_pages_with_its_header_repeated() {
        let latex = table_latex(table_of(3, 31, |row, col| format!("r{row}c{col}")));
        assert!(!latex.contains("kakemonotable"), "{latex}");
        assert!(latex.contains("\\begin{longtable}{lll}"), "{latex}");
        let head = latex
            .find("r0c0 & r0c1 & r0c2 \\\\\n\\midrule\n\\endhead")
            .expect("no repeated header");
        assert!(latex.find("r1c0").unwrap() > head);
        assert!(latex.contains("\\endhead\n\\bottomrule\n\\endlastfoot\n"), "{latex}");
        assert!(
            latex.contains("r31c0 & r31c1 & r31c2 \\\\\n\\end{longtable}"),
            "{latex}"
        );
    }

    #[test]
    fn a_long_table_with_many_columns_is_set_smaller() {
        let latex = table_latex(table_of(8, 40, |row, col| format!("{row}.{col}")));
        assert!(
            latex.contains("\\begingroup\\footnotesize\\setlength{\\tabcolsep}{3pt}\n\\begin{longtable}"),
            "{latex}"
        );
    }

    #[test]
    fn a_long_table_too_wide_for_the_page_wraps_its_long_columns() {
        let latex = table_latex(table_of(4, 40, |row, col| {
            if col == 2 {
                format!("{row} {}", "a long description ".repeat(10))
            } else {
                format!("{row}")
            }
        }));
        let spec_line = latex
            .lines()
            .find(|line| line.starts_with("\\begin{longtable}"))
            .unwrap();
        // Every column in its width, the long one the widest.
        let shares: Vec<f32> = spec_line
            .split("p{\\dimexpr")
            .skip(1)
            .map(|part| part.split("\\linewidth").next().unwrap().parse().unwrap())
            .collect();
        assert_eq!(shares.len(), 4, "{spec_line}");
        assert!(shares[2] > shares[0] + shares[1] + shares[3], "{spec_line}");
    }

    #[test]
    fn a_long_table_with_a_header_taller_than_a_quarter_page_shows_it_once() {
        let latex = table_latex(table_of(16, 40, |row, col| {
            if row == 0 {
                "a long header of many words ".repeat(30)
            } else {
                format!("{row} {col} text of the cell {}", "word ".repeat(col))
            }
        }));
        assert!(
            latex.contains("\\midrule\n\\endfirsthead\n\\toprule\n\\endhead"),
            "{latex}"
        );
        let short = table_latex(table_of(16, 40, |row, col| {
            format!("{row} {col} text {}", "word ".repeat(col))
        }));
        assert!(!short.contains("\\endfirsthead") && short.contains("\\endhead"));
    }

    /// The value of the cell at `row`, `col` of a wide table of numbers.
    fn number(row: usize, col: usize) -> String {
        format!("{}.{col:02}", (row * 1000 + col * 37) % 99_999)
    }

    #[test]
    fn a_long_table_of_unbreakable_columns_too_wide_is_turned() {
        // 20 columns of numbers: wider than the text even in \tiny.
        let latex = table_latex(table_of(20, 31, number));
        assert!(
            latex.contains("\\begin{landscape}") && latex.contains("\\end{landscape}"),
            "{latex}"
        );
        assert!(latex.contains("\\tiny"), "{latex}");
        assert!(latex.contains(&number(31, 19)));
    }

    #[test]
    fn a_long_table_too_wide_even_turned_is_cut_into_groups_of_columns() {
        let latex = table_latex(table_of(60, 31, number));
        let tables = latex.matches("\\begin{longtable}").count();
        assert!(tables >= 2, "{tables} tables");
        // Every column is in one of them, the first one repeated in each.
        for col in 0..60 {
            assert!(latex.contains(&number(31, col)), "column {col} lost");
        }
        assert_eq!(latex.matches(&format!("{} &", number(31, 0))).count(), tables);
    }

    #[test]
    fn wrapped_columns_are_at_least_one_em_wide() {
        let table = table_of(16, 40, |row, col| format!("{row} {col} {}", "語".repeat(10 + col * 20)));
        for &(_, _, size_pt, tabcolsep_pt) in LONG_TABLE_SIZES {
            let cols = Columns::fitted(&table, size_pt, tabcolsep_pt, TEXT_WIDTH_PT);
            for col in (0..16).filter(|&col| cols.wrap[col]) {
                let content_pt = cols.share[col] * TEXT_WIDTH_PT - 2.0 * tabcolsep_pt;
                assert!(
                    content_pt >= size_pt * 0.999,
                    "column {col}: {content_pt} pt at {size_pt} pt"
                );
            }
        }
    }

    #[test]
    fn every_column_of_a_long_table_keeps_one_em_inside_its_padding() {
        // An empty column and a column of single characters next to wrapped
        // ones: a share of only the two `\tabcolsep` leaves no room, or a
        // negative one, for the text.
        let table = table_of(16, 40, |row, col| match col {
            0 => String::new(),
            1 => "x".to_string(),
            _ => format!("{row} {col} {}", "語".repeat(10 + col * 20)),
        });
        for &(_, _, size_pt, tabcolsep_pt) in LONG_TABLE_SIZES {
            for width_pt in [TEXT_WIDTH_PT, TEXT_HEIGHT_PT] {
                let cols = Columns::fitted(&table, size_pt, tabcolsep_pt, width_pt);
                if !cols.wrap.iter().any(|&wrap| wrap) {
                    continue;
                }
                for (col, share) in cols.share.iter().enumerate().take(16) {
                    let content_pt = share * width_pt - 2.0 * tabcolsep_pt;
                    assert!(
                        content_pt >= size_pt * 0.999,
                        "column {col}: {content_pt} pt at {size_pt} pt in {width_pt} pt"
                    );
                }
            }
        }
    }

    /// The text of each page of the PDF that pdfLaTeX makes of `tex` in a
    /// scratch folder named after `name`; `None` (said on the standard
    /// error) when pdfLaTeX or pdftotext is not installed.
    fn compiled_pages(name: &str, tex: &str) -> Option<Vec<String>> {
        let dir = std::env::temp_dir().join(format!("kakemono-{name}-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("t.tex"), tex).unwrap();
        let Ok(out) = std::process::Command::new("pdflatex")
            .args(["-interaction=nonstopmode", "-halt-on-error", "t.tex"])
            .current_dir(&dir)
            .output()
        else {
            eprintln!("SKIPPED {name}: pdflatex is not installed, the LaTeX was not compiled");
            return None;
        };
        assert!(
            out.status.success() && dir.join("t.pdf").is_file(),
            "pdflatex failed:\n{}",
            String::from_utf8_lossy(&out.stdout)
        );
        let Ok(text) = std::process::Command::new("pdftotext")
            .args(["-layout", "t.pdf", "-"])
            .current_dir(&dir)
            .output()
        else {
            eprintln!("SKIPPED {name}: pdftotext is not installed, the PDF text was not checked");
            return None;
        };
        let _ = std::fs::remove_dir_all(&dir);
        Some(
            String::from_utf8_lossy(&text.stdout)
                .split('\u{c}')
                .map(str::to_string)
                .collect(),
        )
    }

    /// The widths of the alignments pdfLaTeX reports as too wide (`Overfull
    /// \hbox (..pt too wide) in alignment`) on its first run over `tex`, in
    /// a scratch folder named after `name`; `None` (said on the standard
    /// error) when pdfLaTeX is not installed.
    fn first_run_overfull_alignments(name: &str, tex: &str) -> Option<Vec<f32>> {
        let dir = std::env::temp_dir().join(format!("kakemono-{name}-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("t.tex"), tex).unwrap();
        let Ok(out) = std::process::Command::new("pdflatex")
            .args(["-interaction=nonstopmode", "-halt-on-error", "t.tex"])
            .current_dir(&dir)
            .output()
        else {
            eprintln!("SKIPPED {name}: pdflatex is not installed, the LaTeX was not compiled");
            return None;
        };
        assert!(
            out.status.success(),
            "pdflatex failed:\n{}",
            String::from_utf8_lossy(&out.stdout)
        );
        let log = std::fs::read(dir.join("t.log")).unwrap();
        let _ = std::fs::remove_dir_all(&dir);
        let overfull = String::from_utf8_lossy(&log)
            .lines()
            .filter(|line| line.starts_with("Overfull \\hbox (") && line.contains("in alignment"))
            .filter_map(|line| line["Overfull \\hbox (".len()..].split("pt").next()?.parse().ok())
            .collect();
        Some(overfull)
    }

    /// A long table's head is set apart from its body on LaTeX's first run:
    /// a `\multicolumn` in it over columns of no width there widens the last
    /// of them, and the table runs off the page until LaTeX is run again.
    /// One of wrapped columns (a form, labels over several of them) and one
    /// of numbers under a header over twelve of them.
    #[test]
    fn a_long_table_with_spans_in_its_head_fits_on_the_first_run() {
        let span = |col: usize, colspan: usize, rowspan: usize, text: &str| TableCell {
            col,
            colspan,
            rowspan,
            spans: vec![Span::plain(text)],
        };
        let mut form = table_of(20, 40, |row, col| match col {
            1 => format!("{row} {}", "a note on what to do ".repeat(4)),
            5..=12 if row % 3 == 0 => "y".to_string(),
            14 => "Labour office".to_string(),
            16 => format!("{row} the witness, with address and phone number"),
            19 => "TEL".to_string(),
            _ => String::new(),
        });
        form.rows[0] = vec![
            span(2, 4, 1, "Work name"),
            span(6, 1, 1, ":"),
            span(7, 13, 1, "Outer walls"),
        ];
        let months = [
            "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
        ];
        let mut prices = table_of(14, 40, |row, col| match (row, col) {
            (_, 0) => format!("{} ...", 1990 + row),
            (..=20, _) => "-".to_string(),
            _ => format!("1{}.{}0", 80 + (row * 7 + col) % 40, col % 10),
        });
        prices.header_rows = 2;
        prices.rows[0] = vec![
            span(0, 1, 2, "reporting year"),
            span(1, 12, 1, "reporting month"),
            span(13, 1, 2, "yearly average"),
        ];
        prices.rows[1] = months
            .iter()
            .enumerate()
            .map(|(i, month)| span(i + 1, 1, 1, month))
            .collect();
        prices.rows[2] = vec![span(1, 13, 1, "Wood products for energy production 1 2015 = 100")];
        let body = format!("{}\n\n{}", table_latex(form), table_latex(prices));
        let tex = document(&doc_of("x"), &body);
        let Some(overfull) = first_run_overfull_alignments("long-table-heads", &tex) else {
            return;
        };
        assert!(overfull.iter().all(|&pt| pt <= 5.0), "too wide by {overfull:?} pt");
    }

    /// The caption of a long table is on the page of its first row, wherever
    /// the table starts on the page.
    #[test]
    fn the_caption_of_a_long_table_stays_with_its_first_row() {
        let mut block = Block::new(
            0,
            BlockKind::Figure {
                kind: FloatKind::Table,
                image: None,
                label: None,
            },
        );
        block.spans = vec![Span::plain("Table 1: CAPTIONMARK")];
        block.caption_above = true;
        block.table = Some(table_of(2, 31, |row, col| {
            if row == 1 && col == 0 {
                "FIRSTROW".to_string()
            } else {
                format!("r{row}c{col}")
            }
        }));
        let table = figure_latex(&block);
        // The table starts `room` pt above the bottom of the first page.
        for room in (10..=90).step_by(8) {
            let body = format!("\\noindent\\rule{{1pt}}{{\\dimexpr\\textheight-{room}pt\\relax}}\n\n{table}");
            let tex = document(&doc_of("x"), &body);
            let Some(pages) = compiled_pages(&format!("caption-{room}"), &tex) else {
                return;
            };
            let page_of = |mark: &str| pages.iter().position(|page| page.contains(mark));
            assert_eq!(
                page_of("CAPTIONMARK"),
                page_of("FIRSTROW"),
                "{room} pt left: the caption is not on the page of the first row"
            );
        }
    }

    /// Long tables compile with pdfLaTeX (when it is installed) and show
    /// their last row and last column: one of wrapped columns, with a
    /// `\multicolumn` over two of them and a `\multirow{..}{=}` in one, and
    /// one of 20 columns of numbers, which needs landscape pages.
    #[test]
    fn long_tables_compile_and_keep_every_column() {
        let mut wrapped = table_of(3, 40, |row, col| match col {
            0 => format!("{row}"),
            1 => format!("row {row} {}", "has a long description ".repeat(6)),
            _ => format!("end{row} {}", "and some more words ".repeat(5)),
        });
        wrapped.header_rows = 2;
        wrapped.rows[0] = vec![
            TableCell {
                col: 0,
                colspan: 1,
                rowspan: 2,
                spans: vec![Span::plain("Key")],
            },
            TableCell {
                col: 1,
                colspan: 2,
                rowspan: 1,
                spans: vec![Span::plain("Both texts")],
            },
        ];
        wrapped.rows[1].remove(0);
        // A cell over three rows of the body, in a wrapped column.
        wrapped.rows[5][1].rowspan = 3;
        wrapped.rows[6].remove(1);
        wrapped.rows[7].remove(1);
        let first = table_latex(wrapped);
        assert!(
            first.contains("\\multicolumn{2}{>{\\raggedright\\arraybackslash}p{"),
            "{first}"
        );
        assert!(first.contains("\\multirow{3}{=}"), "{first}");
        let second = table_latex(table_of(20, 31, number));
        let tex = document(&doc_of("x"), &format!("{first}\n\n{second}"));
        let Some(pages) = compiled_pages("long-tables", &tex) else {
            return;
        };
        let text = pages.concat();
        for value in ["end40", "Both texts", &number(31, 19), &number(31, 0)] {
            assert!(text.contains(value), "{value} is not in the PDF");
        }
    }

    /// A form laid out over 20 columns, as Japanese workbooks are: long
    /// notes in column 1, short labels in columns 14 to 16, and columns
    /// of one character or none between them.
    fn form() -> Table {
        table_of(20, 40, |row, col| match col {
            1 => format!("{row} {}", "注意事項の説明文".repeat(4)),
            2 => "会社名".to_string(),
            5..=12 => {
                if row % 3 == 0 {
                    "年".to_string()
                } else {
                    String::new()
                }
            }
            14 => "〇〇労働基準監督署".to_string(),
            15 => "〇〇水道局".to_string(),
            16 => format!("{row} 現認者(参考人)の確認(住所、氏名、電話番号)"),
            19 => "TEL".to_string(),
            _ => String::new(),
        })
    }

    #[test]
    fn a_long_table_with_wrapped_columns_sets_every_column_in_its_width() {
        let latex = table_latex(form());
        let spec = latex
            .lines()
            .find(|line| line.starts_with("\\begin{longtable}"))
            .unwrap();
        let columns = spec.matches("p{\\dimexpr").count();
        assert_eq!(columns, 20, "{spec}");
    }

    #[test]
    fn short_labels_of_a_wrapped_column_stay_on_one_line() {
        let mut table = form();
        // A label over columns 17 and 18, empty elsewhere.
        table.rows[3].retain(|cell| cell.col != 17 && cell.col != 18);
        table.rows[3].push(TableCell {
            col: 17,
            colspan: 2,
            rowspan: 1,
            spans: vec![Span::plain("〇〇監理役")],
        });
        for &(_, _, size_pt, tabcolsep_pt) in LONG_TABLE_SIZES {
            for width_pt in [TEXT_WIDTH_PT, TEXT_HEIGHT_PT] {
                let cols = Columns::fitted(&table, size_pt, tabcolsep_pt, width_pt);
                if cols.fits() {
                    let room_em = (cols.share[15] * width_pt - 2.0 * tabcolsep_pt) / size_pt;
                    assert!(room_em >= 5.0 - 1e-3, "〇〇水道局 in {room_em} em at {size_pt} pt");
                    let room_em = ((cols.share[17] + cols.share[18]) * width_pt - 2.0 * tabcolsep_pt) / size_pt;
                    assert!(room_em >= 5.0 - 1e-3, "〇〇監理役 in {room_em} em at {size_pt} pt");
                }
            }
        }
    }

    #[test]
    fn long_tables_load_their_packages() {
        let text = "\\begin{longtable}{ll}\na & b \\\\\n\\end{longtable}";
        let tex = document(&doc_of("x"), text);
        assert!(tex.contains("\\usepackage{longtable}") && tex.contains("\\usepackage{array}"));
        assert!(!document(&doc_of("x"), "x").contains("longtable"));
    }

    #[test]
    fn options_of_items_are_protected() {
        assert_eq!(protect_start("[1] Smith"), "{}[1] Smith");
        assert_eq!(protect_start("Smith [1]"), "Smith [1]");
    }

    #[test]
    fn table_with_spanning_cells() {
        let cell = |col: usize, text: &str, colspan: usize, rowspan: usize| TableCell {
            col,
            colspan,
            rowspan,
            spans: vec![Span::plain(text)],
        };
        let table = Table {
            n_cols: 3,
            header_rows: 2,
            col_align: vec!["left", "center", "right"],
            rows: vec![
                vec![cell(0, "Model", 1, 2), cell(1, "Score", 2, 1)],
                vec![cell(1, "A", 1, 1), cell(2, "B", 1, 1)],
                vec![cell(0, "x & y", 1, 1), cell(2, "[3]", 1, 1)],
            ],
            rule_above: Vec::new(),
            rule_bottom: Vec::new(),
            rule_left: Vec::new(),
            rule_right: Vec::new(),
            geom: crate::model::TableGeom::default(),
        };
        let anchors = std::collections::HashSet::new();
        let mut images = ImageStore {
            opts: &HtmlOptions::default(),
            jobs: Vec::new(),
            svg_as_pdf: true,
        };
        let opts = LatexOptions::default();
        let writer = Writer {
            opts: &opts,
            text: Text {
                anchors: &anchors,
                math: true,
                table_notes: RefCell::default(),
            },
            images: &mut images,
            out: String::new(),
            group: Group::None,
            item_open: false,
            nested: 0,
            after_text: false,
        };
        assert_eq!(
            writer.tabular(&table),
            "\\begin{tabular}{lcr}\n\\toprule\n\\multirow{2}{*}{Model} & \\multicolumn{2}{c}{Score} \\\\\n \
             & A & B \\\\\n\\midrule\nx \\& y &  & {}[3] \\\\\n\\bottomrule\n\\end{tabular}"
        );
    }
}