Tex Problems

The TeXbook 练习题 7.10 的勘误。

Knuth 的 TeXbook 练习题 7.10

Define a control sequence \appendroman with three parameters such that \appendroman#1#2#3 defines control sequence #1 to expand to a control sequence whose name is the name of control sequence #2 followed by the value of the positive integer #3 expressed in roman numerals. For example, suppose \count20 equals 30; then \appendroman\a\TeX{\count20} should have the same e ect as \def\a{\TeXxxx} .

两次勘误

在第 2 版和第3 版有两次勘误:

第二版改为:

\def\appendroman#1#2#3{\edef#1{\def\noexpand#1{\csname

这个显然少印了一部分。

第三版改为

\def\appendroman#1#2#3{\edef#1{\csname
      \expandafter\gobble\string#2\romannumeral#3\endcsname}}

所以其实算是只改了一次。以后直到“Millennium Edition”也没有 修改过。

正确答案

其实这个第三版修改之后的答案仍然是错误的。我们可以用以下这个 例子来证明:

\input manmac

\def\gobble#1{}

% two definitions

\def\appendromanwrong#1#2#3{\edef#1{\csname
      \expandafter\gobble\string#2\romannumeral#3\endcsname}}

\def\appendromancorrect#1#2#3{%
\expandafter\def\expandafter#1\expandafter{%
\csname\expandafter\gobble\string#2\romannumeral#3\endcsname}}

% test case

\count20=30
\def\TeXxxx{Orinial definition for TeXxxx}

\appendromanwrong\awrong\TeX{\count20}

$\meaning\awrong$

\awrong

\appendromancorrect\acorrect\TeX{\count20}
$\meaning\acorrect$

\acorrect


\bigskip
After a redefinition for |\TeXxxx|:

\def\TeXxxx{Modified definition for TeXxxx}

\awrong

\acorrect

\bye

这个例子证明,第3版的答案定义的 \appendroman 实际上使得 当 \count20 存放的书是 30 时,

\appendroman\a\TeX{\count20}

跟 \edef\a{\TeXxxx} 有同样的效果,而不是跟 \def\a{\TeXxxx} 有同样的效果。

更正后的答案才是正确的。