[comp.text.tex] \loop and iteration constructs in TeX

RAYBRO%HOLON@UTRCGW.UTC.COM ("William R(ay) Brohinsky") (02/11/91)

I have seen very little said anywhere about proper use of \loop
and the other iteration operators available in plain/vanilla TeX.
I have had a few applications (most notably for mail-list and
envelope addressing) that would really have benefitted from
being able to write working loops for input from a relatively
unformatted file, or outputting repetitive sets of type.
 
I have queried Karl Berry (author of eplain, and the third listed author
of `TeX for the Impatient', a must-have book!) and he replied with
some information that was admittedly incomplete. I will post this if
there's any interest, but I would really like to see an exposition on
the iteration operators from a TeX wizard who understands and uses them.
 
Any takers?
 
raybro

bed_gdg@SHSU.BITNET (George D. Greenwade) (02/13/91)

Our mailer went down briefly yesterday and posts to it were sent to a general
delivery account, which I am just now reviewing.  This is a reply received
Mon, 11 Feb 1991 12:54:40 CST from Michael Downes <MJD@MATH.AMS.COM>.
                   ---------------------------------------
In reply to raybro's query about loops.
 
There have been a few things in TUGboat about loops.  There was
an article ``Loopy.tex'' by J. Pittman a couple of years back.  I
think the most noteworthy article, however, was the short one by A.
Kabelschacht, Volume 8, No. 2 (1987?), describing an application of
\expandafter to \loop, because the redefinition of \loop given there
allows both true and false branches within the loop body, unlike the
plain.tex definition.  This is fairly important because TeX doesn't
have any sort of \not operator for \ifnum and \ifdim comparisons (or,
equivalently, >= and <= operators), and as a result certain loop
control conditions commonly are done by awkward roundabout means.
 
Another issue with \loop is nesting.  Basically, it can be done with
plain.tex's \loop if you enclose the inner loop(s) in braces; in some
applications this extra level of grouping creates problems, however.
Pittman's article was in part addressed to this problem, if I recall
correctly.  \loop could probably be defined differently to allow
nesting without any extra work by the user, but a thorough examination
of all the issues to decide on a ``best'' definition, in some sense,
would involve a fairly substantial amount of work (which is why I
am not volunteering at this time :^).
 
> I have had a few applications (most notably for mail-list and
> envelope addressing) that would really have benefitted from
> being able to write working loops for input from a relatively
> unformatted file
 
If there is any kind of distinct delimiter at all between separate
elements of the data file, that delimiter can usually be defined to do
useful work and make it unnecessary to use \loop (single
characters could be catcoded to active; or if elements are delimited
by blank lines, \everypar should be applicable, as described in a
couple of recent TUGboat articles by Victor Eijkhout; etc.).
 
In applying \loop to successive parts of a data file the loop
structure should be fairly straightforward; the trickier part
would be reading the data in the requisite chunks.  But come to
think of it, you might have encountered a problem if you tried
to use \par (or a blank line) within the body of a loop, since
\loop is not defined as \long---using \endgraf instead of \par
will solve that problem.  Note that \par or blank line in the
data is OK, it's just forbidden in the actual code of the
loop body as written, before execution.
 
Well, why not just make \loop a \long macro and be done with it? you
may wonder.  That's certainly a possibility, but keeping it not \long
gives you quicker error trapping if you ever forget or misspell the
\repeat (you'll get the error message at the first blank line
instead of at end of your file, or of your TeX capacity [main memory],
or the first subsequent \outer macro).
 
Michael Downes
Internet: mjd@math.ams.com
-------