@@ -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 ) => {
0 commit comments