-
Notifications
You must be signed in to change notification settings - Fork 13
/
theorems.typ
284 lines (246 loc) · 6.07 KB
/
theorems.typ
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
// Store theorem environment numbering
#let thmcounters = state("thm",
(
"counters": ("heading": ()),
"latest": ()
)
)
#let thmenv(identifier, base, base_level, fmt) = {
let global_numbering = numbering
return (
..args,
body,
number: auto,
numbering: "1.1",
refnumbering: auto,
supplement: identifier,
base: base,
base_level: base_level
) => {
let name = none
if args != none and args.pos().len() > 0 {
name = args.pos().first()
}
if refnumbering == auto {
refnumbering = numbering
}
let result = none
if number == auto and numbering == none {
number = none
}
if number == auto and numbering != none {
result = context {
let heading-counter = counter(heading).get()
return thmcounters.update(thmpair => {
let counters = thmpair.at("counters")
// Manually update heading counter
counters.at("heading") = heading-counter
if not identifier in counters.keys() {
counters.insert(identifier, (0, ))
}
let tc = counters.at(identifier)
if base != none {
let bc = counters.at(base)
// Pad or chop the base count
if base_level != none {
if bc.len() < base_level {
bc = bc + (0,) * (base_level - bc.len())
} else if bc.len() > base_level{
bc = bc.slice(0, base_level)
}
}
// Reset counter if the base counter has updated
if tc.slice(0, -1) == bc {
counters.at(identifier) = (..bc, tc.last() + 1)
} else {
counters.at(identifier) = (..bc, 1)
}
} else {
// If we have no base counter, just count one level
counters.at(identifier) = (tc.last() + 1,)
let latest = counters.at(identifier)
}
let latest = counters.at(identifier)
return (
"counters": counters,
"latest": latest
)
})
}
number = context {
global_numbering(numbering, ..thmcounters.get().at("latest"))
}
}
return figure(
result + // hacky!
fmt(name, number, body, ..args.named()) +
[#metadata(identifier) <meta:thmenvcounter>],
kind: "thmenv",
outlined: false,
caption: name,
supplement: supplement,
numbering: refnumbering,
)
}
}
#let thmbox(
identifier,
head,
..blockargs,
supplement: auto,
padding: (top: 0.5em, bottom: 0.5em),
namefmt: x => [(#x)],
titlefmt: strong,
bodyfmt: x => x,
separator: [#h(0.1em):#h(0.2em)],
base: "heading",
base_level: none,
) = {
if supplement == auto {
supplement = head
}
let boxfmt(name, number, body, title: auto, ..blockargs_individual) = {
if not name == none {
name = [ #namefmt(name)]
} else {
name = []
}
if title == auto {
title = head
}
if not number == none {
title += " " + number
}
title = titlefmt(title)
body = bodyfmt(body)
pad(
..padding,
block(
width: 100%,
inset: 1.2em,
radius: 0.3em,
breakable: false,
..blockargs.named(),
..blockargs_individual.named(),
[#title#name#separator#body]
)
)
}
return thmenv(
identifier,
base,
base_level,
boxfmt
).with(
supplement: supplement,
)
}
#let thmplain = thmbox.with(
padding: (top: 0em, bottom: 0em),
breakable: true,
inset: (top: 0em, left: 1.2em, right: 1.2em),
namefmt: name => emph([(#name)]),
titlefmt: emph,
)
// Track whether the qed symbol has already been placed in a proof
#let thm-qed-done = state("thm-qed-done", false)
// The configured QED symbol
#let thm-qed-symbol = state("thm-qed-symbol", $qed$)
// Show the qed symbol, update state
#let thm-qed-show = {
thm-qed-done.update(true)
context [#thm-qed-symbol.get()]
}
// If placed in a block equation/enum/list, place a qed symbol to its right
#let qedhere = metadata("thm-qedhere")
// Checks if content x contains the qedhere tag
#let thm-has-qedhere(x) = {
if x == "thm-qedhere" {
return true
}
if type(x) == content {
for (f, c) in x.fields() {
if thm-has-qedhere(c) {
return true
}
}
}
if type(x) == array {
for c in x {
if thm-has-qedhere(c) {
return true
}
}
}
return false
}
// bodyfmt for proofs
#let proof-bodyfmt(body) = {
thm-qed-done.update(false)
body
context {
if thm-qed-done.at(here()) == false {
h(1fr)
thm-qed-show
}
}
}
#let thmproof(..args) = thmplain(
..args,
namefmt: emph,
bodyfmt: proof-bodyfmt,
..args.named()
).with(numbering: none)
#let thmrules(qed-symbol: $qed$, doc) = {
show figure.where(kind: "thmenv"): set block(breakable: true)
show figure.where(kind: "thmenv"): set align(start)
show figure.where(kind: "thmenv"): it => it.body
show ref: it => {
if it.element == none {
return it
}
if it.element.func() != figure {
return it
}
if it.element.kind != "thmenv" {
return it
}
let supplement = it.element.supplement
if it.citation.supplement != none {
supplement = it.citation.supplement
}
let loc = it.element.location()
let thms = query(selector(<meta:thmenvcounter>).after(loc))
let number = thmcounters.at(thms.first().location()).at("latest")
return link(
it.target,
[#supplement~#numbering(it.element.numbering, ..number)]
)
}
show math.equation.where(block: true): eq => {
if thm-has-qedhere(eq) and thm-qed-done.at(eq.location()) == false {
grid(
columns: (1fr, auto, 1fr),
[], eq, align(right + horizon)[#thm-qed-show]
)
} else {
eq
}
}
show enum.item: it => {
show metadata.where(value: "thm-qedhere"): {
h(1fr)
thm-qed-show
}
it
}
show list.item: it => {
show metadata.where(value: "thm-qedhere"): {
h(1fr)
thm-qed-show
}
it
}
thm-qed-symbol.update(qed-symbol)
doc
}