Skip to content

Commit 4cd7b2c

Browse files
authored
🧮 Updates to check, bar, bf, widetilde, aligned (#17)
1 parent d0c59c4 commit 4cd7b2c

File tree

3 files changed

+92
-9
lines changed

3 files changed

+92
-9
lines changed

‎src/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export function parseLatex(value: string) {
3030
ddot: { signature: 'm' },
3131
hat: { signature: 'm' },
3232
tilde: { signature: 'm' },
33+
check: { signature: 'm' },
34+
bar: { signature: 'm' },
3335
widehat: { signature: 'm' },
36+
widetilde: { signature: 'm' },
3437
overset: { signature: 'm m' },
3538
underset: { signature: 'm m' },
3639
overbrace: { signature: 'm' },
@@ -43,6 +46,8 @@ export function parseLatex(value: string) {
4346
overleftarrow: { signature: 'm' },
4447
stackrel: { signature: 'm m' },
4548
mathop: { signature: 'm' },
49+
bf: { signature: 'm' },
50+
textstyle: { signature: 'm' },
4651
// color: { signature: 'm m' }, // This doesn't work, changing it below manually
4752
},
4853
})
@@ -78,6 +83,9 @@ export function walkLatex(node: LatexNode) {
7883
next.args = [...(next.args?.slice(1) ?? []), ...args];
7984
skip += nodesRemoved;
8085
}
86+
if (next.type === 'macro' && next.content === 'textstyle') {
87+
next.content = 'text';
88+
}
8189
if (
8290
next.type === 'macro' &&
8391
(next.content === 'overbrace' || next.content === 'underbrace')
@@ -92,6 +100,20 @@ export function walkLatex(node: LatexNode) {
92100
next.args = [...(next.args ?? []), ...args[0].content[0].args];
93101
skip += nodesRemoved;
94102
}
103+
if (
104+
args[0].content.length === 1 &&
105+
args[0].content[0].type === 'string' &&
106+
((args[0].content[0].content === '^' && next.content === 'overbrace') ||
107+
(args[0].content[0].content === '_' && next.content === 'underbrace'))
108+
) {
109+
// need to get another argument, the _/^ was treated as a string, not an arg
110+
const { args: doubleArgs, nodesRemoved: doubleNodesRemoved } = gobbleArguments(
111+
array.slice(i + 1),
112+
'm m',
113+
);
114+
next.args = [...(next.args ?? []), doubleArgs[1]];
115+
skip += doubleNodesRemoved;
116+
}
95117
}
96118
if (next.type === 'macro' && next.content === 'middle' && array[i + 1]?.content === '|') {
97119
skip += 1;
@@ -133,7 +155,7 @@ class State implements IState {
133155
if (!this._value) return;
134156
if (lastChar.match(/^(["\s_^{(-])$/)) return;
135157
const lastTwoChar = this.value.slice(-2);
136-
if (!this._value || lastTwoChar === ')[') return; // e.g. `#text(fill: red)[V]
158+
if (!this._value || lastTwoChar === ')[' || lastTwoChar === '[$') return; // e.g. `#text(fill: red)[V]
137159
this._value += ' ';
138160
}
139161

‎src/macros.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function createBrackets(scale: string): (state: IState, node: LatexNode) => stri
3434
const b = (args?.[0].content?.[0] as LatexNode).content as string;
3535
const typstB = brackets[b];
3636
if (!typstB) throw new Error(`Undefined left bracket: ${b}`);
37-
return `#scale(x: ${scale}, y: ${scale})[$ ${typstB} $]`;
37+
return `#scale(x: ${scale}, y: ${scale})[$${typstB}$]`;
3838
};
3939
}
4040

@@ -73,8 +73,11 @@ export const typstMacros: Record<string, string | ((state: IState, node: LatexNo
7373
return 'root';
7474
},
7575
vec: 'arrow',
76+
check: 'caron',
77+
bar: 'macron',
7678
mathbf: 'bold',
7779
boldsymbol: 'bold',
80+
bf: 'bold',
7881
mathrm: 'upright',
7982
textrm: 'upright',
8083
rm: 'upright',
@@ -176,6 +179,7 @@ export const typstMacros: Record<string, string | ((state: IState, node: LatexNo
176179
cap: 'sect',
177180
cup: 'union',
178181
widehat: 'hat',
182+
widetilde: 'tilde',
179183
// Spaces
180184
',': 'thin',
181185
':': 'med',
@@ -238,7 +242,7 @@ export const typstMacros: Record<string, string | ((state: IState, node: LatexNo
238242
const [fill, children] = node.args ?? [];
239243
const color = (fill.content?.[0] as LatexNode)?.content as string;
240244
node.args = [];
241-
state.openFunction(`#text(fill: ${color})`, { openToken: '[$ ', closeToken: ' $]' });
245+
state.openFunction(`#text(fill: ${color})`, { openToken: '[$', closeToken: '$]' });
242246
state.writeChildren(children as LatexNode);
243247
state.closeFunction();
244248
return '';
@@ -262,4 +266,10 @@ export const typstEnvs: Record<string, (state: IState, node: LatexNode) => void>
262266
bmatrix: matrixEnv('['),
263267
Bmatrix: matrixEnv('{'),
264268
vmatrix: matrixEnv('|'),
269+
aligned(state, node) {
270+
state.writeChildren(node);
271+
},
272+
['aligned*'](state, node) {
273+
state.writeChildren(node);
274+
},
265275
};

‎tests/math.yml

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ cases:
9999
typst: |-
100100
overbrace([ frac(1, Delta t) frac(diff bold(theta)^(n + 1), diff bold(psi)^(n + 1)) -bold(D) "diag"(bold(G) bold(psi)^(n + 1)) frac(diff bold(k)_(A v), diff bold(psi)^(n + 1)) -bold(D) "diag"(bold(k)_(A v)(bold(psi)^(n + 1) comma bold(m))) bold(G) -bold(G)_z frac(diff bold(k)_(A v), diff bold(psi)^(n + 1)) ], bold(A)_0(bold(psi)^(n + 1))) frac(diff bold(psi)^(n + 1), diff bold(m)) \
101101
+ underbrace([ -frac(1, Delta t) frac(diff bold(theta)^n, diff bold(psi)^n) ], bold(A)_(-1)(bold(psi)^n)) frac(diff bold(psi)^n, diff bold(m)) = underbrace([ -bold(D) "diag"(bold(G) bold(psi)^(n + 1)) frac(diff bold(k)_(A v), diff bold(m)) -bold(G)_z frac(diff bold(k)_(A v), diff bold(m)) ], bold(B)(psi^(n + 1))) &
102+
- title: underbraces inside of a function
103+
description: For some reason the "_" here is treated as text, not a macro.
104+
tex: |
105+
\color{red}{\underbrace{
106+
\color{black}{\begin{bmatrix}
107+
0 & 1 \\
108+
-1 & 0
109+
\end{bmatrix}}
110+
}_{\textstyle A} }
111+
typst: '#text(fill: red)[$underbrace(#text(fill: black)[$mat(delim: "[", 0, 1; -1, 0)$], "A")$]'
102112
- title: leading underscore
103113
tex: _2
104114
typst: '""_2'
@@ -123,6 +133,9 @@ cases:
123133
- title: ddot
124134
tex: 'q, \dot{q}, \ddot{q}'
125135
typst: q, dot(q), dot.double(q)
136+
- title: dot
137+
tex: '\dot x^2'
138+
typst: 'dot(x)^2'
126139
- title: mat
127140
tex: 'x(t) = \left[ \begin{array}{c} q(t) & x \\ \dot{q}(t) & x \end{array}\right]'
128141
typst: 'x(t) = [ mat(delim: #none, q(t), x; dot(q)(t), x) ]'
@@ -235,7 +248,7 @@ cases:
235248
typst: 'V = {"\"ng\",\"mr\",\"sr\""}'
236249
- title: color
237250
tex: '\color{red}{V}'
238-
typst: '#text(fill: red)[$ V $]'
251+
typst: '#text(fill: red)[$V$]'
239252
- title: bmatrix
240253
tex: |
241254
\begin{bmatrix}
@@ -252,7 +265,7 @@ cases:
252265
=
253266
\color{red}{ad} - \color{blue}{bc}.
254267
# The `] ;` space is important for some reason!
255-
typst: 'mat(delim: "[", #text(fill: red)[$ a $], #text(fill: blue)[$ b $] ; #text(fill: blue)[$ c $], #text(fill: red)[$ d $]) = #text(fill: red)[$ a d $] -#text(fill: blue)[$ b c $].'
268+
typst: 'mat(delim: "[", #text(fill: red)[$a$], #text(fill: blue)[$b$] ; #text(fill: blue)[$c$], #text(fill: red)[$d$]) = #text(fill: red)[$a d$] -#text(fill: blue)[$b c$].'
256269
- title: bmatrix shapes
257270
tex: |
258271
-2
@@ -278,13 +291,51 @@ cases:
278291
typst: 'mat(delim: "[", p_0; p_1; p_2; dots.v; p_(T -1); p_T)'
279292
- title: Big
280293
tex: '\Big|'
281-
typst: '#scale(x: 180%, y: 180%)[$ bar.v $]'
294+
typst: '#scale(x: 180%, y: 180%)[$bar.v$]'
282295
- title: Big{
283296
tex: '\Big\{'
284-
typst: '#scale(x: 180%, y: 180%)[$ brace.l $]'
297+
typst: '#scale(x: 180%, y: 180%)[$brace.l$]'
285298
- title: big
286299
tex: '\big|'
287-
typst: '#scale(x: 120%, y: 120%)[$ bar.v $]'
300+
typst: '#scale(x: 120%, y: 120%)[$bar.v$]'
288301
- title: Bigl
289302
tex: '\Bigl| \frac{\lambda-\alpha(1-\lambda)}{1-\alpha(1-\lambda)} \Bigr| < 1'
290-
typst: '#scale(x: 180%, y: 180%)[$ bar.v $] frac(lambda -alpha(1 -lambda), 1 -alpha(1 -lambda)) #scale(x: 180%, y: 180%)[$ bar.v $] < 1'
303+
typst: '#scale(x: 180%, y: 180%)[$bar.v$] frac(lambda -alpha(1 -lambda), 1 -alpha(1 -lambda)) #scale(x: 180%, y: 180%)[$bar.v$] < 1'
304+
- title: big no space
305+
tex: '\theta = \tan^{-1} \Big( \frac{y}{x} \Big)'
306+
typst: 'theta = tan^(-1) #scale(x: 180%, y: 180%)[$paren.l$] frac(y, x) #scale(x: 180%, y: 180%)[$paren.r$]'
307+
- title: hat group
308+
tex: '{\hat x}'
309+
typst: 'hat(x)'
310+
- title: hat function
311+
tex: '\hat{x}'
312+
typst: 'hat(x)'
313+
- title: check
314+
tex: '\check p_{-1}, \check \pi_{-1}^*'
315+
typst: 'caron(p)_(-1), caron(pi)_(-1)^(*)'
316+
- title: bar
317+
tex: '\bar x'
318+
typst: 'macron(x)'
319+
- title: hat
320+
tex: '\hat x'
321+
typst: 'hat(x)'
322+
- title: accents
323+
tex: 'B_{-1}- \check B_{-1} = \frac{\widetilde R}{p_0} \left( \check m_0 - m_0 \right)'
324+
typst: 'B_(-1) -caron(B)_(-1) = frac(tilde(R), p_0) (caron(m)_0 -m_0)'
325+
- title: bf
326+
tex: '{\bf R}^2'
327+
typst: 'bold(R)^2'
328+
- title: bf
329+
tex: '{\bf R}^2'
330+
typst: 'bold(R)^2'
331+
- title: aligned
332+
tex: |
333+
\begin{aligned}
334+
35p_0 - 5p_1 - 5p_2 = 100 \\
335+
-5p_0 + 25p_1 - 10p_2 = 75 \\
336+
-5p_0 - 5p_1 + 15p_2 = 55
337+
\end{aligned}
338+
typst: |-
339+
35 p_0 -5 p_1 -5 p_2 = 100 \
340+
-5 p_0 + 25 p_1 -10 p_2 = 75 \
341+
-5 p_0 -5 p_1 + 15 p_2 = 55

0 commit comments

Comments
 (0)