+19
-14
lines changedFilter options
+19
-14
lines changed Original file line number Diff line number Diff line change
@@ -146,7 +146,9 @@ defineMacro("\\char", function(context) {
146
146
// \newcommand{\macro}[args]{definition}
147
147
// \renewcommand{\macro}[args]{definition}
148
148
// TODO: Optional arguments: \newcommand{\macro}[args][default]{definition}
149
-
const newcommand = (context, existsOK: boolean, nonexistsOK: boolean) => {
149
+
const newcommand = (
150
+
context, existsOK: boolean, nonexistsOK: boolean, skipIfExists: boolean
151
+
) => {
150
152
let arg = context.consumeArg().tokens;
151
153
if (arg.length !== 1) {
152
154
throw new ParseError(
@@ -181,16 +183,21 @@ const newcommand = (context, existsOK: boolean, nonexistsOK: boolean) => {
181
183
arg = context.consumeArg().tokens;
182
184
}
183
185
184
-
// Final arg is the expansion of the macro
185
-
context.macros.set(name, {
186
-
tokens: arg,
187
-
numArgs,
188
-
});
186
+
if (!(exists && skipIfExists)) {
187
+
// Final arg is the expansion of the macro
188
+
context.macros.set(name, {
189
+
tokens: arg,
190
+
numArgs,
191
+
});
192
+
}
189
193
return '';
190
194
};
191
-
defineMacro("\\newcommand", (context) => newcommand(context, false, true));
192
-
defineMacro("\\renewcommand", (context) => newcommand(context, true, false));
193
-
defineMacro("\\providecommand", (context) => newcommand(context, true, true));
195
+
defineMacro("\\newcommand",
196
+
(context) => newcommand(context, false, true, false));
197
+
defineMacro("\\renewcommand",
198
+
(context) => newcommand(context, true, false, false));
199
+
defineMacro("\\providecommand",
200
+
(context) => newcommand(context, true, true, true));
194
201
195
202
// terminal (console) tools
196
203
defineMacro("\\message", (context) => {
Original file line number Diff line number Diff line change
@@ -3659,17 +3659,15 @@ describe("A macro expander", function() {
3659
3659
expect`\newcommand{\foo}{1}\foo\renewcommand{\foo}{2}\foo`.toParseLike`12`;
3660
3660
});
3661
3661
3662
-
it("\\providecommand (re)defines macros", () => {
3662
+
it("\\providecommand defines but does not redefine macros", () => {
3663
3663
expect`\providecommand\foo{x^2}\foo+\foo`.toParseLike`x^2+x^2`;
3664
3664
expect`\providecommand{\foo}{x^2}\foo+\foo`.toParseLike`x^2+x^2`;
3665
-
expect`\providecommand\bar{x^2}\bar+\bar`.toParseLike`x^2+x^2`;
3666
-
expect`\providecommand{\bar}{x^2}\bar+\bar`.toParseLike`x^2+x^2`;
3667
3665
expect`\newcommand{\foo}{1}\foo\providecommand{\foo}{2}\foo`
3668
-
.toParseLike`12`;
3666
+
.toParseLike`11`;
3669
3667
expect`\providecommand{\foo}{1}\foo\renewcommand{\foo}{2}\foo`
3670
3668
.toParseLike`12`;
3671
3669
expect`\providecommand{\foo}{1}\foo\providecommand{\foo}{2}\foo`
3672
-
.toParseLike`12`;
3670
+
.toParseLike`11`;
3673
3671
});
3674
3672
3675
3673
it("\\newcommand is local", () => {
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4