2

For e.g. take

$G^\times$

here we do not need to write the curly parenthesis, as in

$G^{\times}$

What is it that one has to add to a newly declared command, e.g.

\newcommand{\foo}{\ensuremath \times\ast 2}

so that

$G^\foo$

yields the same as

$G^{\foo}$

thx

12
  • What about \newcommand{\foo}{\ensuremath {\times\ast 2}}
    – Sigur
    Jul 30, 2013 at 15:17
  • 1
    \newcommand*{\foo}{{\times\ast 2}} works like a charm (\ensuremath is not needed and even frowned upon). Jul 30, 2013 at 15:19
  • 1
    if you're always going to use \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. Jul 30, 2013 at 15:22
  • 1
    @Sigur It is not relevant for this question (and even more so as \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*? Jul 30, 2013 at 15:24
  • 1
    @arolle maybe worth reading: How bad for TeX is omitting braces {}, even if the result is the same?
    – cgnieder
    Jul 30, 2013 at 15:49

1 Answer 1

4

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.)

1
  • 2
    best not to use \ensuremath if you do use it it needs to be {{\ensuremath{\times\ast 2}}} otherwise the ensuremath just takes \times` as its argument. Jul 30, 2013 at 20:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .