TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to have an anonymous function or closures in LaTeX. I am using Will Robertson's mlist, and I have need for several macros that have a very limited usefulness. So, I'd like to be able to define a macro in place, like this


where \wavefcn produces

Psi as a function of n variables.

In the above declaration, \anon{\vec{r}_#2} is the anonymous function that is defined in place. How would one set this up?

share|improve this question
up vote 4 down vote accepted

Actually, the original plan for this package was to use syntax like this, but xkeyval at the time could not handle such constructions. Things have changed since then! Try this:


It's unfortunate that you need to double the hashes twice inside \newfunc, but the package would need more of a rework to get that working. (I didn't focus on that the first time around because the idea didn't work anyway, then.)

To answer your actual question, no, TeX doesn't have anonymous functions per say. In this case, the keyval processing hides the fact that a specific macro is being defined. Internally, writing

\newfunc \foo {...} [ key = val ]

executes an internal macro for key given the argument val. As you can see from the example above, the internal macro for elemfn simply defines a new macro based on the argument given in the keyval list; after some processing you'd see something like


So the syntax itself is what makes it like an anonymous function, but there's otherwise no such thing in TeX. (In fact, there are no “functions” at all — it is a macro expansion language so you often can't think about it in the same terms as a regular programming language.)

P.S. The thing with the hashes is that every time you nest a \def inside a \def command (or similar), you have to double them so you know which argument belongs to which \def.

share|improve this answer
+1, it works great. I hesitate to mark it as the accepted answer, though, as it does not necessarily answer the broader question of creating anonymous functions in TeX. Would you be willing to explain why your solution works? (I'm a (La)TeX novice, and I've never encountered the double hash before.) – rcollyer Aug 29 '11 at 17:11
I am no profound expert in TeX. Having said this: if there is any means of defining properly anonymous (e.g. purely lambda-expression-type) functions in TeX or LaTeX, it is an extremely well-hidden feature. You can certainly define macros which are overloaded (such as \@tempa, \@tempb, etc.) to do things that you want ad-hoc; but there is no means to describe "the function which does bla" without giving it at least a temporary name. – Niel de Beaudrap Aug 30 '11 at 0:10
@Niel, I figured they would have to have some form of temporary name, whether that was visible to the user, or not, is a different question. – rcollyer Aug 30 '11 at 0:13
@rcollyer: the answer then is "no": they're visible to anyone intrepid enough to browse your source-code. But the definition you give a macro may be limited by scope (within an environment, between a \bgroup and an \egroup, etc.), or may not be persistent (the macros \@tempa and friends are routinely cobbered as work-space by other macros); and in other ways may not be things which are reliably accessible outside of the context you use them in. Does that make them "practically anonymous"? That depends on who you're shielding your macros from. Just worry about robustness on your end! – Niel de Beaudrap Aug 30 '11 at 0:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.