在 Plain TeX 中,当我尝试创建一个控制序列数组(\word0
、\word1
等)并使用循环将其打印出来时,TeX 会遇到重音符号 i 的控制序列\'{\i}
。我收到此错误:
! 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 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
答案1
一个控制序列不能同时具有两种含义。
当你这样做时,\newcount\i
你只会丢失无点的 i。
修复(一些%
在行尾):
% 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