( s: string, topLevel = true, style: 'operator' | 'italic' | 'upright' | 'auto' | 'none' = 'auto' )
| 555 | // If the source end in a LaTeX command, |
| 556 | // and the appended string begins with a letter |
| 557 | if (/\\[a-zA-Z]+\*?$/.test(src) && /[a-zA-Z]/.test(s[0])) { |
| 558 | // Add a space between them |
| 559 | return src + ' ' + s; |
| 560 | } |
| 561 | // No space needed |
| 562 | return src + s; |
| 563 | } |
| 564 | |
| 565 | /** Lazy map from symbol name (e.g. 'alpha') to LaTeX command (e.g. |
| 566 | * '\\alpha'). Built once from the SYMBOLS table on first access; first entry |
| 567 | * wins (like the `findIndex()` scan it replaces). */ |
| 568 | let _latexBySymbolName: Map<string, string> | null = null; |
| 569 | function getLatexBySymbolName(): Map<string, string> { |
| 570 | if (!_latexBySymbolName) { |
| 571 | _latexBySymbolName = new Map(); |
| 572 | for (const [name, latex] of SYMBOLS) |
| 573 | if (!_latexBySymbolName.has(name)) _latexBySymbolName.set(name, latex); |
| 574 | } |
| 575 | return _latexBySymbolName; |
| 576 | } |
| 577 | |
| 578 | /** Lazy map from Unicode codepoint to LaTeX command. Built once from the |
| 579 | * SYMBOLS table on first access; first entry wins. */ |
| 580 | let _latexByCodepoint: Map<number, string> | null = null; |
| 581 | function getLatexByCodepoint(): Map<number, string> { |
| 582 | if (!_latexByCodepoint) { |
| 583 | _latexByCodepoint = new Map(); |
| 584 | for (const [, latex, codepoint] of SYMBOLS) |
| 585 | if (!_latexByCodepoint.has(codepoint)) |
| 586 | _latexByCodepoint.set(codepoint, latex); |
| 587 | } |
| 588 | return _latexByCodepoint; |
| 589 | } |
| 590 | |
| 591 | /** Digits spelled out, e.g. for `\mathbb{1}`. See `specialName()` */ |
| 592 | const SPELLED_OUT_DIGITS = new Map<string, string>([ |
| 593 | ['zero', '0'], |
| 594 | ['one', '1'], |
| 595 | ['two', '2'], |
| 596 | ['three', '3'], |
| 597 | ['four', '4'], |
| 598 | ['five', '5'], |
| 599 | ['six', '6'], |
| 600 | ['seven', '7'], |
| 601 | ['eight', '8'], |
| 602 | ['nine', '9'], |
| 603 | ['ten', '10'], |
| 604 | ]); |
| 605 | |
| 606 | /** Special symbol-body names mapped to LaTeX. See `specialName()` */ |
| 607 | const EXTRA_SYMBOLS = new Map<string, string>([ |
| 608 | ['plus', '+'], |
| 609 | ['minus', '-'], |
| 610 | ['pm', '\\pm'], |
| 611 | ['ast', '\\ast'], |
| 612 | ['dag', '\\dag'], |
| 613 | ['ddag', '\\ddag'], |
| 614 | ['hash', '\\#'], |
no test coverage detected