-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.typ
More file actions
219 lines (181 loc) · 5.1 KB
/
Copy pathlib.typ
File metadata and controls
219 lines (181 loc) · 5.1 KB
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
#import "util.typ": _next_prime, _gcd
// Each quantity is defined by
// a) A numerical value.
// b) An implicit value, that is already contained within the unit, e.g.
// 1 inch has an implicit value of 0.0254 (meter).
// c) A fractional unit. Both nominator and divisor are stored as products
// of the base units, which are represented by prime numbers.
// It is therefore possible, to simplify the fractional unit and
// also determine the base units that it is made up of.
// d) A textual representation of the unit. The unit symbols are taken from
// the unify package, and spaces are inserted after every unit (but not
// prefixes!). Powers are inserted like "mm^2" and fractions like "/s".
// This is helpful for formatting the quantities with unify.
//
// Quantities may be calculated with (multiplication, division, exponential).
// Furthermore, a quantity may be rendered using `unify`.
#let unity = (value: 1.0, iv: 1.0, nid: 1, did: 1, format: "")
// Getters:
#let _value(x) = {
if type(x) == int or type(x) == float {
return x
} else if type(x) == dictionary {
return x.at("value", default: 1)
} else {
panic("Unknown format, type is " + str(type(x)) + "! If in math mode, make sure to escape number.")
}
}
#let _iv(x) = {
if type(x) == int or type(x) == float {
return 1
} else if type(x) == dictionary {
return x.at("iv", default: 1)
} else {
panic("Unknown format! If in math mode, make sure to escape number.")
}
}
#let _nid(x) = {
if type(x) == int or type(x) == float {
return 1
} else if type(x) == dictionary {
return x.at("nid", default: 1)
} else {
panic("Unknown format! If in math mode, make sure to escape number.")
}
}
#let _did(x) = {
if type(x) == int or type(x) == float {
return 1
} else if type(x) == dictionary {
return x.at("did", default: 1)
} else {
panic("Unknown format! If in math mode, make sure to escape number.")
}
}
#let _format(x) = {
if type(x) == int or type(x) == float {
return ""
} else if type(x) == dictionary {
return x.at("format", default: " ")
} else {
panic("Unknown format! If in math mode, make sure to escape number.")
}
}
// Create a new unit which is specified by its quantity and a different
// format. For example
// let minute = _define_unit(multiply(60, second), "min ")
#let define_unit(qty, format) = {
if type(qty) == dictionary {
qty.iv = _iv(qty) * _value(qty)
qty.value = 1
qty.format = format
return qty
} else if type(qty) == int or type(qty) == float {
let unit = unity
unit.iv = qty
unit.format = format
return unit
} else {
panic("Unknown format, type is " + str(type(x)) + "! If in math mode, make sure to escape number.")
}
}
// Formatting
// Invert a textual unit, e. g. "kg m /s" becomes
// "/kg m /s"
#let _invert_format(txt) = {
// 1. Insert slashes in front of every unit:
txt = "/" + txt.replace(" ", " /")
// 2. Remove extra slash at the end.
txt = txt.slice(0, -1)
// 3. Prune double slashes:
return txt.replace("//", "")
}
// Power a textual unit, e. g. "kg m^2" becomes "kg^3 m^6".
// Extra exponents are introduced
// before every space and at the end.
#let _pow_format(txt, exp) = {
let _pat = regex("[0-9]+")
txt = txt.split(" ").slice(0, -1)
let new = ""
for unit in txt {
let last-exp = unit.find(_pat)
if last-exp == none {
unit += "^" + str(exp)
} else {
let new-exp = str(int(last-exp) * exp)
unit = unit.replace(last-exp, new-exp)
}
new += unit + " "
}
return new
}
// Maths
#let multiply(..num) = {
let result = unity
for n in num.pos() {
result.value *= _value(n)
result.iv *= _iv(n)
result.nid *= _nid(n)
result.did *= _did(n)
result.format += _format(n)
}
return result
}
#let divide(a, b) = {
let result = unity
result.value = _value(a) / _value(b)
result.iv = _iv(a) / _iv(b)
result.nid = _nid(a) * _did(b)
result.did = _did(a) * _nid(b)
result.format = _format(a) + _invert_format(_format(b))
return result
}
#let pow(a, exp) = {
let result = unity
result.value = calc.pow(_value(a), exp)
result.iv = calc.pow(_iv(a), exp)
result.nid = calc.pow(_nid(a), exp)
result.did = calc.pow(_did(a), exp)
result.format = _pow_format(_format(a), exp)
return result
}
#let value-in(num, unit) = {
if type(unit) == dictionary {
unit.value = 1
}
let result = divide(num, unit)
if result.nid != result.did {
panic("Units do not match!")
}
return result.value * (_iv(num) / _iv(unit))
}
#let add(a, ..num) = {
let result = a
for n in num.pos() {
result.value += value-in(n, a)
}
return result
}
#let subtract(a, b) = {
a.value -= value-in(b, a)
return a
}
// Utility Functions
#let simplify(num) = {
// Simplify unit fraction:
let gcd = _gcd(nid(num), did(num))
num.nid = nid(num) / gcd
num.did = did(num) / gcd
return num
}
// Displays a quantity.
#let fmt(num, unit: auto, digits: auto) = {
if unit == auto {
unit = num
}
let val = value-in(num, unit)
if digits != auto {
val = calc.round(val, digits: digits)
}
return (val, _format(unit).trim())
}