A \(re)newcommand that does not care if a command is already defined
There is \providecommand
that defines a macro if it is not yet defined. Together with \renewcommand
you have your "\extranewcommand
":
\providecommand{\foo}{}
\renewcommand{\foo}[1]{bar: #1}
Or you can switch to the plain TeX primitives (other syntax!), e.g.:
\long\def\foo#1{bar: #1}
(\renewcommand
and friends without star use \long\def
and if the star form is given, \def
without \long
is used. Also the parameters are specified differently.)
\declarecommand
A definition of the "requested" \declarecommand
that calls \providecommand
and \renewcommand
together:
\documentclass{article}
\makeatletter
\newcommand*{\declarecommand}{%
\@star@or@long\declare@command
}
\newcommand*{\declare@command}[1]{%
\provide@command{#1}{}%
% \let#1\@empty % would be more efficient, but without error checking
\renew@command{#1}%
}
\makeatother
\begin{document}
\declarecommand*{\foo}{\typeout{foo 1: \meaning\foo}}
\foo
\declarecommand{\foo}{\typeout{foo 2: \meaning\foo}}
\foo
\declarecommand*{\foo}[1]{\typeout{foo #1: \meaning\foo}}
\foo{3}
\end{document}
Result:
foo 1: macro:->\typeout {foo 1: \meaning \foo }
foo 2: \long macro:->\typeout {foo 2: \meaning \foo }
foo 3: macro:#1->\typeout {foo #1: \meaning \foo }
It's not as crafty as Heiko's answer but another option would be
\ifdefined\foo
\renewcommand{\foo}[1]{bar: #1}
\else
\newcommand{\foo}[1]{bar: #1}
\fi
Note that \ifdefined
is part of the e-TeX extension and as such part of any modern LaTeX compiler. No packages required.