Skip to content

Commit

Permalink
smart floor and ceil
Browse files Browse the repository at this point in the history
  • Loading branch information
qwinsi committed Aug 17, 2024
1 parent 6bca7a5 commit 8669574
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 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.0.15",
"version": "0.0.16",
"description": "JavaScript library for converting TeX code to Typst",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
16 changes: 16 additions & 0 deletions src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,22 @@ export class TypstWriter {

public finalize(): string {
this.flushQueue();
const smartFloorPass = function (input: string): string {
// Use regex to replace all "⌊ xxx ⌋" with "floor(xxx)"
let res = input.replace(/⌊\s*(.*?)\s*⌋/g, "floor($1)");
// Typst disallow "floor()" with empty argument, so add am empty string inside if it's empty.
res = res.replace(/floor\(\)/g, 'floor("")');
return res;
};
const smartCeilPass = function (input: string): string {
// Use regex to replace all "⌈ xxx ⌉" with "ceil(xxx)"
let res = input.replace(/⌈\s*(.*?)\s*⌉/g, "ceil($1)");
// Typst disallow "ceil()" with empty argument, so add an empty string inside if it's empty.
res = res.replace(/ceil\(\)/g, 'ceil("")');
return res;
}
this.buffer = smartFloorPass(this.buffer);
this.buffer = smartCeilPass(this.buffer);
return this.buffer;
}
}
Expand Down
12 changes: 9 additions & 3 deletions test/symbol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ cases:
- title: combinatorial number
tex: \binom{n}{k}
typst: binom(n, k)
- title: left and right floor,ceil
tex: \lfloor \rfloor \lceil \rceil
typst: ⌊ ⌋ ⌈ ⌉
- title: unpaired left and right floor, ceil 1
tex: \lfloor \rceil
typst: ⌊ ⌉
- title: unpaired left and right floor,ceil 2
tex: \rfloor \lceil
typst: ⌋ ⌈
- title: paired left and right floor,ceil
tex: \lfloor x \rfloor \lceil x \rceil
typst: floor(x) ceil(x)
- title: trigonometric functions
tex: \sin \cos \tan \cot \sec \csc
typst: sin cos tan cot sec csc
Expand Down

0 comments on commit 8669574

Please sign in to comment.