From 86695743578f5a3736141c9d45e423586dc81128 Mon Sep 17 00:00:00 2001 From: qwinsi <70425035+qwinsi@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:24:53 +0800 Subject: [PATCH] smart floor and ceil --- package.json | 2 +- src/writer.ts | 16 ++++++++++++++++ test/symbol.yml | 12 +++++++++--- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index c71e95c..bf44c3e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/writer.ts b/src/writer.ts index 3c1dc01..7a094ef 100644 --- a/src/writer.ts +++ b/src/writer.ts @@ -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; } } diff --git a/test/symbol.yml b/test/symbol.yml index 3e971a9..3965483 100644 --- a/test/symbol.yml +++ b/test/symbol.yml @@ -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