Sunday, 18 August 2013

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

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

Does there exists a version of \newcommand, something like
\extranewcommand{\foo}{bar} which does defines \foo to stand for bar
regardless of whether \foo has been previously defined? Of course, if \foo
is undefined, one can use \newcommand{\foo}{bar}, and if it is defined,
one can use \renewcommand{\foo}{bar}. However, in either case, one has to
know which version to use: if one chooses wrong, the document won't
compile. What I am interested in is if there exists a similar command (or
perhaps a combination of commands, etc.) which will work in either
situation.
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.
One doesn't always know what commands are already defined. Sometimes, it
happens that a newly defined command happens to also be something defined
in some standard package. For instance, one wants to use \operatorname{Im}
for the image of a linear operator, so it is convenient to declare
\newcommand{\Im}{\operatorname{Im}}. However, \Im is already a fancy
symbol for the imaginary part, so a renewcommand is needed instead. I
never use the mentioned imaginary part symbol, so it would be nice if I
could just forget that it exists (but I can't). (Of course, this issue can
be solved by compiling, noticing the error and adding "re" in front of
"newcommand"; but, there are a lot of other such clashes that happen.)
I use a lot of commands used only "locally" (I'm not sure if it's a good
coding practice, but I find it useful). For example, if I have a set S
which occurs in many places in the text, I would often declare
\newcommand{\Set}{S} or even \newcommand{\S}{S}, so that I can easily
change the symbol S to another one if I feel like it. This potentially
leads to renewing commands quite frequently, which is not that much of a
problem. However, if a block of text with a \newcommand moves, then also a
prefix re has to move. (Again, it is not much of a problem, but I find it
a nuisance).

No comments:

Post a Comment