本練習詢問如何儲存處理\chapno
時的擴展,但必須在 時擴展。這是一個完整的範例:\write
\the\count0
\shipout
\def\chapno{5}
\newwrite\cont \openout\cont=out.txt
\edef\next{\write\cont{\chapno,\noexpand\the\count0}}\next
\def\chapno{6}
\count0=100
\bye
答案中使用了一種奇怪的技巧:
{\let\the=0\edef\next{\write\cont{<token list>}}\next}
using\noexpand
相當於\let
-trick 嗎?為什麼\noexpand
不在答案中使用 - 只是為了展示可能性?
答案1
\protected@write
這本質上也是乳膠的工作原理:
\long\def \protected@write#1#2#3{%
\begingroup
\let\thepage\relax
#2%
\let\protect\@unexpandable@protect
\edef\reserved@a{\write#1{#3}}%
\reserved@a
\endgroup
\if@nobreak\ifvmode\nobreak\fi\fi
}
通常,在這樣的構造中,您可以\noexpand
在編寫巨集時可以控制的標記前面使用,但<token list>
範例中的 通常來自#1
文件中的某些部分標題參數,但您希望抑制所有暫存器的擴展,直到\shipout
並且文字可能涉及其中任何一個。因此,使其\the
不可擴展(無論您\let
做什麼,0
或\relax
任何其他不可擴展標記,那麼它將保持在 中,\edef
但在\write
.
請注意,該練習有一個簡單的情況,其中章節號是可擴展的宏。在大多數格式中,章節會自動編號,因此使用計數暫存器,並且可能最終透過\the
在這種情況下抑制所有\the
擴充功能進行存取可能會太多。 (注意 LaTeX 只是抑制\thepage
讓其他計數器在使用時擴展,而不是在最終發貨時擴展)。