[texhax] testing if a control sequence is primitive
Heiko Oberdiek
heiko.oberdiek at googlemail.com
Sun Oct 9 03:27:53 CEST 2011
On Sat, Oct 08, 2011 at 09:48:54PM +0100, Philip TAYLOR (Webmaster, Ret'd) wrote:
> I am /seriously/ impressed, and was about to nominate you
> for the post of vice-Grand Wizard of TeX (no salary, I am
> afraid),
Thanks.
> when I thought to try the following :
>
> \Test \newif
\newif is "outer" in plain TeX. Outer macros cannot be catched
as arguments. Thus \ifprimitivecs of my previous mail can be used
with any single token as argument except outer macros.
The following \ifprimitivecsname catches command names
(without backslash). The command tokens are internally
constructed by \csname. But this \ifprimitivecsname also
works with names of outer commands.
\catcode`\{=1
\catcode`\}=2
\catcode`\#=6
\def\ifprimitivecsname#1{%
\begingroup
\escapechar=`\\%
\edef\tempa{\expandafter\string\csname#1\endcsname}%
\edef\tempb{\expandafter\meaning\csname#1\endcsname}%
\expandafter\endgroup
\ifx\tempa\tempb
\expandafter\FirstOfTwo
\else
\expandafter\SecondOfTwo
\fi
}
\long\def\FirstOfTwo#1#2{#1}
\long\def\SecondOfTwo#1#2{#2}
\begingroup
\catcode`\|=0 %
\catcode`\\=12 %
|SecondOfTwo{}{|endgroup
|def|Backslash{\}%
}
\def\Test#1{%
\ifprimitivecsname{#1}{%
\immediate\write16{\Backslash#1 is original primitive}%
}{%
\immediate\write16{\Backslash#1 is something else}%
}%
}
\Test{hbox}
\Test{engraf}
\Test{ }
\Test{-}
\Test{/}
\Test{newif}
\newif\iffoobar
\Test{iffoobar}
\Test{iftrue}
\Test{iffalse}
\Test{else}
\Test{fi}
\Test{undefined}
\Test{par}
\begingroup
\immediate\write16{Redefine \string\par}%
\def\par{\endgraf}%
\Test{par}%
\endgroup
\csname @@end\endcsname\end
Yours sincerely
Heiko Oberdiek
More information about the texhax
mailing list