From 61976a8ff44f421a527fdb88e2de259532822da4 Mon Sep 17 00:00:00 2001
From: Rowan Cockett
Date: Thu, 9 Jan 2025 22:43:53 -0700
Subject: [PATCH] =?UTF-8?q?=C2=BE=20Fixes=20for=20\over=20and=20\frac?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/index.ts | 14 ++++++++++++++
tests/math.yml | 6 ++++++
2 files changed, 20 insertions(+)
diff --git a/src/index.ts b/src/index.ts
index 35117b7..f0a1019 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -118,6 +118,20 @@ export function walkLatex(node: LatexNode) {
if (next.type === 'macro' && next.content === 'middle' && array[i + 1]?.content === '|') {
skip += 1;
}
+ if (
+ next.type === 'group' &&
+ (next.content as LatexNode[])?.find?.((n) => n.type === 'macro' && n.content === 'over')
+ ) {
+ // Change {a \over b} to \frac{a}{b}
+ const children = next.content as LatexNode[];
+ next.type = 'macro';
+ next.content = 'frac';
+ const index = children.findIndex((n) => n.type === 'macro' && n.content === 'over');
+ next.args = [
+ { type: 'argument', content: children.slice(0, index) },
+ { type: 'argument', content: children.slice(index + 1) },
+ ];
+ }
list.push(next);
return list;
}, [] as LatexNode[]);
diff --git a/tests/math.yml b/tests/math.yml
index 2d6f6db..44b6fd8 100644
--- a/tests/math.yml
+++ b/tests/math.yml
@@ -344,3 +344,9 @@ cases:
\mathbf{u} \wedge \mathbf{v} \equiv
\mathbf{u} \otimes \mathbf{v} - \mathbf{v} \otimes \mathbf{u}\,.
typst: 'bold(u) and bold(v) equiv bold(u) times.circle bold(v) -bold(v) times.circle bold(u) thin.'
+ - title: frac
+ tex: '\vec{e}_i = \frac{\partial\mathbf{r}}{\partial x^i} \equiv \frac{\partial}{\partial x^i}\,,'
+ typst: 'arrow(e)_i = frac(diff bold(r), diff x^i) equiv frac(diff, diff x^i) thin,'
+ - title: over
+ tex: '\vec{e}_i = {\partial\mathbf{r}\over\partial x^i} \equiv {\partial\over\partial x^i}\,,'
+ typst: 'arrow(e)_i = frac(diff bold(r), diff x^i) equiv frac(diff, diff x^i) thin,'