mathshuttle 0.8.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mathshuttle/__init__.py +3 -0
- mathshuttle/__main__.py +343 -0
- mathshuttle/_clipboard_macos.py +235 -0
- mathshuttle/_clipboard_win32.py +199 -0
- mathshuttle/_hotkeys_macos.py +137 -0
- mathshuttle/app.py +1483 -0
- mathshuttle/clipboard.py +127 -0
- mathshuttle/compat.py +1537 -0
- mathshuttle/convert.py +496 -0
- mathshuttle/diagnose.py +184 -0
- mathshuttle/documents.py +903 -0
- mathshuttle/hotkeys.py +179 -0
- mathshuttle/latex_wrap.py +333 -0
- mathshuttle/office_html.py +234 -0
- mathshuttle/packs.py +610 -0
- mathshuttle/pandoc_engine.py +1732 -0
- mathshuttle/pandoc_fetch.py +325 -0
- mathshuttle/paths.py +19 -0
- mathshuttle/preview.py +232 -0
- mathshuttle/recall.py +337 -0
- mathshuttle/sv_theme/LICENSE +21 -0
- mathshuttle/sv_theme/sv.tcl +130 -0
- mathshuttle/sv_theme/theme/dark.tcl +465 -0
- mathshuttle/sv_theme/theme/light.tcl +469 -0
- mathshuttle/sv_theme/theme/sprites_dark.tcl +95 -0
- mathshuttle/sv_theme/theme/sprites_light.tcl +95 -0
- mathshuttle/sv_theme/theme/spritesheet_dark.png +0 -0
- mathshuttle/sv_theme/theme/spritesheet_light.png +0 -0
- mathshuttle/theme.py +127 -0
- mathshuttle/tidy.py +830 -0
- mathshuttle/wordexport.py +887 -0
- mathshuttle-0.8.0.dist-info/METADATA +113 -0
- mathshuttle-0.8.0.dist-info/RECORD +37 -0
- mathshuttle-0.8.0.dist-info/WHEEL +5 -0
- mathshuttle-0.8.0.dist-info/entry_points.txt +5 -0
- mathshuttle-0.8.0.dist-info/licenses/LICENSE +21 -0
- mathshuttle-0.8.0.dist-info/top_level.txt +1 -0
mathshuttle/packs.py
ADDED
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
"""Built-in definitions for packages that papers load and texmath does not know: physics, braket,
|
|
2
|
+
slashed, and siunitx quantities with units written as letters.
|
|
3
|
+
|
|
4
|
+
They are applied by :func:`compat.for_texmath` after the user's and the document's own macros, and only
|
|
5
|
+
to commands that neither defines (an author's own ``\\abs`` wins). A command whose name also means
|
|
6
|
+
something standard (physics' ``\\div``, ``\\grad`` with ``\\div`` is ÷ in LaTeX) is rewritten only when
|
|
7
|
+
the document loads the package. The LaTeX writer can also use the packs to write out their commands.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import re
|
|
12
|
+
from typing import Callable, Dict, List, Optional, Sequence, Set, Tuple
|
|
13
|
+
|
|
14
|
+
from .compat import _CS, _GAP, _argument, _find, _group_end, _optional, _unbrace
|
|
15
|
+
|
|
16
|
+
# ---- reading a command's arguments -----------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class _Args:
|
|
20
|
+
"""What follows a command: a star, [options] and {arguments}, or a delimited argument (\\qty(...))."""
|
|
21
|
+
|
|
22
|
+
def __init__(self, tex: str, pos: int, for_latex: bool = False):
|
|
23
|
+
self.tex = tex
|
|
24
|
+
self.end = pos
|
|
25
|
+
self.star = False
|
|
26
|
+
self.for_latex = for_latex
|
|
27
|
+
|
|
28
|
+
def take_star(self) -> bool:
|
|
29
|
+
i = _GAP.match(self.tex, self.end).end()
|
|
30
|
+
if self.tex[i:i + 1] == "*":
|
|
31
|
+
self.end = i + 1
|
|
32
|
+
self.star = True
|
|
33
|
+
return self.star
|
|
34
|
+
|
|
35
|
+
def option(self) -> Optional[str]:
|
|
36
|
+
j = _optional(self.tex, self.end, 1)
|
|
37
|
+
if j == self.end:
|
|
38
|
+
return None
|
|
39
|
+
text = self.tex[self.end:j].strip()
|
|
40
|
+
self.end = j
|
|
41
|
+
return text[1:-1]
|
|
42
|
+
|
|
43
|
+
def brace(self) -> Optional[str]:
|
|
44
|
+
"""The next {group} (after spaces), or None if the next thing is not a brace group."""
|
|
45
|
+
i = _GAP.match(self.tex, self.end).end()
|
|
46
|
+
if self.tex[i:i + 1] != "{":
|
|
47
|
+
return None
|
|
48
|
+
j = _group_end(self.tex, i)
|
|
49
|
+
if j < 0:
|
|
50
|
+
return None
|
|
51
|
+
self.end = j
|
|
52
|
+
return self.tex[i + 1:j - 1]
|
|
53
|
+
|
|
54
|
+
def first(self) -> Optional[str]:
|
|
55
|
+
"""A {group}, or else one token (physics takes \\ket\\psi as \\ket{\\psi})."""
|
|
56
|
+
x = self.brace()
|
|
57
|
+
return self.argument() if x is None else x
|
|
58
|
+
|
|
59
|
+
def argument(self) -> Optional[str]:
|
|
60
|
+
"""A TeX argument: a group, a control sequence or one character (\\vb r)."""
|
|
61
|
+
a, b = _argument(self.tex, self.end)
|
|
62
|
+
if b <= a:
|
|
63
|
+
return None
|
|
64
|
+
self.end = b
|
|
65
|
+
return _unbrace(self.tex[a:b])
|
|
66
|
+
|
|
67
|
+
def delimited(self) -> Optional[Tuple[str, str]]:
|
|
68
|
+
"""(opening, content) of \\qty(..), \\qty[..], \\qty|..|, \\qty{..}; None for anything else."""
|
|
69
|
+
i = _GAP.match(self.tex, self.end).end()
|
|
70
|
+
opening = self.tex[i:i + 1]
|
|
71
|
+
if opening == "{":
|
|
72
|
+
content = self.brace()
|
|
73
|
+
return None if content is None else ("{", content)
|
|
74
|
+
closing = {"(": ")", "[": "]", "|": "|"}.get(opening)
|
|
75
|
+
if not closing:
|
|
76
|
+
return None
|
|
77
|
+
depth, j = 0, i + 1
|
|
78
|
+
while j < len(self.tex):
|
|
79
|
+
ch = self.tex[j]
|
|
80
|
+
if ch == "\\":
|
|
81
|
+
j += 2
|
|
82
|
+
continue
|
|
83
|
+
if ch == "{":
|
|
84
|
+
depth += 1
|
|
85
|
+
elif ch == "}":
|
|
86
|
+
depth -= 1
|
|
87
|
+
elif not depth and opening != "|" and ch == opening:
|
|
88
|
+
depth_paren = 1 # a nested pair of the same kind: (a (b) c)
|
|
89
|
+
k = j + 1
|
|
90
|
+
while k < len(self.tex) and depth_paren:
|
|
91
|
+
if self.tex[k] == "\\":
|
|
92
|
+
k += 2
|
|
93
|
+
continue
|
|
94
|
+
depth_paren += {opening: 1, closing: -1}.get(self.tex[k], 0)
|
|
95
|
+
k += 1
|
|
96
|
+
j = k
|
|
97
|
+
continue
|
|
98
|
+
elif not depth and ch == closing:
|
|
99
|
+
self.end = j + 1
|
|
100
|
+
return opening, self.tex[i + 1:j]
|
|
101
|
+
j += 1
|
|
102
|
+
return None
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _split_bars(text: str) -> List[str]:
|
|
106
|
+
"""`text` split at its own (not nested) | bars: \\braket{a|b} -> [a, b]."""
|
|
107
|
+
parts, depth, last, i = [], 0, 0, 0
|
|
108
|
+
while i < len(text):
|
|
109
|
+
ch = text[i]
|
|
110
|
+
if ch == "\\":
|
|
111
|
+
i += 2
|
|
112
|
+
continue
|
|
113
|
+
if ch == "{":
|
|
114
|
+
depth += 1
|
|
115
|
+
elif ch == "}":
|
|
116
|
+
depth -= 1
|
|
117
|
+
elif ch == "|" and not depth:
|
|
118
|
+
parts.append(text[last:i])
|
|
119
|
+
last = i + 1
|
|
120
|
+
i += 1
|
|
121
|
+
return parts + [text[last:]]
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _fence(left: str, right: str, content: str, sized: bool = True) -> str:
|
|
125
|
+
"""\\left< content \\right> (or without sizing)."""
|
|
126
|
+
if sized:
|
|
127
|
+
return "\\left%s {%s} \\right%s" % (left, content, right)
|
|
128
|
+
return "%s {%s} %s" % (left, content, right)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _bracket(parts: Sequence[str], sized: bool, left: str = "\\langle", right: str = "\\rangle") -> str:
|
|
132
|
+
sep = " \\middle| " if sized else " \\mid "
|
|
133
|
+
inner = sep.join("{%s}" % p.strip() for p in parts)
|
|
134
|
+
return ("\\left%s %s \\right%s" % (left, inner, right)) if sized else ("%s %s %s" % (left, inner, right))
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
# ---- the commands ------------------------------------------------------------------------------------
|
|
138
|
+
# A handler gets the parsed arguments and returns the replacement, or None to leave the command alone.
|
|
139
|
+
Handler = Callable[[_Args, Set[str]], Optional[str]]
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _physics_style(packages) -> bool:
|
|
143
|
+
"""physics' meaning of the commands it shares with braket (unless only braket is loaded)."""
|
|
144
|
+
return "physics" in packages or "braket" not in packages
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _bra(a: _Args, packages) -> Optional[str]:
|
|
148
|
+
sized = not a.take_star()
|
|
149
|
+
x = a.first()
|
|
150
|
+
if x is None:
|
|
151
|
+
return None
|
|
152
|
+
k = re.compile(_GAP.pattern + r"\\ket" + _CS).match(a.tex, a.end)
|
|
153
|
+
if k and _physics_style(packages): # physics contracts \bra{a}\ket{b} into <a|b>
|
|
154
|
+
ket = _Args(a.tex, k.end())
|
|
155
|
+
ket.take_star()
|
|
156
|
+
y = ket.first()
|
|
157
|
+
if y is not None:
|
|
158
|
+
a.end = ket.end
|
|
159
|
+
return _bracket([x, y], sized)
|
|
160
|
+
return _fence("\\langle", "|", x, sized)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _ket(a: _Args, _p) -> Optional[str]:
|
|
164
|
+
sized = not a.take_star()
|
|
165
|
+
x = a.first()
|
|
166
|
+
return None if x is None else _fence("|", "\\rangle", x, sized)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _braket(a: _Args, packages) -> Optional[str]:
|
|
170
|
+
sized = not a.take_star()
|
|
171
|
+
x = a.first()
|
|
172
|
+
if x is None:
|
|
173
|
+
return None
|
|
174
|
+
parts = _split_bars(x)
|
|
175
|
+
# physics: \braket{a}{b}; braket's \braket takes one argument (\braket{a|b}), a group after it is
|
|
176
|
+
# the next factor
|
|
177
|
+
if len(parts) == 1 and _physics_style(packages):
|
|
178
|
+
y = a.brace()
|
|
179
|
+
if y is not None:
|
|
180
|
+
return _bracket([x, y], sized)
|
|
181
|
+
if "physics" in packages:
|
|
182
|
+
parts = [x, x] # physics' \braket{a} is <a|a>
|
|
183
|
+
return _bracket(parts, sized)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def _sized_braket(a: _Args, _p) -> Optional[str]: # braket's \Braket{a | A | b}
|
|
187
|
+
x = a.brace()
|
|
188
|
+
return None if x is None else _bracket(_split_bars(x), True)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _set(sized: bool) -> Handler:
|
|
192
|
+
def handler(a: _Args, _p) -> Optional[str]:
|
|
193
|
+
x = a.brace()
|
|
194
|
+
if x is None:
|
|
195
|
+
return None
|
|
196
|
+
parts = _split_bars(x)
|
|
197
|
+
if len(parts) > 2: # only the first | separates
|
|
198
|
+
parts = [parts[0], "|".join(parts[1:])]
|
|
199
|
+
return _bracket(parts, sized, "\\{", "\\}")
|
|
200
|
+
return handler
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _expval(a: _Args, _p) -> Optional[str]:
|
|
204
|
+
sized = not a.take_star()
|
|
205
|
+
a.star = False
|
|
206
|
+
a.take_star() # \ev**{A}{psi}: physics' second star (no sizing either)
|
|
207
|
+
x = a.first()
|
|
208
|
+
if x is None:
|
|
209
|
+
return None
|
|
210
|
+
y = a.brace()
|
|
211
|
+
return _bracket([x] if y is None else [y, x, y], sized)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _mel(a: _Args, _p) -> Optional[str]:
|
|
215
|
+
sized = not a.take_star()
|
|
216
|
+
n, op, m = a.brace(), a.brace(), a.brace()
|
|
217
|
+
return None if m is None else _bracket([n, op, m], sized)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def _innerproduct(a: _Args, _p) -> Optional[str]:
|
|
221
|
+
sized = not a.take_star()
|
|
222
|
+
x = a.first()
|
|
223
|
+
if x is None:
|
|
224
|
+
return None
|
|
225
|
+
y = a.brace()
|
|
226
|
+
return _bracket([x, x if y is None else y], sized) # physics' \ip{a} is <a|a>
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def _outerproduct(a: _Args, _p) -> Optional[str]:
|
|
230
|
+
sized = not a.take_star()
|
|
231
|
+
x = a.first()
|
|
232
|
+
if x is None:
|
|
233
|
+
return None
|
|
234
|
+
y = a.brace()
|
|
235
|
+
y = x if y is None else y
|
|
236
|
+
return _fence("|", "\\rangle", x, sized) + _fence("\\langle", "|", y, sized)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def _derivative(symbol: str) -> Handler:
|
|
240
|
+
def handler(a: _Args, _p) -> Optional[str]:
|
|
241
|
+
star = a.take_star() # \dv*: in the line (f'/x)
|
|
242
|
+
order = a.option()
|
|
243
|
+
first = a.brace()
|
|
244
|
+
if first is None:
|
|
245
|
+
return None
|
|
246
|
+
second = a.brace()
|
|
247
|
+
third = a.brace() if symbol == "\\partial" and second is not None else None
|
|
248
|
+
power = "^{%s}" % order.strip() if order else ""
|
|
249
|
+
if second is None: # \dv{x}: the operator d/dx
|
|
250
|
+
num, den = symbol + power, "%s {%s}%s" % (symbol, first, power)
|
|
251
|
+
elif third is not None: # \pdv{f}{x}{y}: mixed
|
|
252
|
+
num, den = "%s^{2} {%s}" % (symbol, first), "%s {%s} \\, %s {%s}" % (symbol, second, symbol, third)
|
|
253
|
+
else:
|
|
254
|
+
num, den = "%s%s {%s}" % (symbol, power, first), "%s {%s}%s" % (symbol, second, power)
|
|
255
|
+
return "{%s} / {%s}" % (num, den) if star else "\\frac{%s}{%s}" % (num, den)
|
|
256
|
+
return handler
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def _pair(left: str, right: str) -> Handler:
|
|
260
|
+
def handler(a: _Args, _p) -> Optional[str]:
|
|
261
|
+
sized = not a.take_star()
|
|
262
|
+
x, y = a.brace(), a.brace()
|
|
263
|
+
return None if y is None else _fence(left, right, "{%s}, {%s}" % (x, y), sized)
|
|
264
|
+
return handler
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
_QTY = {"(": ("(", ")"), "[": ("[", "]"), "|": ("|", "|"), "{": ("\\{", "\\}")}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def _qty(a: _Args, _p) -> Optional[str]:
|
|
271
|
+
"""physics' \\qty(..) / [..] / |..| / {..}; siunitx' \\qty{number}{unit} is left to texmath (or to
|
|
272
|
+
the literal-unit rule below)."""
|
|
273
|
+
start = a.end
|
|
274
|
+
sized = not a.take_star()
|
|
275
|
+
found = a.delimited()
|
|
276
|
+
if found is None:
|
|
277
|
+
return None
|
|
278
|
+
opening, content = found
|
|
279
|
+
probe = _Args(a.tex, a.end)
|
|
280
|
+
# \qty{3.2}{\per\second}, \qty[per-mode=symbol]{10}{m/s}: siunitx
|
|
281
|
+
if (opening == "{" and probe.brace() is not None) or (
|
|
282
|
+
opening == "[" and probe.brace() is not None and probe.brace() is not None):
|
|
283
|
+
a.end = start
|
|
284
|
+
return None
|
|
285
|
+
left, right = _QTY[opening]
|
|
286
|
+
return _fence(left, right, content, sized)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def _fixed_fence(left: str, right: str) -> Handler: # \pqty{x}, \abs{x}, \norm{x}
|
|
290
|
+
def handler(a: _Args, _p) -> Optional[str]:
|
|
291
|
+
sized = not a.take_star()
|
|
292
|
+
x = a.brace()
|
|
293
|
+
return None if x is None else _fence(left, right, x, sized)
|
|
294
|
+
return handler
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def _font(plain: str, starred: str, accent: str = "") -> Handler:
|
|
298
|
+
def handler(a: _Args, _p) -> Optional[str]:
|
|
299
|
+
star = a.take_star()
|
|
300
|
+
x = a.argument()
|
|
301
|
+
if x is None:
|
|
302
|
+
return None
|
|
303
|
+
inner = "%s{%s}" % (starred if star else plain, x)
|
|
304
|
+
return "%s{%s}" % (accent, inner) if accent else inner
|
|
305
|
+
return handler
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def _arrow_vector(a: _Args, _p) -> Optional[str]: # \va{x}, \va*{x}
|
|
309
|
+
star = a.take_star()
|
|
310
|
+
x = a.argument()
|
|
311
|
+
if x is None:
|
|
312
|
+
return None
|
|
313
|
+
return "\\vec{%s{%s}}" % ("\\boldsymbol" if star else "\\mathbf", x) # physics: an arrow over bold
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def _operator(name: str) -> Handler:
|
|
317
|
+
def handler(a: _Args, _p) -> Optional[str]:
|
|
318
|
+
return "\\operatorname{%s}" % name
|
|
319
|
+
return handler
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def _order(a: _Args, _p) -> Optional[str]:
|
|
323
|
+
sized = not a.take_star()
|
|
324
|
+
found = a.delimited()
|
|
325
|
+
if found is None:
|
|
326
|
+
return None
|
|
327
|
+
return "\\mathcal{O}" + _fence("(", ")", found[1], sized)
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def _eval(a: _Args, _p) -> Optional[str]:
|
|
331
|
+
a.take_star()
|
|
332
|
+
x = a.brace()
|
|
333
|
+
return None if x is None else "\\left. {%s} \\right|" % x
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def _vector_operator(symbol: str) -> Handler: # \grad, \curl, \laplacian, \div (with an optional argument)
|
|
337
|
+
def handler(a: _Args, _p) -> Optional[str]:
|
|
338
|
+
a.take_star()
|
|
339
|
+
x = a.brace()
|
|
340
|
+
return symbol if x is None else "%s {%s}" % (symbol, x)
|
|
341
|
+
return handler
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def _symbol(symbol: str) -> Handler:
|
|
345
|
+
def handler(a: _Args, _p) -> Optional[str]:
|
|
346
|
+
return symbol
|
|
347
|
+
return handler
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
_MQTY = {"(": "pmatrix", "[": "bmatrix", "|": "vmatrix", "{": "matrix"}
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def _mqty(a: _Args, _p) -> Optional[str]:
|
|
354
|
+
a.take_star()
|
|
355
|
+
found = a.delimited()
|
|
356
|
+
if found is None:
|
|
357
|
+
return None
|
|
358
|
+
env = _MQTY[found[0]]
|
|
359
|
+
return "\\begin{%s} %s \\end{%s}" % (env, found[1], env)
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def _matrix(env: str) -> Handler: # \pmqty{..}, \bmqty{..}, \vmqty{..}
|
|
363
|
+
def handler(a: _Args, _p) -> Optional[str]:
|
|
364
|
+
x = a.brace()
|
|
365
|
+
return None if x is None else "\\begin{%s} %s \\end{%s}" % (env, x, env)
|
|
366
|
+
return handler
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def _slashed(a: _Args, _p) -> Optional[str]:
|
|
370
|
+
x = a.argument()
|
|
371
|
+
if x is None:
|
|
372
|
+
return None
|
|
373
|
+
x = x.strip()
|
|
374
|
+
if re.fullmatch(r"[A-Za-z]", x):
|
|
375
|
+
return "{\\not %s}" % x # a slash through the letter (texmath: U+0338)
|
|
376
|
+
if a.for_latex:
|
|
377
|
+
return "{\\not{%s}}" % x
|
|
378
|
+
return "\\cancel{%s}" % x
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def _indices(text: str) -> Optional[str]:
|
|
382
|
+
"""tensor's index list ^a_b^c: each index on its own, staggered as the package sets them."""
|
|
383
|
+
out, i = [], 0
|
|
384
|
+
while i < len(text):
|
|
385
|
+
ch = text[i]
|
|
386
|
+
if ch.isspace():
|
|
387
|
+
i += 1
|
|
388
|
+
continue
|
|
389
|
+
if ch not in "^_":
|
|
390
|
+
return None
|
|
391
|
+
a = _Args(text, i + 1)
|
|
392
|
+
index = a.argument()
|
|
393
|
+
if index is None:
|
|
394
|
+
return None
|
|
395
|
+
out.append("{}%s{%s}" % (ch, index))
|
|
396
|
+
i = a.end
|
|
397
|
+
return "".join(out)
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def _tensor(a: _Args, _p) -> Optional[str]: # \tensor{T}{^a_b}, \tensor*{T}{^a_b}
|
|
401
|
+
a.take_star()
|
|
402
|
+
base, indices = a.brace(), a.brace()
|
|
403
|
+
written = None if indices is None else _indices(indices)
|
|
404
|
+
return None if written is None else "{%s}%s" % (base, written)
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
def _index_list(a: _Args, _p) -> Optional[str]: # T\indices{^a_b}
|
|
408
|
+
indices = a.brace()
|
|
409
|
+
return None if indices is None else _indices(indices)
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
# siunitx with a unit written as letters (kHz, m/s^2): texmath sets those letters as italic variables
|
|
413
|
+
def _literal_unit(unit: str) -> Optional[str]:
|
|
414
|
+
unit = unit.strip()
|
|
415
|
+
if not unit or "\\" in unit:
|
|
416
|
+
return None # \kilo\hertz: texmath reads siunitx' unit macros itself
|
|
417
|
+
unit = re.sub(r"\s*[.~]\s*", "\\\\,", unit) # kg.m, kg~m: a thin space between units
|
|
418
|
+
return "\\mathrm{%s}" % unit
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def _si_latex(command: str) -> str:
|
|
422
|
+
"""Let texmath read this siunitx number or unit and return its ordinary LaTeX spelling."""
|
|
423
|
+
from .pandoc_engine import Equation, read_mathml, write_mathml
|
|
424
|
+
return read_mathml(write_mathml(Equation(command)), tidy=False)[0].tex
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def _number(value: str, for_latex: bool) -> str:
|
|
428
|
+
if for_latex and re.fullmatch(r"[+-]?\d+(?:\.\d+)?", value):
|
|
429
|
+
return value
|
|
430
|
+
command = "\\num{%s}" % value
|
|
431
|
+
return _si_latex(command) if for_latex else command
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def _quantity(a: _Args, _p) -> Optional[str]: # \qty{12}{kHz}, \SI{12}{kHz}
|
|
435
|
+
start = a.end
|
|
436
|
+
options = a.option()
|
|
437
|
+
number = a.brace()
|
|
438
|
+
unit = a.brace()
|
|
439
|
+
if unit is None or (options and a.for_latex): # [round-precision=1]...: what siunitx prints is not
|
|
440
|
+
a.end = start # the number as written; LaTeX output keeps \qty
|
|
441
|
+
return None
|
|
442
|
+
written = _literal_unit(unit)
|
|
443
|
+
if written is None:
|
|
444
|
+
if a.for_latex:
|
|
445
|
+
return _si_latex("\\SI" + a.tex[start:a.end])
|
|
446
|
+
a.end = start
|
|
447
|
+
return None
|
|
448
|
+
return "%s\\,%s" % (_number(number, a.for_latex), written)
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def _unit(a: _Args, _p) -> Optional[str]: # \si{kHz}, \unit{kHz}
|
|
452
|
+
start = a.end
|
|
453
|
+
a.option()
|
|
454
|
+
unit = a.brace()
|
|
455
|
+
written = None if unit is None else _literal_unit(unit)
|
|
456
|
+
if written is None:
|
|
457
|
+
if a.for_latex and unit is not None:
|
|
458
|
+
return _si_latex("\\si" + a.tex[start:a.end])
|
|
459
|
+
a.end = start
|
|
460
|
+
return None
|
|
461
|
+
return written
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
def _numlist(a: _Args, _p) -> Optional[str]: # texmath writes \numlist{1;2;3} as "1, 2, & 3"
|
|
465
|
+
a.option()
|
|
466
|
+
x = a.brace()
|
|
467
|
+
if x is None:
|
|
468
|
+
return None
|
|
469
|
+
items = [_number(i.strip(), a.for_latex) for i in x.split(";")]
|
|
470
|
+
if len(items) == 1:
|
|
471
|
+
return items[0]
|
|
472
|
+
return ", ".join(items[:-1]) + "\\text{ and }" + items[-1]
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
# name -> (package, handler, only when the document loads the package)
|
|
476
|
+
PACKS: Dict[str, Tuple[str, Handler, bool]] = {}
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
def _add(package: str, names: str, handler: Handler, loaded_only: bool = False) -> None:
|
|
480
|
+
for name in names.split():
|
|
481
|
+
PACKS["\\" + name] = (package, handler, loaded_only)
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
_add("physics", "bra", _bra)
|
|
485
|
+
_add("physics", "ket", _ket)
|
|
486
|
+
_add("physics", "braket", _braket)
|
|
487
|
+
_add("physics", "ip innerproduct", _innerproduct)
|
|
488
|
+
_add("physics", "op outerproduct dyad ketbra", _outerproduct)
|
|
489
|
+
_add("physics", "expval ev expectationvalue", _expval)
|
|
490
|
+
_add("physics", "mel matrixel matrixelement", _mel)
|
|
491
|
+
_add("physics", "dv derivative", _derivative("\\mathrm{d}"))
|
|
492
|
+
_add("physics", "pdv partialderivative pderivative", _derivative("\\partial"))
|
|
493
|
+
_add("physics", "comm commutator", _pair("[", "]"))
|
|
494
|
+
_add("physics", "acomm anticommutator pb poissonbracket", _pair("\\{", "\\}"))
|
|
495
|
+
_add("physics", "qty quantity", _qty)
|
|
496
|
+
_add("physics", "pqty", _fixed_fence("(", ")"))
|
|
497
|
+
_add("physics", "bqty", _fixed_fence("[", "]"))
|
|
498
|
+
_add("physics", "Bqty", _fixed_fence("\\{", "\\}"))
|
|
499
|
+
_add("physics", "vqty", _fixed_fence("|", "|"))
|
|
500
|
+
_add("physics", "abs absolutevalue", _fixed_fence("|", "|"))
|
|
501
|
+
_add("physics", "norm", _fixed_fence("\\Vert", "\\Vert"))
|
|
502
|
+
_add("physics", "vb vectorbold", _font("\\mathbf", "\\boldsymbol"))
|
|
503
|
+
_add("physics", "vu vectorunit", _font("\\mathbf", "\\boldsymbol", "\\hat"))
|
|
504
|
+
_add("physics", "va vectorarrow", _arrow_vector)
|
|
505
|
+
_add("physics", "tr trace", _operator("tr"))
|
|
506
|
+
_add("physics", "Tr Trace", _operator("Tr"))
|
|
507
|
+
_add("physics", "rank", _operator("rank"))
|
|
508
|
+
_add("physics", "erf", _operator("erf"))
|
|
509
|
+
_add("physics", "Res Residue", _operator("Res"))
|
|
510
|
+
_add("physics", "pv principalvalue", _operator("P.V."))
|
|
511
|
+
_add("physics", "order", _order)
|
|
512
|
+
_add("physics", "eval evaluated", _eval)
|
|
513
|
+
_add("physics", "grad gradient", _vector_operator("\\nabla"))
|
|
514
|
+
_add("physics", "curl", _vector_operator("\\nabla\\times"))
|
|
515
|
+
_add("physics", "laplacian", _vector_operator("\\nabla^{2}"))
|
|
516
|
+
_add("physics", "divergence", _vector_operator("\\nabla\\cdot"))
|
|
517
|
+
_add("physics", "div", _vector_operator("\\nabla\\cdot"), loaded_only=True) # \div is ÷ in LaTeX
|
|
518
|
+
_add("physics", "cross crossproduct", _symbol("\\times"))
|
|
519
|
+
_add("physics", "vdot dotproduct", _symbol("\\cdot"))
|
|
520
|
+
_add("physics", "mqty matrixquantity", _mqty)
|
|
521
|
+
_add("physics", "pmqty", _matrix("pmatrix"))
|
|
522
|
+
_add("physics", "bmqty", _matrix("bmatrix"))
|
|
523
|
+
_add("physics", "vmqty", _matrix("vmatrix"))
|
|
524
|
+
_add("physics", "smqty", _matrix("smallmatrix"))
|
|
525
|
+
_add("braket", "Bra", _fixed_fence("\\langle", "|"))
|
|
526
|
+
_add("braket", "Ket", _fixed_fence("|", "\\rangle"))
|
|
527
|
+
_add("braket", "Braket", _sized_braket)
|
|
528
|
+
_add("braket", "set", _set(False))
|
|
529
|
+
_add("braket", "Set", _set(True))
|
|
530
|
+
_add("slashed", "slashed", _slashed)
|
|
531
|
+
_add("tensor", "tensor", _tensor)
|
|
532
|
+
_add("tensor", "indices", _index_list)
|
|
533
|
+
# AASTeX (aastex*.cls, which pandoc does not read): decimal arc units 6\farcs15
|
|
534
|
+
_add("aastex", "farcs", _symbol("{.}^{\\prime\\prime}"))
|
|
535
|
+
_add("aastex", "farcm", _symbol("{.}^{\\prime}"))
|
|
536
|
+
_add("aastex", "fdg", _symbol("{.}^{\\circ}"))
|
|
537
|
+
_add("aastex", "arcsec", _symbol("{}^{\\prime\\prime}"))
|
|
538
|
+
_add("aastex", "arcmin", _symbol("{}^{\\prime}"))
|
|
539
|
+
_add("aastex", "micron", _symbol("\\mu\\mathrm{m}"))
|
|
540
|
+
_add("siunitx", "SI", _quantity)
|
|
541
|
+
_add("siunitx", "si unit", _unit)
|
|
542
|
+
_add("siunitx", "numlist", _numlist)
|
|
543
|
+
# siunitx' \qty{12}{kHz} goes through physics' \qty handler first (which leaves the two-group form
|
|
544
|
+
# alone); the literal-unit rule for it is applied separately (see apply)
|
|
545
|
+
_QUANTITY_NAMES = ("\\qty", "\\SI")
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
_SHARED = {"\\bra", "\\ket", "\\braket"} # in physics and in braket, with different meanings
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
def packages_for(tex: str, defined: Set[str], packages: Set[str]) -> List[str]:
|
|
552
|
+
"""The LaTeX packages the pack commands in `tex` come from, for the preview (which sets the LaTeX as
|
|
553
|
+
written): physics or braket (not both: they define the same commands), slashed, tensor, siunitx."""
|
|
554
|
+
from .compat import parse_definitions
|
|
555
|
+
# a \newcommand{\abs} of the equation's own would clash with the package's; a \renewcommand needs it
|
|
556
|
+
defined = set(defined) | {d.name for d in parse_definitions(tex)[0]
|
|
557
|
+
if not re.match(r"\s*\\(?:renewcommand|RenewDocumentCommand)", d.source)}
|
|
558
|
+
needed: Set[str] = set()
|
|
559
|
+
for m in re.finditer(r"\\[A-Za-z]+", tex):
|
|
560
|
+
name = m.group()
|
|
561
|
+
if name not in PACKS or name in defined:
|
|
562
|
+
continue
|
|
563
|
+
package, _, loaded_only = PACKS[name]
|
|
564
|
+
if loaded_only and package not in packages or package == "aastex":
|
|
565
|
+
continue
|
|
566
|
+
if package == "physics" and name in _SHARED and not _physics_style(packages):
|
|
567
|
+
package = "braket" # the paper's own provider of \bra, \ket, \braket
|
|
568
|
+
if name == "\\qty": # siunitx' \qty{12}{kHz} or physics' \qty(..)
|
|
569
|
+
probe = _Args(tex, m.end())
|
|
570
|
+
probe.option()
|
|
571
|
+
package = "siunitx" if probe.brace() is not None and probe.brace() is not None else "physics"
|
|
572
|
+
needed.add(package)
|
|
573
|
+
if {"physics", "braket"} <= needed:
|
|
574
|
+
needed.discard("physics" if "braket" in packages and "physics" not in packages else "braket")
|
|
575
|
+
return sorted(needed)
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
def apply(tex: str, defined: Set[str], packages: Set[str], used: List[str], for_latex: bool = False) -> str:
|
|
579
|
+
"""`tex` with the commands of the built-in packs written out; `defined`: names the input or the
|
|
580
|
+
user's / document's macros define (left alone); `packages`: what the document loads; `used`
|
|
581
|
+
collects "package: \\name" for the note."""
|
|
582
|
+
names = [n for n, (pkg, _, loaded_only) in PACKS.items()
|
|
583
|
+
if n not in defined and (not loaded_only or pkg in packages)]
|
|
584
|
+
if not names:
|
|
585
|
+
return tex
|
|
586
|
+
pattern = re.compile(r"(%s)%s" % ("|".join(re.escape(n) for n in sorted(names, key=len, reverse=True)), _CS))
|
|
587
|
+
pos = 0
|
|
588
|
+
guard = 0
|
|
589
|
+
while guard < 10000:
|
|
590
|
+
guard += 1
|
|
591
|
+
m = _find(tex, pattern, pos)
|
|
592
|
+
if not m:
|
|
593
|
+
break
|
|
594
|
+
name = m.group(1)
|
|
595
|
+
package, handler, _ = PACKS[name]
|
|
596
|
+
args = _Args(tex, m.end(), for_latex)
|
|
597
|
+
new = handler(args, packages)
|
|
598
|
+
if new is None and name in _QUANTITY_NAMES: # \qty{12}{kHz}: siunitx with a literal unit
|
|
599
|
+
args = _Args(tex, m.end(), for_latex)
|
|
600
|
+
new = _quantity(args, packages)
|
|
601
|
+
package = "siunitx"
|
|
602
|
+
if new is None:
|
|
603
|
+
pos = m.end()
|
|
604
|
+
continue
|
|
605
|
+
used.append("%s: %s" % (package, name))
|
|
606
|
+
if re.search(r"\\[A-Za-z]+$", new) and re.match(r"[A-Za-z@]", tex[args.end:args.end + 1]):
|
|
607
|
+
new += " " # \ket{0}a: \rangle a, not the control word \ranglea
|
|
608
|
+
tex = tex[:m.start()] + new + tex[args.end:]
|
|
609
|
+
pos = m.start() # the arguments may hold more of them
|
|
610
|
+
return tex
|