# Anonymous Functions (Closures) in LaTeX

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

\newfunc\wavefcn{\Psi}[elemcmd=\anon{\vec{r}_#2},index={1,2,:,n}]


where \wavefcn produces

.

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

-

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:

\documentclass{article}
\usepackage{mlist}
\makeatletter
\define@key[mlist]{sym}{elemfn}{\def\mlist@elemcmd##1##2{#1}}
\makeatother
\begin{document}
\newfunc\wavefcn{\Psi}[elemfn=\vec{r}_####2,index={1,2,:,n}]
$\wavefcn$
\end{document}


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

\def\mlist@elemcmd##1##2{\vec{r}_##2}


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.

-
+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