# [texhax] testing if a control sequence is primitive

Sat Oct 8 22:13:32 CEST 2011

On Sun, Oct 09, 2011 at 03:26:37AM +1100, Vafa Khalighi wrote:

> > Except that \par is not always redefined in LaTeX, only in some
> > circumstances.
> >
>
> Can not it test if \par is defined in any circumstances (e.g. inside
> quotation environment)? If it can not, then the test actually fails which is
> not what I wanted.

> > What happens if \par is redefined to be another primitive?
> >
> > \let\par=\hbox
> >
>
> I want it to return "\par is not a primitive any more"

> > What about primitives under a different name?
> >
> > \let\endgraf=\par
> >
> > \endgraf is not an original primitive, but it has the meaning of
> > a primitive.
>
> I want it to return "\endgraf is not a primitive"

% iniTeX, plain, LaTeX, ...
\catcode\{=1
\catcode\}=2
\catcode\#=6

\long\def\ifprimitivecs#1{%
\begingroup
\escapechar=\\%
\edef\tempa{\string#1}%
\edef\tempb{\meaning#1}%
\expandafter\endgroup
\ifx\tempa\tempb
\expandafter\FirstOfTwo
\else
\expandafter\SecondOfTwo
\fi
}
\long\def\FirstOfTwo#1#2{#1}
\long\def\SecondOfTwo#1#2{#2}

\long\def\Test#1{%
\ifprimitivecs{#1}{%
\immediate\write16{\string#1 is original primitive}%
}{%
\immediate\write16{\string#1 is something else}%
}%
}
\Test\hbox
\Test\engraf
\Test\ %
\Test\-
\Test\/
\Test\par
\begingroup
\immediate\write16{Redefine \string\par}%
\def\par{\endgraf}%
\Test\par
\endgroup

\csname @@end\endcsname\end

Yours sincerely
Heiko Oberdiek