Skip to content

Commit

Permalink
implement option.customTexMacros using tokenizer
Browse files Browse the repository at this point in the history
  • Loading branch information
qwinsi committed Aug 23, 2024
1 parent cb6f28c commit d00f8e1
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,15 @@ export class LatexParser {

export function parseTex(tex: string, customTexMacros: {[key: string]: string}): TexNode {
const parser = new LatexParser();
for (const [macro, replacement] of Object.entries(customTexMacros)) {
tex = tex.replaceAll(macro, replacement);
const original_tokens = tokenize(tex);
let processed_tokens: Token[] = [];
for (const token of original_tokens) {
if (token.type === 'command' && customTexMacros[token.value]) {
const expanded_tokens = tokenize(customTexMacros[token.value]);
processed_tokens = processed_tokens.concat(expanded_tokens);
} else {
processed_tokens.push(token);
}
}
const tokens = tokenize(tex);
return parser.parse(tokens) as TexNode;
return parser.parse(processed_tokens);
}

0 comments on commit d00f8e1

Please sign in to comment.