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
|
"""
sphinx.util.texescape
~~~~~~~~~~~~~~~~~~~~~
TeX escaping helper.
:copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import re
if False:
# For type annotation
from typing import Dict # NOQA
tex_replacements = [
# map TeX special chars
('$', r'\$'),
('%', r'\%'),
('&', r'\&'),
('#', r'\#'),
('_', r'\_'),
('{', r'\{'),
('}', r'\}'),
('[', r'{[}'),
(']', r'{]}'),
('`', r'{}`'),
('\\', r'\textbackslash{}'),
('~', r'\textasciitilde{}'),
('<', r'\textless{}'),
('>', r'\textgreater{}'),
('^', r'\textasciicircum{}'),
# map special Unicode characters to TeX commands
('¶', r'\P{}'),
('§', r'\S{}'),
('€', r'\texteuro{}'),
('∞', r'\(\infty\)'),
('±', r'\(\pm\)'),
('→', r'\(\rightarrow\)'),
('‣', r'\(\rightarrow\)'),
('✓', r'\(\checkmark\)'),
('✔', r'\(\pmb{\checkmark}\)'),
# used to separate -- in options
('', r'{}'),
# map some special Unicode characters to similar ASCII ones
('⎽', r'\_'),
('–', r'\textendash{}'),
('|', r'\textbar{}'),
('ℯ', r'e'),
('ⅈ', r'i'),
('⁰', r'\(\sp{\text{0}}\)'),
('¹', r'\(\sp{\text{1}}\)'),
('²', r'\(\sp{\text{2}}\)'),
('³', r'\(\sp{\text{3}}\)'),
('⁴', r'\(\sp{\text{4}}\)'),
('⁵', r'\(\sp{\text{5}}\)'),
('⁶', r'\(\sp{\text{6}}\)'),
('⁷', r'\(\sp{\text{7}}\)'),
('⁸', r'\(\sp{\text{8}}\)'),
('⁹', r'\(\sp{\text{9}}\)'),
('₀', r'\(\sb{\text{0}}\)'),
('₁', r'\(\sb{\text{1}}\)'),
('₂', r'\(\sb{\text{2}}\)'),
('₃', r'\(\sb{\text{3}}\)'),
('₄', r'\(\sb{\text{4}}\)'),
('₅', r'\(\sb{\text{5}}\)'),
('₆', r'\(\sb{\text{6}}\)'),
('₇', r'\(\sb{\text{7}}\)'),
('₈', r'\(\sb{\text{8}}\)'),
('₉', r'\(\sb{\text{9}}\)'),
# Greek alphabet not escaped: pdflatex handles it via textalpha and inputenc
# OHM SIGN U+2126 is handled by LaTeX textcomp package
]
tex_escape_map = {} # type: Dict[int, str]
tex_replace_map = {}
tex_hl_escape_map_new = {}
def escape(s):
# type: (str) -> str
"""Escape text for LaTeX output."""
return s.translate(tex_escape_map)
def escape_abbr(text):
# type: (str) -> str
"""Adjust spacing after abbreviations. Works with @ letter or other."""
return re.sub(r'\.(?=\s|$)', r'.\@{}', text)
def init():
# type: () -> None
for a, b in tex_replacements:
tex_escape_map[ord(a)] = b
tex_replace_map[ord(a)] = '_'
for a, b in tex_replacements:
if a in '[]{}\\':
continue
tex_hl_escape_map_new[ord(a)] = b
|