you can "protect" the string you intend to use in sub- or superscripts by doubling the outer braces; only the outermost pair of braces gets stripped off when a definition is "internalized". thus
\newcommand{\foo}{{\ensuremath \times\ast 2}}
will have the desired effect.
however:
you really don't need \ensuremath
if you're always going to use this in a sub/superscript.
it's not as "informative" when viewed in the input, since it's not obvious that \foo
is a compoind object. omitting the braces at the point of use gets one out of the habit of using braces, which can get one into trouble with previously existing definitions that are "unprotected" compounds, such as \neq
, defined as \def\neq{\not=}
in fontmath.lts
(inherited from plain.tex
).
edit: a comment from the op states that this doesn't work "when setting <0
or alike as a superscript". that's true, and there's no way around it, except to make a definition, e.g. \newcommand{\lszero}{{<0}}
, and use that. (that is what the original question asked for.)
\newcommand{\foo}{\ensuremath {\times\ast 2}}
\newcommand*{\foo}{{\times\ast 2}}
works like a charm (\ensuremath
is not needed and even frowned upon).\foo
as a superscript you really don't want\ensuremath
. but that wasn't your question. if you just add another outer pair of braces --\newcommand{\foo}{{\ensuremath \times\ast 2}}
it should withstand the indignity of being used without braces as a superscript.\foo
doesn’t take any arguments). The macro is defined via\def
and not\long\def
, see What's the difference between \newcommand and \newcommand*?