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
//! Typesets a batch of famous formulas to see what is missing
//!
//! Usage:
//! cargo run --example corpus lists only the rejected ones
//! cargo run --example corpus -- all lists all of them
//! cargo run --example corpus -- page writes out a page for viewing them
use suki::font::{self, Font};
use suki::{Mode, Shape, escape, render_svg};
/// (field, name, formula)
#[rustfmt::skip]
const CORPUS: &[(&str, &str, &str)] = &[
("Analysis", "Euler's identity", r"e^{i\pi} + 1 = 0"),
("Analysis", "Gaussian integral", r"\int_{-\infty}^{\infty} e^{-x^2}\,dx = \sqrt{\pi}"),
("Analysis", "Basel problem", r"\sum_{n=1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{6}"),
("Analysis", "Taylor series", r"f(x) = \sum_{n=0}^{\infty} \frac{f^{(n)}(a)}{n!}(x-a)^n"),
("Analysis", "Stirling's approximation", r"n! \sim \sqrt{2\pi n}\left(\frac{n}{e}\right)^{n}"),
("Analysis", "Fourier transform", r"\hat{f}(\xi) = \int_{-\infty}^{\infty} f(x)\,e^{-2\pi i x \xi}\,dx"),
("Analysis", "Fundamental theorem of calculus", r"\int_a^b f'(x)\,dx = f(b) - f(a)"),
("Analysis", "Cauchy's integral formula", r"f(a) = \frac{1}{2\pi i} \oint_\gamma \frac{f(z)}{z-a}\,dz"),
("Analysis", "Standard limit", r"\lim_{x \to 0} \frac{\sin x}{x} = 1"),
("Analysis", "Euler product", r"\zeta(s) = \sum_{n=1}^{\infty} n^{-s} = \prod_{p} \left(1 - p^{-s}\right)^{-1}"),
("Analysis", "Chain rule for partial derivatives", r"\frac{\partial f}{\partial t} = \frac{\partial f}{\partial x}\frac{\partial x}{\partial t}"),
("Analysis", "Multiple integrals", r"\iint_D f(x,y)\,dx\,dy \quad \iiint_V f\,dV"),
("Analysis", "Continued fraction", r"\cfrac{1}{1 + \cfrac{1}{1 + \cfrac{1}{1 + \cdots}}}"),
("Analysis", "Big delimiters", r"\Bigl( \bigl[ \Bigl\{ x \Bigr\} \bigr] \Bigr)"),
("Algebra", "First isomorphism theorem", r"G/\ker\varphi \cong \operatorname{im}\varphi"),
("Algebra", "Semidirect product", r"G = N \rtimes H \quad N \trianglelefteq G"),
("Algebra", "Tensor product", r"M \otimes_R N \quad \operatorname{Hom}_R(M, N)"),
("Algebra", "Root space decomposition", r"\mathfrak{g} = \mathfrak{h} \oplus \bigoplus_{\alpha \in \Phi} \mathfrak{g}_\alpha"),
("Algebra", "Group action", r"g \cdot (h \cdot x) = (gh) \cdot x"),
("Algebra", "Definition of the determinant", r"\det A = \sum_{\sigma \in S_n} \operatorname{sgn}(\sigma) \prod_{i=1}^{n} a_{i\sigma(i)}"),
("Algebra", "Ideals", r"\mathfrak{p} \subsetneq \mathfrak{m} \subset R"),
("Homology", "Short exact sequence", r"0 \to A \xrightarrow{f} B \xrightarrow{g} C \to 0"),
("Homology", "Homology group", r"H_n(X) = \ker \partial_n / \operatorname{im} \partial_{n+1}"),
("Homology", "Chain complex", r"\cdots \to C_{n+1} \xrightarrow{\partial_{n+1}} C_n \xrightarrow{\partial_n} C_{n-1} \to \cdots"),
("Homology", "Boundary operator", r"\partial_n \circ \partial_{n+1} = 0"),
("Homology", "Derived functors", r"\operatorname{Ext}^1_R(A, B) \quad \operatorname{Tor}_n^R(A, B)"),
("Homology", "Categories and functors", r"F \colon \mathcal{C} \to \mathcal{D}"),
("Homology", "Natural transformation", r"\eta \colon F \Rightarrow G"),
("Homology", "Embedding and surjection", r"A \hookrightarrow B \twoheadrightarrow C"),
("Homology", "Limits", r"\varprojlim A_n \quad \varinjlim A_n"),
("Homology", "Euler characteristic", r"\chi(X) = \sum_{n} (-1)^n \operatorname{rank} H_n(X)"),
("Number theory", "Fermat's little theorem", r"a^{p-1} \equiv 1 \pmod p"),
("Number theory", "Prime number theorem", r"\pi(x) \sim \frac{x}{\log x}"),
("Number theory", "Euler's totient function", r"\varphi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right)"),
("Number theory", "Quadratic reciprocity", r"\left(\frac{p}{q}\right)\left(\frac{q}{p}\right) = (-1)^{\frac{p-1}{2}\cdot\frac{q-1}{2}}"),
("Number theory", "Riemann hypothesis", r"\zeta(s) = 0 \implies \Re(s) = \tfrac{1}{2}"),
("Number theory", "Divisibility", r"d \mid n \quad d \nmid m \quad n \bmod d"),
("Linear algebra", "Eigenvalue equation", r"A\mathbf{v} = \lambda \mathbf{v}"),
("Linear algebra", "Singular value decomposition", r"A = U \Sigma V^{*}"),
("Linear algebra", "Trace", r"\operatorname{tr}(AB) = \operatorname{tr}(BA)"),
("Linear algebra", "Inner product", r"\langle u, v \rangle = \sum_{i=1}^{n} \bar{u}_i v_i"),
("Linear algebra", "Norm", r"\lVert x \rVert_2 = \sqrt{\sum_i |x_i|^2}"),
("Linear algebra", "Matrix", r"A = \begin{pmatrix} a_{11} & \cdots & a_{1n} \\ \vdots & \ddots & \vdots \\ a_{m1} & \cdots & a_{mn} \end{pmatrix}"),
("Geometry", "Gauss-Bonnet", r"\int_M K\,dA + \int_{\partial M} k_g\,ds = 2\pi \chi(M)"),
("Geometry", "Metric", r"ds^2 = g_{\mu\nu}\,dx^\mu dx^\nu"),
("Geometry", "Christoffel symbols", r"\Gamma^{\lambda}_{\mu\nu} = \tfrac{1}{2} g^{\lambda\sigma}(\partial_\mu g_{\nu\sigma} + \partial_\nu g_{\mu\sigma} - \partial_\sigma g_{\mu\nu})"),
("Geometry", "Stokes' theorem", r"\int_M d\omega = \int_{\partial M} \omega"),
("Geometry", "Exterior product", r"\alpha \wedge \beta = -\beta \wedge \alpha"),
("Probability", "Bayes' theorem", r"P(A \mid B) = \frac{P(B \mid A)\,P(A)}{P(B)}"),
("Probability", "Normal distribution", r"f(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)"),
("Probability", "Expected value", r"\mathbb{E}[X] = \int_{-\infty}^{\infty} x\,dF(x)"),
("Probability", "Variance", r"\operatorname{Var}(X) = \mathbb{E}[X^2] - \left(\mathbb{E}[X]\right)^2"),
("Probability", "Central limit theorem", r"\frac{S_n - n\mu}{\sigma\sqrt{n}} \xrightarrow{d} N(0, 1)"),
("Probability", "Chebyshev", r"P\left(|X - \mu| \geq k\sigma\right) \leq \frac{1}{k^2}"),
("Physics", "Schrödinger equation", r"i\hbar \frac{\partial}{\partial t} \Psi = \hat{H} \Psi"),
("Physics", "Maxwell", r"\nabla \times \mathbf{B} = \mu_0\left(\mathbf{J} + \varepsilon_0 \frac{\partial \mathbf{E}}{\partial t}\right)"),
("Physics", "Einstein field equations", r"G_{\mu\nu} + \Lambda g_{\mu\nu} = \frac{8\pi G}{c^4} T_{\mu\nu}"),
("Physics", "Dirac equation", r"(i\gamma^\mu \partial_\mu - m)\psi = 0"),
("Physics", "Uncertainty principle", r"\Delta x\,\Delta p \geq \frac{\hbar}{2}"),
("Physics", "Boltzmann's entropy formula", r"S = k_B \log W"),
("Physics", "Euler-Lagrange", r"\frac{d}{dt}\frac{\partial L}{\partial \dot{q}} - \frac{\partial L}{\partial q} = 0"),
("Physics", "Path integral", r"\langle x_f | x_i \rangle = \int \mathcal{D}x\, e^{iS[x]/\hbar}"),
("Logic", "Universal and existential quantifiers", r"\forall x\, \exists y\, (x < y)"),
("Logic", "Implication and equivalence", r"P \Rightarrow Q \quad P \iff Q \quad \neg P \vee Q"),
("Logic", "Derivation", r"\Gamma \vdash \varphi \quad \mathcal{M} \models \varphi"),
("Logic", "Consequence", r"\therefore\ x = 1 \quad \because\ x^2 = 1 \wedge x > 0"),
("Set theory", "Power set", r"|\mathcal{P}(A)| = 2^{|A|}"),
("Set theory", "Continuum", r"\aleph_0 < 2^{\aleph_0}"),
("Set theory", "Set operations", r"A \cup B,\quad A \cap B,\quad A \setminus B,\quad A \sqcup B"),
("Set theory", "Empty set", r"A \cap B = \varnothing"),
("Set theory", "Set-builder notation", r"\{\, x \in \mathbb{R} \mid x^2 < 2 \,\}"),
("Combinatorics", "Binomial theorem", r"(x+y)^n = \sum_{k=0}^{n} \binom{n}{k} x^k y^{n-k}"),
("Combinatorics", "Multinomial coefficient", r"\binom{n}{k_1, k_2, \ldots, k_m}"),
("Combinatorics", "Double sum", r"\sum_{\substack{i=1 \\ i \neq j}}^{n} a_{ij}"),
("Combinatorics", "Catalan numbers", r"C_n = \frac{1}{n+1}\binom{2n}{n}"),
("Typesetting", "Stacking", r"a \overset{f}{\to} b \quad c \underset{g}{\to} d \quad e \stackrel{h}{=} f"),
("Typesetting", "Braces", r"\overbrace{a + b + c}^{n} \quad \underbrace{x + y}_{m}"),
("Typesetting", "Style commands", r"\displaystyle\frac{1}{2} \quad \textstyle\frac{1}{2} \quad \scriptstyle\frac{1}{2}"),
("Typesetting", "Bold symbols", r"\boldsymbol{\alpha} \quad \mathsf{S} \quad \mathtt{T} \quad \mathscr{L}"),
("Typesetting", "Long arrows", r"A \longrightarrow B \Longrightarrow C \longmapsto D"),
("Typesetting", "Cases", r"|x| = \begin{cases} x & x \geq 0 \\ -x & x < 0 \end{cases}"),
("Typesetting", "Lines above and below", r"\overline{z} \quad \underline{w} \quad \overrightarrow{AB}"),
("Typesetting", "Comparison symbols", r"a \leqslant b \quad c \geqslant d \quad e \lesssim f \quad g \asymp h"),
("Typesetting", "Spacing", r"a\!b\,c\:d\;e\quad f\qquad g"),
];
fn main() {
let arguments: Vec<String> = std::env::args().skip(1).collect();
let mode = arguments.first().map_or("", |value| value.as_str());
let loaded = font::load(std::fs::read("font/latinmodern-math.otf").unwrap()).unwrap();
if mode == "page" {
write_page(&loaded);
return;
}
let mut failed = 0;
for (field, name, source) in CORPUS {
match render_svg(source, &loaded, Mode::Display, Shape::Outline) {
Ok(_) => {
if mode == "all" {
println!("accepted [{field}] {name}");
}
}
Err(reason) => {
failed += 1;
println!("rejected [{field}] {name}");
println!(" {source}");
println!(" {reason}");
}
}
}
println!("\n{} formulas, {} rejected", CORPUS.len(), failed);
}
fn write_page(loaded: &Font) {
println!("<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><title>Famous formulas</title><style>");
println!("@font-face{{font-family:\"{}\";src:url(\"font/latinmodern-math.otf\")}}",
escape(font::family(loaded)));
println!("{}", suki::style(loaded));
println!("body{{margin:0;padding:48px 40px 96px;color:#1f1f1f;font-size:14px;");
println!("font-family:system-ui,sans-serif;line-height:1.9}}");
println!("h2{{font-size:15px;font-weight:500;margin:44px 0 18px}}");
println!(".item{{margin-bottom:26px}}");
println!(".name{{color:#767676;font-size:12px;margin-bottom:4px}}");
println!(".formula{{font-size:22px;overflow-x:auto}}");
println!(".note{{color:#767676;font-size:12px}}");
println!("</style></head><body><h1 style=\"font-size:19px;font-weight:500\">Famous formulas</h1>");
let mut current = "";
for (field, name, source) in CORPUS {
if *field != current {
println!("<h2>{}</h2>", escape(field));
current = field;
}
println!("<div class=\"item\"><div class=\"name\">{}</div>", escape(name));
match render_svg(source, loaded, Mode::Display, Shape::Outline) {
Ok(markup) => println!("<div class=\"formula\">{markup}</div></div>"),
Err(reason) => println!(
"<div class=\"note\">{} - {}</div></div>",
escape(source),
escape(&reason.to_string())
),
}
}
println!("</body></html>");
}