Take the 2-minute tour ×
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.

First, I am new to TeX and use it only for mathematical expressions. IMHO the most striking feature is that TeX does not respect whitespaces.

  1. In lambda calculus, application is denoted by juxtaposition. In programming languages based on lambda calculus (ML, Haskell) names may be several letters long, therefore, to separate names in application, a whitespace is placed between names. I want to use function names from standard libraries of those programming languages, therefore I can not just give up multiletter names. Currently I use “\ ” in application, but it seems hackish to me and clutters the code. What is the best way to do this? Example: You can take just any functional code, like $foldr\ f\ z\ (x:xs) = f\ x\ (foldr\ f\ z\ xs)$.

  2. BTW, is it regular in lambda calculus to embrace multiletter names in “operatorname”?

share|improve this question
\operatorname{func} will indeed add a thin space when the string is followed by another similar object, but not before an open parenthesis. Would you please add some examples to your question? And maybe some reference for us to look at. –  egreg Sep 14 '11 at 8:49
Can you give an example of the sort of thing you would like to be able to write and the output you would like it to produce? –  Loop Space Sep 14 '11 at 8:50
@egreg: I added an example. –  beroal Sep 14 '11 at 9:20

3 Answers 3

It's not quite clear what symbols should be followed by a space and what not.

Something like



$\lname{foldr}\lname{f}\lname{z}(x:xs) =

might give good results. In other words, put each symbol you want to be spaced as argument of \lname. One might choose a different name for it: \? for example. It's quite hard to think how to render such formulas automatically, without explicit markup.

A similar effect as Andrew Stacey's can be obtained in standard LaTeX: the following lines go in the preamble

  {\catcode` =12 \setupspace
   \makeordinary{:}% to make the colon an ordinary symbol
   %%% possible other \makeordinary declarations
   $\mathgroup0 }
{\catcode` =\active\gdef\setupspace{\def {\;}}}
  \divide\@tempcntb by "1000
  \multiply\@tempcntb by "1000
  \advance\@tempcnta by -\@tempcntb

In the document one can use

foldr f z (x:xs)=f x (foldr f z xs)

One should put spaces only where they must appear in the output.

Note: the space has mathcode "8000, but this mathcode is ignored when the space has category code different from 11 or 12. The indirect definition of the active space avoids a global definition that could interfere with the \verb command.

Note: the \makeordinary macro strips off the first hexadecimal digit from a mathcode.


lambda calculus

share|improve this answer
“It's not quite clear what symbols should be followed by a space and what not.” Every lambda application corresponds to 1 space. I.e. there is a space between “foldr f z” and “(x:xs)”, unlike in the common function application. (IMHO this method yields the best visual results.) –  beroal Sep 14 '11 at 9:47
Wish I could vote this up again! Now I see why the mathcode bit didn't work. What does the \mathgroup0 do? Also, that's an important point about necessary spaces. My version has spaces at the start of the line which shouldn't be there! –  Loop Space Sep 14 '11 at 10:48
@Andrew Saying \mathgroup0 is equivalent to enclose the whole formula in \mathrm, as it chooses math family 0 that always contains upright letters. \mathgroup and \fam are synonymous. –  egreg Sep 14 '11 at 10:55
In that case, if you put in the redeclaration of : then your answer becomes a superset of mine! And with more concise code. Could you add that in? –  Loop Space Sep 14 '11 at 11:30
Just tried compiling your code (to produce a screenshot - the one thing my answer still has that yours doesn't!) and discovered a problem. \DeclareMathSymbol can only be used in the preamble (unicode-math removes this prohibition). So either you need to set : as \mathord for the whole document, or you need to do something more wizardry. –  Loop Space Sep 14 '11 at 12:14

I've struggled with the same thing myself, and ended up with two possible solutions; first of which is basically egreg's solution (though you could just make the functions straight to macros, ie. \foldr). The other one is making math-mode obey spaces, but that has its own problems.

In the end, you basically won't save any typing with either of those.

To expand a little, what I've done is to define macros (these are in plain-format, latex-format would look different):

\def\defun#1{\mathop{\hbox{\it#1}}} % define functions as math operators,
  % so they get the spacing (note: not a "full" space, but a '\,'-space.)
% etc. Silly, I know.
\def\f{\mathop f} % I don't want to remove the math italic kerning from 'f',
                  % because it has some additional space
$$ \foldr \f z (x:xs) = \f x (\foldr \f z\, xs) $$
% Note: I didn't want to remove the spacing of ':', and also, I used
% a manual spacing ('\,') to separate parameters.

The output looks like this:
enter image description here

It's a little tedious, but I think it looks good.

share|improve this answer
Since egreg's answer is now a superset of mine, I've deleted mine. That might mean that you don't see my reply to your comment (I don't know). Just in case not: I agree that \foo{}bar could quickly get very tedious! Maybe in this particular case it wouldn't be too annoying as the questioner doesn't seem to use any macros in this setup. –  Loop Space Sep 14 '11 at 12:11

The semantic package can be helpful here. Among other things, semantic provides a reserved words feature that allows you to do the following:

% Preamble:

% Later:
$\<let> x = 5 \<in> x$

Produces this image:

You could define foldr, f, etc the same way (possibly exchanging \mathtt for something else).

Beyond that, semantic provides ligatures which provide a way to fix the spacing of : and .. (Both should have a space after, but not before.)

% Preamble:

% Later:
\lambda x: nat. x + 1

Produces this image:

Note: These styles attempt to mimick those from John Mitchell's textbook Foundations for Programming Languages. (Strangely enough, the formatting in my physical copy of his book differs from the downloadable chapter online, so keep that in mind.) In my opinion, my methods above do a much better job of reproducing his formatting than egreg's answer, which destroys spacing around : and removes italics on the variable names.

share|improve this answer
I've edited your post accordingly, modifying the markdown a little and embedding the images. New user restrictions are removed at 10 reputation. –  Werner Feb 2 '12 at 0:49

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.