Skip to content

Commit

Permalink
feat: Allow changing registers using \renewcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
arnog committed Nov 15, 2023
1 parent bcc67d9 commit 17631d5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

- Simplified the syntax to modify registers. Use `mf.registers.arraystretch = 1.5`
instead of mf.registers = {...mf.registers, arraystretch: 1.5}`

- Allow changing registers using `\renewcommand`, for example
`\renewcommand{\arraystretch}{1.5}`

## 0.96.0 (2023-11-14)

Expand Down
49 changes: 31 additions & 18 deletions src/core/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1515,24 +1515,37 @@ export class Parser {
return [new PlaceholderAtom({ mode: this.parseMode, style: this.style })];
}

// if (command === '\\char') {
// const initialIndex = this.index;
// let codepoint = this.scanNumber(true)?.number ?? NaN;
// if (!Number.isFinite(codepoint) || codepoint < 0 || codepoint > 0x10ffff)
// codepoint = 0x2753; // BLACK QUESTION MARK

// return [
// new Atom({
// type: this.parseMode === 'math' ? 'mord' : 'text',
// command: '\\char',
// mode: this.parseMode,
// value: String.fromCodePoint(codepoint),
// verbatimLatex:
// '\\char' +
// tokensToString(this.tokens.slice(initialIndex, this.index)),
// }),
// ];
// }
if (
command === '\\renewcommand' ||
command === '\\newcommand' ||
command === '\\providecommand' ||
command === '\\def'
) {
// \\renewcommand: error if command is not already defined
// \\newcommand: error if command is already defined
// \\providecommand: define command only if it is not already defined
// \\def: define command, silently overwriting any existing definition
const index = this.index;
// Get the command name
const cmd = this.scanLiteralGroup() || this.next();
if (!cmd) return null;

// Define (or redefine) a command (or register)
if (this.context.registers[cmd.substring(1)]) {
const value = this.scanArgument('string');
if (value !== null) this.context.registers[cmd.substring(1)] = value;

const verbatimLatex = joinLatex([
command,
tokensToString(this.tokens.slice(index, this.index)),
]);

return [new Atom({ type: 'text', value: '', verbatimLatex })];
}

// Could be a macro definition... @todo
// \newcommand{\cmd}[nargs][optargdefault]{defn}
}

// Is this a macro?
let result = this.scanMacro(command);
Expand Down

0 comments on commit 17631d5

Please sign in to comment.