Skip to content

Loops in macros #3943

Answered by edemaine
fmorroni asked this question in Q&A
Apr 4, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

Yes, it's possible, but I don't think it's documented currently. See src/macros.js for a bunch of examples. Dealing with numbers is unfortunately a bit painful, because each character becomes its own token, and the tokens are in reverse order. This should do what you want:

katex.renderToString('\\tabs{10}', {
  macros: {
    '\\tabs': (context) => {
      let [arg] = context.consumeArgs(1);
      arg = arg.reverse().map(token => token.text).join('');
      arg = Number(arg);
      return '\\quad'.repeat(arg)
    }
  }
})

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@fmorroni
Comment options

Answer selected by fmorroni
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants