# A \(re)newcommand that does not care if a command is already defined


As a rationalisation for the question, below are two situations in which I would find this command useful. Of course, the change is very minor, but it adds up over time, I think.

-
please be very careful! an author redefined \i to be a math function, failing to notice that one of the items in the bibliography was by a russian author whose transliterated name required \i (for use with a breve). it took a ridiculously long time for the production team to figure out (1) why there was a message that math mode was required, and (2) change all the other instances of \i to something else (and hoping that no mistakes were made in the process). –  barbara beeton Aug 18 '13 at 14:08


\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 }

-
To be equal to renewcommand & friends, it should be \long\def\foo#1 –  Marco Daniel Aug 18 '13 at 13:07
@MarcoDaniel: The star forms of \renewcommand and friends use \def without \long. –  Heiko Oberdiek Aug 18 '13 at 13:28
@egreg: Done, a bit more elaborate because of the star forms. –  Heiko Oberdiek Aug 18 '13 at 13:44

You can put

\newcommand{\foo}[1]{to be renewed}


\renewcommmand{\foo}[1]{whatever #1}

If \foo happens to be defined when you start out, you'll find that out and can choose another name, or renew in your preamble.