例子

例子

我决定测试我对扩展和嵌入\g@addto@macro另外三个宏的理解。

\g@addto@macro
  \appendmacro
    \layertwo
      \activatelayerone

据我理解,下面示例的日志输出应该是:

  ==> item1item2\item \undefinedmacroA item5\undefinedmacroB

但事实上是这样的:

  ==> item1item2\item \undefinedmacroA \undefinedmacroB

例子

\documentclass{article}

\def\macrolist{}

\makeatletter
\def\appendmacro#1{%
  \g@addto@macro\macrolist{#1}
}%
\makeatother

\def\activatelayerone#1{%
  \def\layertwo##1{%
    \appendmacro{##1}
  }%
}%

\begin{document}
\null
\appendmacro{item1}
\appendmacro{item2}
\appendmacro{\unexpanded{\item}} % Demonstrate that \unexpanded must be called by the sender
\appendmacro{\unexpanded{\undefinedmacroA}}
\activatelayerone
\layertwo{item5}
\layertwo{\unexpanded{\undefinedmacroB}}
\typeout{ ==> \macrolist}
\end{document}

答案1

\def\activatelayerone#1{%
  \def\layertwo##1{%
    \appendmacro{##1}
  }%
}%

除了定义\layertwo为附加并由于缺少而添加虚假空格之外%,这还起到\@gobble丢弃下一个#1未使用的标记的作用。此处下一个标记是,\layertwo因此被丢弃并{item5}在该点进行排版。

相关内容