7

In Plain TeX, when I try to create an array of control sequences (\word0, \word1, etc.) and print it out using a loop, TeX stumbles over the accented-i control sequence \'{\i}. I get this error:

! Missing number, treated as zero.
<to be read again> 
                   }
\word1 ->r\'{\i }
                 os
\body ->\csname word\the \i \endcsname 
                                       \^^M\advance \i by 1 \ifnum \i < \word 
\next ->\body 
              \let \next \iterate \else \let \next \relax \fi \next 
l.40 Automatic array and printing: \printlist

This makes me think I have not understood something correctly about expansion, perhaps. Why does this error occur?

The image shows the output that results from ignoring the error during compilation.

% This accent macro works.
Spelled out: r\'{\i}os

% It also works inside another macro.
\def\rios{r\'{\i}os}
Macro: \rios

% When I make a quasi-array by hand it works.
\expandafter\def\csname word0\endcsname{river}
\expandafter\def\csname word1\endcsname{r\'{\i}os}

Array by hand:
\csname word0\endcsname\
\csname word1\endcsname

% When I make the array automatically and print the results manually, it works.
\newcount\word
\word=0
\def\addtolist #1{
    \expandafter\def\csname word\the\word\endcsname{#1}
    \advance\word by 1
}
\addtolist{river}
\addtolist{r\'{\i}os}

Automatic array, print by hand:
\csname word0\endcsname\
\csname word1\endcsname

% But when I try to print the list automatically, TeX stumbles over the accented i.
\newcount\i
\def\printlist{
    \i=0
    \loop
        \csname word\the\i\endcsname\
        \advance\i by 1
        \ifnum\i < \word
    \repeat
}
Automatic array and printing: \printlist
\bye

enter image description here

0

1 Answer 1

9

A control sequence can't have two meanings at the same time.

When you do \newcount\i you simply lose the dotless i.

Fix (with some % at end of lines):

% This accent macro works.
Spelled out: r\'{\i}os

% It also works inside another macro.
\def\rios{r\'{\i}os}
Macro: \rios

% When I make a quasi-array by hand it works.
\expandafter\def\csname word0\endcsname{river}
\expandafter\def\csname word1\endcsname{r\'{\i}os}

Array by hand:
\csname word0\endcsname\
\csname word1\endcsname

% When I make the array automatically and print the results manually, it works.
\newcount\word
\word=0
\def\addtolist #1{%
    \expandafter\def\csname word\the\word\endcsname{#1}%
    \advance\word by 1
}
\addtolist{river}
\addtolist{r\'{\i}os}

Automatic array, print by hand:
\csname word0\endcsname\
\csname word1\endcsname

% But when I try to print the list automatically, TeX stumbles over the accented i.
\newcount\anewcounter
\def\printlist{%
    \anewcounter=0
    \loop
        \csname word\the\anewcounter\endcsname\
        \advance\anewcounter by 1
        \ifnum\anewcounter < \word
    \repeat
}
Automatic array and printing: \printlist
\bye

enter image description here

1
  • The lesson here is that you cannot use the same name for a count variable that you use for any other macro. Commented Jun 23, 2015 at 23:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .