28

I've seen in every introductory material on LaTeX and in a lot of code of other people (event experts) that the \newcommand command is used by wrapping the first argument in braces, even if it seems unnecessary to me.

The braces are consistent with the usual way arguments are passed to LaTeX commands (since usually the braces are used even if the argument is made of a single token, for consistency and clarity), but in this single case they appear very ugly to me and in my opinion they lower readability.

But these are opinions, while the question is technical: is there any technical reason why one should write this:

\newcommand{\mycmd}[1]{Hello #1}

instead of this:

\newcommand\mycmd[1]{Hello #1}
36

The official latex syntax, as described by the LaTeX book only ever uses the braced form, and similarly always uses x^{2} rather than x^2.

However the unbraced form works fine and for arguments that are necessarily a single token such as the first argument of \newcommand. That is effectively also supported syntax and you will find many examples from the LaTeX Project team using that style.

Of course dropping braces technically also works even when the argument isn't necessarily a single token, people go \frac12 instead of \frac{1}{2} or even, x^\frac12 instead of x^{\frac{1}{2}} but really, they shouldn't....

  • 7
    After reading this \frac12 my eyes hurt really bad! :( – Andrea Lazzarotto Nov 24 '16 at 17:32
  • 3
    \frac12 is easier for my eyes than \frac{1}{2}. I guess I got used to it… – liori Nov 25 '16 at 1:39
  • 2
    @liori try to get unused to it:-) People who write \frac12 will write \frac110 see how often it comes up here that x^16 does not work. If x^2 had not been suggested they would not have tried x^16. It is much simpler for people (and tex to xyz convertors) to say that mandatory arguments are braced. – David Carlisle Nov 25 '16 at 7:34
11

There is no difference between those two commands. It is a matter of taste, I guess. I prefer the braced version. It looks more consistent to me, it is offered to me by the auto-completion of my IDE, and the starred version \newcommand*{\mycmd}[1]{Hello #1} which I would use for this very case, appears easier to read to me. I used "to me" three times in the last sentence... makes it clear that you might choose whatever you like.

I have done some tests on both commands and I am not able to spot any difference. As the rules for command-naming are quite strict, there should be no possibility to create any issue by leaving the braces apart.

6

When (La)TeX is "grabbing" an undelimited argument, leading space tokens will be discarded and the outermost level of braces [a pair consisting of a category-code-1-character-token and a matching category-code-2-character-token] will be removed if present. In case an undelimited argument is to consist of more than one token, these tokens must be nested into an additional level of braces which in turn will be considered the outermost level of braces that will be removed.

E.g., the following code-sequences yield the same result (\processtwo is defined in a way where the control-sequence is delimited by a dot while the arguments are undelimited—this way a space in front of the first argument will not be skipped by TeX' reading apparatus but will be tokenized as space token):

\def\processtwo.#1#2{%
  \def\tempa{FirstArg:|#1| SecondArg:|#2|}%
  \par{\tt\meaning\tempa}%
}
\processtwo.AB
\processtwo. AB
\processtwo.A B
\processtwo. A B
\processtwo.{A}B
\processtwo. {A}B
\processtwo.{A} B
\processtwo. {A} B
\processtwo.A{B}
\processtwo. A{B}
\processtwo.A {B}
\processtwo. A {B}
\processtwo.{A}{B}
\processtwo. {A}{B}
\processtwo.{A} {B}
\processtwo. {A} {B}
\bye

When (La)TeX is "grabbing" a delimited argument, leading space tokens will not be removed. In case the entire argument is wrapped into braces [into a pair consisting of a category-code-1-character-token and a matching category-code-2-character-token], this outermost level of braces will be removed.

E.g., the following code-sequences do not all lead to the same result (\processtwo is defined in a way where the control-sequence and the arguments are delimited by dots. By delimiting the control sequence also it is ensured that a space in front of the first argument will not be skipped by TeX' reading apparatus but will be tokenized as space token):

\def\processtwo.#1.#2.{%
  \def\tempa{FirstArg:|#1| SecondArg:|#2|}%
  \par{\tt\meaning\tempa}%
}
\processtwo.A.B.
\processtwo.{A}.{B}.
\processtwo.{A}.B.
\processtwo.A.{B}.
\medskip
\processtwo.A. B.
\processtwo.{A}. B.
\medskip
\processtwo. A.B.
\processtwo. A.{B}.
\medskip
\processtwo. {A}.B.
\processtwo. {A}.{B}.
\medskip
\processtwo.{A}. {B}.
\processtwo.A. {B}.
\medskip
\processtwo. A. B.
\medskip
\processtwo. {A}. B.
\medskip
\processtwo. A. {B}.
\medskip
\processtwo. {A}. {B}.
\bye

\newcommand itself is a macro. The first argument (after an optional * ) denotes the control sequence token that is to be defined. That argument by LaTeX is taken for an undelimited argument consisting of a single token (a single control sequence token). Therefore it makes no difference whether that argument/token is nested in braces or not.

But when it comes to processing brace delimited arguments (→ parameter text with #{-notation), braces do matter. Often from the syntax described in the user manual it is not obvious whether an argument of a user level macro is processed as an undelimited argument or as brace delimited argument. E.g., the color-package does a lot of processing by means of brace delimited arguments. In order to know about this subtlety, you need to look at the .sty-file or at the commented sources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.