GeneralA question about loop/recursion from texbook

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
alansd
Posts: 1
Joined: Sat Oct 02, 2010 11:06 am

A question about loop/recursion from texbook

Post by alansd »

Hi,

I'm learning TeX from the texbook. I tried to write a loop that is similar to part of the solution to exercise 11.5 (on page 215):

Code: Select all

\def\dolist{\afterassignment\dodolist\let\next= }
\def\dodolist{\ifx\next0 \let\again\relax \else
 [\next]\let\again\dolist \fi \again}

\dolist 1 2 3 0
Then the output is "[1][ ][2][ ][3][ ] ".
However, if we remove the space right after "\next=":

Code: Select all

\def\dolist{\afterassignment\dodolist\let\next=}
\def\dodolist{\ifx\next0 \let\again\relax \else
 [\next]\let\again\dolist \fi \again}

\dolist 1 2 3 0
the output becomes "[1][2][3] ".

Can anyone explain the difference? I guess I'm confused with the tokenizing/expanding processes.

Thanks in advance!

Alan

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Re: A question about loop/recursion from texbook

Post by josephwright »

You need to read up (carefully) on the syntax for \let. TeX will skip spaces after '=' unless you have two ' ' tokens which get tokenized separately. In the first case, the space in '\let\next= }' means that the spaces between numbers in '1 2 3' are not skipped. Hence TeX assigns \next as 'a blank space'. On the other hand, with '\let\next=}' the space between '1 2 3' is treated as a skipable one. See also how one has to work to get spaces into other odd places (for example in plain.tex and latex.ltx).
Joseph Wright
Post Reply