Is it allowed to put spaces after macro parameter?
When you do
\def\foo#1#2{(#1,#2)}
then #1
and #2
are undelimited arguments. An undelimited argument is either a single token (except {
or }
) or a braced group {...}
. TeX ignores spaces when looking at undelimited arguments, and we get
\def\foo#1#2{(#1,#2)}
\foo a b \foo ab \foo {abc} {def}
On the other hand, when you write
\def\foo#1#2 {(#1,#2)}
then #1
is a normal, undelimited argument, but #2
is now an argument delimited by an explicit space token. With this definition you'll find thus
\def\foo#1#2 {(#1,#2)}
\foo ab \foo a b
In both cases the space after the control sequence is ignored (as usual) and a
is grabbed as first (undelimited) argument. Now TeX starts looking at everything which comes before a space. In the first case it finds b
followed by a space, so b
is your second argumeng. But in the second case the first token following a
is a space, so the second argument is empty.
Going specifically to your example
\def\SpinOrb#1#2 { \chi_#1(x_#2) }
\SpinOrb i 2
by the TeX rules I just illustrated the i
is grabbed as first argument, and the second argument is empty, so the code expands effectively to
\SpinOrb i 2
→ \chi_i(x_) 2
and the parenthesis lands as subscript. This is a consequence of omitting braces in the expansion text: even with the correct definition (without space after #2
) the use of braces in the input would nevertheless lead to unexpected results, since
\SpinOrb{i}{10}
→ \chi_i(x_10)
and you would find the 1
as subscript and a trailing 0
...
Moral: use \newcommand
instead of \def
, and don't be too lazy with braces. The way to go is
\newcommand*{\SpinOrb}[2]{ \chi_{#1}(x_{#2}) }
\SpinOrb{i}{2}
and you'll never have problems. Note also the braces around the arguments in the expansion text; without them you might get unpleasant surprises. Use delimited arguments only if you really want to, and if you really know what you are doing.
In TeX, spaces in most places are regular tokens so just adding random spaces will often change the output. In this case, an argument specifier like #2
which is not followed by a {
or another argument specifier specifies a delimited argument which captures everything until the first occurrence of the following tokens (in this case a single space). So when parsing \SpinOrb i 2
, TeX first read the first argument i
and then is looking at " 2
". Now it reads everything until the first space as second argument. Since the first character is already a space this makes the second argument empty.