An example is a bit artificial, but the point is that I do not understand why
{}
influence the outcome of \newcommand
in the way described below.
Consider the following definition:
\newcommand\printit[1]{#1}
whose outcome is obvious. However, a small alteration:
\newcommand{\printit[1]}{#1}
and e.g. \printit{2}
yields:
I have no idea why. Could you please shed some light on what is going on here?
Best Answer
If LaTeX was being written now,
would give an error message that
\printit[1]
was not a single token.The correct syntax is
or
LaTeX does nothing special to allow both forms, it is just on the general TeX macro syntax rules that braces can be omitted if a macro argument is a single token.
If you pass two (or in this case, four) tokens to
\newcommand
then anything that happens is essentially just accidental behaviour, as the system could not really afford the space or time required to catch "unlikely" errors.As to what happens,
produces the terminal log
The command defines
\printit
to have an optional first argument, although the default value of 0 being supplied was intended as the default value for\newcommand
itself. it is what makes\newcommand\foo
the same as\newcommand\foo[0]
and define a command with no arguments.The inner part of the command handling the optional argument is
\\printit
which just drops the square brackets.So
is
which is
which typesets as 02