[comp.text.tex] Strange result in macros

elmar@expbinfo.uni-paderborn.de (Elmar Schalueck) (01/24/91)

%  I discovered a beautiful difference in macros depending
%  on the position of the left braces after the macroname.
%  Is this a desired effect and how can I make use of it to apply
%  macros to whole words that are not surrounded by braces?
%
%  Greetings and have some fun with the example below
%
%  Elmar Schal\"{u}ck           elmar@uni-paderborn.de
%
%
\documentstyle{article}
\begin{document}
\long\def\foo#1
{Hurrah #1 Hurrah}
\long\def\gnat#1{Yippee #1 Yippee}
Now I test \foo{Why} and \gnat{Why not?} and everything's ok.

But watch out for the difference to \foo{Me}-too and \gnat{You}-never.
\end{document}

jeburke@jhunix.HCF.JHU.EDU (John Burke) (01/24/91)

In article <1991Jan24.095405.8868@uni-paderborn.de> elmar@pbinfo.UUCP (Elmar Schalueck) writes:
>%  I discovered a beautiful difference in macros depending
>%  on the position of the left braces after the macroname.
>%  Is this a desired effect and how can I make use of it to apply
>%  macros to whole words that are not surrounded by braces?
>%
>%  Greetings and have some fun with the example below
>%
>%  Elmar Schal\"{u}ck           elmar@uni-paderborn.de
>%
>%
>\documentstyle{article}
>\begin{document}
>\long\def\foo#1
>{Hurrah #1 Hurrah}
>\long\def\gnat#1{Yippee #1 Yippee}
>Now I test \foo{Why} and \gnat{Why not?} and everything's ok.
>
>But watch out for the difference to \foo{Me}-too and \gnat{You}-never.
>\end{document}

From _The TeXbook_, p. 201 (of my copy, anyway, pre-3.0):
<dangerous bend>  Caution: When you define a macro with simple
parameters, as in these examples, you must be careful not to put blank
spaces before the '{' that begins the replacement text. For example,
'\def\row #1 #2 {...}' will not give the same result as
'\def\row#1#2{...}', becuase the spaces after #1 and #2 tell TeX to
look for arguments that are followed by spaces.  (Arguments can be
"delimited" in a fairly general way, as explained below.)  But the
space after \row is optional, as usual, becuase TeX always disregards
spaces after control words.  After you have said '\def\row#1#2{...}',
you are allowed to put spaces between the arguments (e.g., '\row x
n'), because TeX doesn't use single spaces as undelimited arguments.
---

... in other words, look for answers to all life's trying questions in
the Good Book.  Blessed be Donald Knuth, and the horse He rode in on.

John Burke
----------
jeburke@jhunix.hcf.jhu.edu                    | Johns Hopkins doesn't
Homewood Academic Computing Information Center| want my opinions.  Why was
The Johns Hokins University                   | he selling whiskey, though.
---------

ogawa@orion.arc.nasa.gov (Arthur Ogawa) (01/25/91)

The macro \foo, defined as 

\long\def\foo#1
{Hurrah #1 Hurrah}

Has a delimited argument: the delimiter is the whitespace immediately
preceeding the left brace. In your document, 

Now I test \foo{Why} and \gnat{Why not?} and everything's ok.

But watch out for the difference to \foo{Me}-too and \gnat{You}-never.

Your first instance traces to:

\foo #1 ->Hurrah #1 Hurrah
#1<-Why

In the second, it traces to 

\foo #1 ->Hurrah #1 Hurrah
#1<-{Me}-too

This behaviour is easy to understand if you keep in mind that the argument to
\foo ends at the first whitespace character---unlike the definition of \gnat.
This is an attribute of TeX (see chapter 20 of the TeXbook), not a peculiarity
of LaTeX. Keep at it, your experimentation will pay off richly in a deeper 
understanding of TeX!