# Clarification

\documentclass[fleqn]{article}
\def\foo{What do I put here???}
\begin{document}
$a \foo b$
$\foo b$
$a, \foo b$
$a, b$
\end{document}


I would like the document just above to be equivalent to the following one. What should I put as a definition for the \foo command?

\documentclass[fleqn]{article}
\usepackage{amsmath}
\begin{document}
$a\quad{}\forall{}b$
$\forall{}b$
$a,\forall{}b$
$a,\;\forall{}b$
\end{document}


# Original question

How can I reproduce the spacing behaviour of, the + operator in math mode, but with different spacing.

\documentclass[fleqn]{article}
\begin{document}
$a + b$
$+ b$
$a, + b$
$a, b$
\end{document}


In the document above, + has a space on either side in a + b, but has no space before or after in + b. How can I write a command with a spacing behaviour similar to +, that is having different spacing size depending on what's before (and what's after, too, if possible)?

I thought about using flexible glue, but I think it's a feature to have a space which can vary depending on how full the line is, instead of a space which varies depending on the surrounding tokens.

Note: I'm not looking for \operatorname{}. Instead, I'm trying to re-implement it, but with different spacing.

• Your objective isn't entirely clear, I'm afraid. For instance, after mentioning several possible spacing outcomes, you write "How can I write a command with similar spacing behaviour"? Similar to what? TeX has detailed rules to determine the whitespace settings between objects ("atoms") of type mathord, mathrel, mathbin, mathpunct, mathopen, mathclose, etc. Which of these rules are you looking to mimic in the command you're looking to create? Please advise. – Mico May 29 '15 at 15:33
• + is a \mathbin. It's behavior is hard-coded in the TeX engine. If you want the same behavior for a different symbol, then put it in \mathbin{...}. – Heiko Oberdiek May 29 '15 at 15:54
• Automatic spacing depending on context can only be obtained by sticking to the predefined atom types. There's a big limitation in the fact that only one value for each of \thinmuskip, \medmuskip and \thickmuskip is used in a formula. So, if you define something to produce a \mathrel atom, it will be surrounded by \thickmuskip determined from the value current at the end of the formula. – egreg May 29 '15 at 15:55
• In your first example -- a+b -- the + symbol (an object of type mathbin) is located between two objects of type mathord; in consequence, + is treated as a binary operator and whitespace in the amount of \medmuskip is inserted on either side. In the second example, + is not preceded by any (math) object and is thus treated as a unary operator, i.e., no whitespace is inserted between + and b. You'll have to pick up a copy of the TeXbook to get a hold of all possible combinations and of the associated spacing rules. – Mico May 29 '15 at 15:56
• @GeorgesDupéron You find the table for spacings between atoms at tex.stackexchange.com/a/81777/4427 – egreg May 29 '15 at 16:00

You find a table of the spacings automatically inserted by TeX in math mode at https://tex.stackexchange.com/a/81777/4427. The particular spacings for + derive from the fact that it is classified as a Bin atom and from the rules that disallow a Bin atom, transforming it into Ord, if it is not surrounded by possible combinations. So, for instance, Close Bin Ord is allowed and in $(a-b)+2$ the + has \medmuskip on either side.

The parameters that hold the value of the automatic spacings are \thinmuskip (“1” in the table), \medmuskip (“2” in the table) and \thickmuskip (“3” in the table).

An important fact is that only one value for each of the above parameters is used throughout a formula, precisely the value that's current when the $ that closes the formula is scanned and TeX starts the process of transforming the math list it has built into a horizontal list, according to the rules explained in Appendix G of the TeXbook. So, if you try doing $a\begingroup\thickmuskip=18mu+\endgroup b\$ you still get

No matter how you reset the values of the parameters, a Bin atom will always have the same space on either side of it.

It's a limitation built in TeX that the user has no way to influence the automatic selection of spacings, nor we can do tests about the previous or the following atom. With some clever macros one can identify whether a following atom is Ord, Bin or Rel (see the \binrel@ macro in amsbsy.sty, which is used for \overset and \underset of amsmath), but there's no possibility of testing the previous atom.

Things may be different with LuaTeX and its callbacks that can examine the lists being built and research on this would be very welcome, because it could give ways for extending the inflexible paradigma of TeX. There are other cases in which some flexibility would be needed; even better, defining new types of atoms and new spacing parameters could solve some problems that now require ad hoc input.

However, my impression is that you're looking at the problem of \forall from the wrong point of view. My position about the symbol is that it should never be used except in formal logic. People can obviously disagree, but in this case they should place \forall before, not after the term it quantifies. Otherwise, use “for all”, which is clearer and is less ambiguous with respect to placement.