# Which TeX primitives can be recovered after their initial definitions are overwritten?

This is mostly an academic question out of curiosity, but I think it may give some interesting answers. The question is, given a TeX file beginning with \def\someprimitive{}, for which values of \someprimitive is it possible to recover its primitive meaning afterwards?

For example, \def\relax{} is not catastrophic because \csname can be used to get at the primitive meaning:

\def\relax{}
\meaning\relax


Relaxing (no pun intended) the requirements a bit, one could ask if it is possible to recreate a macro which for all practical purposes does the same as the original:

\def\gdef{}
\def\gdef{\global\def}


(at least, I can't think of a situation where this doesn't work, but of course it depends on \global and \def not being redefined later on).

The question can be interpreted with respect to your favorite TeX engine and associated set of primitives. But relying on a format which creates an alias for a primitive is probably cheating (such as plain TeX's \endgraf = \par).

-
Your new \gdef definition would break in e.g. \edef\foo{\noexpand\expandafter\gdef}, because it is now fragile. You should use at least use \protected\def\gdef{\global\def}. I'm aware that it is just an example. –  Martin Scharrer May 3 '11 at 8:49
If you use amsmath they store quite a bit of the primitives –  Yiannis Lazarides May 3 '11 at 9:17
Of course, if you are prepared to cheat then you can save all of the primitives, as expl3 does (well, not all of the XeTeX and LuaTeX ones just yet, but close enough). –  Joseph Wright May 3 '11 at 9:39
@Joseph: I put in the note about cheating precisely because I recalled that expl3 did that. –  Villemoes May 3 '11 at 17:19

Besides \pdfprimitive, in Luatex you can map sets of primitives onto csnames by adding prefixes. So for instance, \directlua{tex.enableprimitives('Knuths', tex.extraprimitives('tex'))} will map all Knuth's Tex primitives onto their basename with the prefix Knuths, so \Knuthsdef has the same meaning as the \def primitive.

The second argument to tex.enableprimitives is a table, so arbitrary sets of primitives can be mapped in this way. tex.extraprimitives defines a table of primitives for the base Tex primitives and for each of the various extensions of Tex ('etex', 'pdftex', 'omega', etc.) supported by Luatex.

-

With a current pdftex (luatex) you can retrieve primitives with \pdfprimitive:

\documentclass[11pt]{article}
\begin{document}

{\def\hbox{I was a hbox}
new: \hbox{}

old: \pdfprimitive\hbox{abc}}

\end{document}

-
Does \expandafter\let\expandafter\hbox\pdfprimitive\hbox redefines \hbox back to its original meaning? –  Martin Scharrer May 3 '11 at 9:52
Ok, doesn't look like it. –  Martin Scharrer May 3 '11 at 9:59
@Martin: It looks like that would require something like Common Lisp's semantics of symbols for csnames: \pdfprimitive\whatever needs to expand to a csname different from anything Tex has seen so far, like an uninterned symbol. –  Charles Stewart May 3 '11 at 10:12
@Charles: There should be some \pdfprimitivelet command then ;-) –  Martin Scharrer May 3 '11 at 10:21