# gensym for TeX?

In other macro systems (notably Lisp's), one often needs to store results into "temporary" variables which shouldn't clash with any variables in the environment where the macro is called. In Lisp, the mechanism for declaring such variables is "gensym"---basically it is a function that returns a fresh variable name that is guaranteed to be unused anywhere else.

It does this essentially by appending an increasing integer onto a variable name prefix. [This is essentially correct, but slightly a lie. Lisp has to do some symbol magic to ensure the variable can't be referred to anywhere else.]

Is there such a capability in Plain TeX?

-
\begingroup-\endgroup? –  egreg Feb 25 '12 at 16:56
that's true. { and } manage scope properly, unlike Lisp. Thanks! –  EfForEffort Feb 25 '12 at 17:01

There's nothing of this kind in TeX. However, any macro defined inside a group (which can be initiated in several different ways) will not be accessible outside that group, unless defined with \gdef or \xdef (or if \globaldefs is positive). So
{\def\temp{something}...}

will possibly clobber the meaning of \temp inside the group, but that meaning will be restored at the end of the group or, if \temp was undefined to begin with, it will disappear completely.