pearl3.tex

TeX document icon pearl3.tex — TeX document, 1 KB (2022 bytes)

File contents

\def\cs#1{{\catcode`##=12	\tt\expandafter\string\csname#1\endcsname}}
\def\nocs#1{{\escapechar=-1 \cs{#1}}}

\def\begincode{$$\vbox\bgroup\tt%
	\catcode`\\=12\catcode`\#=12\catcode`\%=12 \catcode`\ =13%
	\catcode`\{=12 \catcode`\}=12 \catcode`<=12 \catcode`>=12%
	\obeylines\catcode`&=0 }
\def\endcode{\egroup$$}

{\catcode`\ =13%
\gdef {\ }}

\def\{{\char`\{}
\def\}{\char`\}}

\noindent\vbox{\hbox{Storing the parameter text in a macro}\hrule}

\medskip
There happens quite often in package writing that we want to define a macro that defines a new type of someting, \cs{newif} for instance, and inside the definition of such a macro we need to write things like
\begincode
\expandafter\def\csname foo#1\endcsname##1{<a definition for \foo#1, using ##1>}
&endcode

Now suppose that the defining macro is \cs{NewTypeoftok#1#2{<code>}} and that if \nocs{#1={everypar}} and \nocs{#2={mypar}}, say, we want \cs{NewTypeoftok} to make the definition
\begincode
\def\strip@everypar@mypar##1\the@everypar@mypar##2\endof@fenixtok{##1##2}%
&endcode

Ok, but instead of everypar and mypar we need to build the definition with {\tt\#1} and {\tt\#2}. Is looks awkward to achieve this with chains of \cs{expandafter}'s mixed with \cs{csname}'s. We may hence define the parameter text itself (I have replaced the second @ with a /):
\begincode
%An auxiliary macro to make the code below more readable
\def\noexpname#1{\expandafter\noexpand\csname#1\endcsname}
%
\def\NewTypeoftok#1#2{%
  % Some normal code here, and at the end
  %
  \edef\tempa{\noexpname{strip@#1/#2}####1\noexpname{the@#1/#2}}%
  \expandafter\def\tempa##2\endof@fenixtok{{##1##2}}% the ####1 of \tempa is now ##1
}
&endcode

(By the way, the ``standard'' definition of \cs{strip@#1/#2} would be

\begincode
\expandafter\def\csname strip@#1/#2\expandafter\endcsname
\expandafter##\expandafter1\csname the@#1/#2\endcsname##2\endof@fenixtok{##1##2}
&endcode

and it needs the \cs{expandafter}\cs{endcsname} trick.)
\end

Document Actions