@@ -146,7 +146,9 @@ defineMacro("\\char", function(context) {
146146// \newcommand{\macro}[args]{definition}
147147// \renewcommand{\macro}[args]{definition}
148148// 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+ ) => {
150152 let arg = context . consumeArg ( ) . tokens ;
151153 if ( arg . length !== 1 ) {
152154 throw new ParseError (
@@ -181,16 +183,21 @@ const newcommand = (context, existsOK: boolean, nonexistsOK: boolean) => {
181183 arg = context . consumeArg ( ) . tokens ;
182184 }
183185
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+ }
189193 return '' ;
190194} ;
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 ) ) ;
194201
195202// terminal (console) tools
196203defineMacro ( "\\message" , ( context ) => {
0 commit comments