From 4ac14df5bf2e343da785f134e6bcb0460d7ef9ce Mon Sep 17 00:00:00 2001 From: qwinsi <70425035+qwinsi@users.noreply.github.com> Date: Sun, 8 Sep 2024 15:22:59 +0800 Subject: [PATCH] improve logic for \\operatorname --- src/parser.ts | 2 +- src/writer.ts | 11 +++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index 78cacaa..500d98b 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -311,7 +311,7 @@ export function tokenize(latex: string): Token[] { tokens.push(token); - if (token.type === TokenType.COMMAND && ['\\text', '\\begin', '\\end'].includes(token.value)) { + if (token.type === TokenType.COMMAND && ['\\text', '\\operatorname', '\\begin', '\\end'].includes(token.value)) { if (pos >= latex.length || latex[pos] !== '{') { throw new LatexParserError(`No content for ${token.value} command`); } diff --git a/src/writer.ts b/src/writer.ts index 7789568..f88138d 100644 --- a/src/writer.ts +++ b/src/writer.ts @@ -197,15 +197,10 @@ export class TypstWriter { // Fall through } else if (node.content === '\\operatorname') { let body = node.args!; - if (body.length === 1 && body[0].type == 'ordgroup') { - body = body[0].args!; + if (body.length !== 1 || body[0].type !== 'text') { + throw new TypstWriterError(`Expecting body of \\operatorname to be text but got`, node); } - const text = body.reduce((buff, n) => { - // Hope convertToken() will not throw an error - // If it does, the input is bad. - buff += convertToken(n.content); - return buff; - }, "" as string); + const text = body[0].content; if (this.preferTypstIntrinsic && TYPST_INTRINSIC_SYMBOLS.includes(text)) { // e.g. we prefer just sech over op("sech")