What is the \providecommand
equivalent of \DeclareMathOperator
?
I.e. as \providecommand
is to \newcommand
a method for defining new commands if and only if they have not already been defined?
Actually, as pointed out by @egreg in the comments, \providecommand
can be dangerous since it might adversely affect the behaviour of packages that define it. What I would really like is something that combines the ability of \providecommand
to quietly ensure a command exists with the functionality of \CheckCommand
to make sure that the provided definition is consistent with previous definitions.
Best Answer
I'd be very cautions in using
\providecommand
or variations thereof. This simple example may show why: if you sayand then start using
\box
in your document, very puzzling error messages will be raised.One might think to define a
\ProvideMathOperator
that also checks for the definition of the first argument, if already defined somehow, and warns if the control sequence we are trying to define is an operator (with the same definition) or not.However this is quite hard: the supplied control sequence might have any definition, with mandatory or optional arguments, be robust or even be unexpandable.
One could deal with all these variations, in principle, but it's rather hard (I assure you it really is). The number of predefined math operators is not very big, after all, so the "use
\DeclareMathOperator
and try with another name if LaTeX complains" way seems to be the less complicated one.Here's a rather simplistic procedure:
I just try and see whether the proposed control sequence exists or not; in the latter case
\DeclareMathOperator
is safe. In the former case, I look whether the "command with trailing space in the name" exists; then I compare the meaning of the command (using the one with the trailing space, if existent) to whatamsmath
would have defined it if it's an operator.The output of the document is
When the warning is issued, no definition is performed.
\ProvideMathOperator
accepts the*
-variant just like\DeclareMathOperator
.