Skip to content

Commit

Permalink
add support for Typst round function
Browse files Browse the repository at this point in the history
  • Loading branch information
qwinsi committed Dec 8, 2024
1 parent 7d4af03 commit 8a9733d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tex2typst",
"version": "0.2.9",
"version": "0.2.10",
"description": "JavaScript library for converting TeX code to Typst",
"type": "module",
"main": "dist/index.js",
Expand Down
20 changes: 17 additions & 3 deletions src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,17 @@ export class TypstWriter {
res = res.replace(/ceil\(\)/g, 'ceil("")');
return res;
}
this.buffer = smartFloorPass(this.buffer);
this.buffer = smartCeilPass(this.buffer);
const smartRoundPass = function (input: string): string {
// Use regex to replace all "⌊ xxx ⌉" with "round(xxx)"
let res = input.replace(/⌊\s*(.*?)\s*⌉/g, "round($1)");
// Typst disallow "round()" with empty argument, so add an empty string inside if it's empty.
res = res.replace(/round\(\)/g, 'round("")');
return res;
}
const all_passes = [smartFloorPass, smartCeilPass, smartRoundPass];
for (const pass of all_passes) {
this.buffer = pass(this.buffer);
}
return this.buffer;
}
}
Expand Down Expand Up @@ -352,7 +361,12 @@ export function convertTree(node: TexNode): TypstNode {
content: '',
args: node.args!.map(convertTree),
};
if (["[]", "()", "\\{\\}", "\\lfloor\\rfloor", "\\lceil\\rceil"].includes(left.content + right.content)) {
if ([
"[]", "()", "\\{\\}",
"\\lfloor\\rfloor",
"\\lceil\\rceil",
"\\lfloor\\rceil",
].includes(left.content + right.content)) {
return group;
}
return {
Expand Down
8 changes: 4 additions & 4 deletions test/symbol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ cases:
- title: combinatorial number
tex: \binom{n}{k}
typst: binom(n, k)
- title: unpaired left and right floor, ceil 1
tex: \lfloor \rceil \left \lfloor \frac{1}{2} \right \rceil
typst: ⌊ ⌉ lr(⌊ frac(1, 2) ⌉)
- title: unpaired left and right floor,ceil 2
- title: unpaired left and right floor,ceil
tex: \rfloor \lceil \left\lceil \frac{1}{2} \right \rfloor
typst: ⌋ ⌈ lr(⌈ frac(1, 2) ⌋)
- title: paired left and right floor,ceil
tex: \lfloor x \rfloor \lceil x \rceil \left\lceil \frac{1}{2} \right \rceil
typst: floor(x) ceil(x) ceil(frac(1, 2))
- title: paired lfloor and rceil
tex: \lfloor \rceil \lfloor x \rceil \left \lfloor \frac{1}{2} \right \rceil
typst: round("") round(x) round(frac(1, 2))
- title: trigonometric functions
tex: \sin \cos \tan \cot \sec \csc
typst: sin cos tan cot sec csc
Expand Down

0 comments on commit 8a9733d

Please sign in to comment.