13

Which are the reasons not to always use \newcommand{\stuffa}{{STUFF}} instead of \newcommand{\stuffb}{STUFF}? In which cases would the former be a bad idea?

The reason I am considering doing this is to make the commands work e.g. in superscripts without extra curly braces, i.e., 2^\stuffa works while 2^\stuffb doesn't (2^{\stuffb} does). These commands will not only be used in superscripts, though, and I'm interested in a more general answer anyway. (If e.g. xparse provides a convenient solution to this problem, that would be interesting as well.)

I assume that this is a very basic question that has been answered many times, but I could not find an answer with a reasonable amount of research.

This question is related but does not answer my question.

1

1 Answer 1

26

The definitions are completely different, only add the extra group if you want an extra group

\newcommand\zzz{\bfseries}

would make \zzz an alias for \bfseries and \zzz this would make a bold this

    \newcommand\zzz{{\bfseries}}

would do nothing useful as the font change would be in a group that ends immediately.

\newcommand\recip{\frac{1}}

would define a reciprocal function \recip{4} would typeset 1/4.

\newcommand\recip{{\frac{1}}}

would do nothing useful as \recip{4} would be {\frac{1}}{4} and raise an error about a missing argument for \frac.

The fact that having the extra group in the definition makes x^\stuff seem to work is an accident of the implementation and should not be used. The documented LaTeX syntax is always to brace superscripts.

In general it will change the spacing,

\newcommand\zzz{+}

will make \zzz act like +

\newcommand\zzzb{{+}}

will make \zzzb act like {+} which isn't usually wanted, and gets no space compare

 $1  \zzz 2  \zzzb 3$
5
  • Thanks! This is very helpful and answers my question. I do have to say that I find it a pity that the use of the braces cannot be avoided - longer math equations are already difficult to parse, and putting extra braces everywhere doesn't improve readability (in my opinion).
    – Eike P.
    Jul 17, 2020 at 14:15
  • 1
    @jhin in the end it is much easier for computers and humans to parse x^{frac{1}{2}}y than x^\frac12y where knowing where the superscript ends requires knowing an awful lot of context. Jul 17, 2020 at 15:13
  • @campa my definitions do what I claim but I didn't write what I intended, sorry:-) fixed.. Jul 17, 2020 at 18:04
  • "The documented LaTeX syntax is always to brace superscripts": if I recall correctly, Knuth's own TeXbook says that braces are optional around single tokens. Jul 18, 2020 at 8:23
  • @GregMartin Knuth's texbook does not document LaTeX, Lamport's LaTeX book does. And as shown in the example in the question the "optional for single tokens" applies to macro arguments not to superscripts. The braces are not optional here, which is the reason for the question. Jul 18, 2020 at 9:11

You must log in to answer this question.

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