# How to redefine spacing of a symbol inside a macro?

When writing mathematics expression, I would like to remove the space around the symbol +.

For a simple expression a+b, I can do it by enclosing + like a{+}b. But it requires more labor work for complex expression like: a+b+c+d+e.

I would like to ask whether there is a programmable way so that I can define a macro fooplus:

\newcommand{\fooplus}[1]{do some thing here about +}


so that \fooplus{a+b+c+d+e} produces the same result likea{+}b{+}c{+}d{+}e?

Thanks for spending time to take a look at my question.

• Possible. But the meaning will change. Do you really want that? – Johannes_B Feb 9 '17 at 6:14
• One option is to define \newcommand\p{{+}} and then write $a\p b\p c\p d\p e$. – user30471 Feb 9 '17 at 6:14
• Are you using \fooplus inside math mode? – Werner Feb 9 '17 at 6:22
• One way would be by changing catcode of +. Make Characters Active via Macro in Math Mode should get you going. However, I would recommend you not do this. There is a reason why the binary spacing is different than the unary spacing. See the difference in $+5+7=12$. – Peter Grill Feb 9 '17 at 6:29
• @Johannes_B: it will be very useful in my opinion since we can easily enclose or remove the command \fooplus. – Trung Ta Feb 9 '17 at 6:44

For simple cases it's sufficient to change the math code of + (in a group):

\documentclass{article}

\newcommand\fooplus[1]{%
\begingroup\mathcode+=\numexpr\mathcode+-"2000\relax
#1
\endgroup
}

\begin{document}
$\fooplus{a+b+c+d+e}\neq x+y$

$a+b+c+d+e\neq x+y$
\end{document}


Explanation. Since + should be a binary operation symbol, its mathcode is of the form "2abc, so subtracting "2000 we turn it into an ordinary symbol.

• Thanks a lot! I tested and it works perfectly. For curiosity, I tried also to with the comma , and subtract "1500 but it didn't work, but also work with subtracting "1000. Do you know why it is so? (note that this is for the comma ,) – Trung Ta Feb 9 '17 at 10:05
• @TrungTa The mathcode of the comma is "6abc; if you subtract "1500 you get a different symbol altogether; if you subtract "1000 you get a “closing delimiter”. Subtract "6000, instead. – egreg Feb 9 '17 at 10:08
• Thanks a lot! Now I understand. Previously, I misunderstood the number as a spacing metrics :-D – Trung Ta Feb 9 '17 at 10:16

If you can use LuaLaTeX, it's not too much work to set up a Lua function that changes the status of + -- as well as -, *, and = -- to math-ordinary. (Lua provides powerful and flexible string handling functions.) The LaTeX macro \fooplus below simply invokes the Lua function.

% !TeX program = lualatex
\documentclass{article}
\usepackage{luacode} % for 'luacode' environment and '\luastring' macro
\begin{luacode}
function fooplus ( s )
tex.sprint (( string.gsub ( s , "[%+%-%*%=]" , "{%0}" ) ))
end
\end{luacode}
\newcommand\fooplus[1]{\directlua{fooplus(\luastring{#1})}}

\begin{document}
$\fooplus{a+b+c+d+e=f-g*h}$

$a+b+c+d+e=f-g*h$
\end{document}

• Thanks! This is very neat. I wonder why Latex cannot do the same thing? – Trung Ta Feb 9 '17 at 9:57
• @TrungTa - Actually, LuaLaTeX is a combination of the LaTeX format and the LuaTeX engine. LuaTeX provides, among other things, an interface -- via the \directlua "primitive" instruction -- to the Lua programming language. (Hence the name, I suppose.) I assume your comment refers to pdfLaTeX, which is a combination of the LaTeX format and the pdfTeX engine. pdfTeX is an older, well-established, and very robust engine, but it does lack some of the "goodies" of LuaTeX. – Mico Feb 9 '17 at 10:22
• Thank you for the explanation! Indeed, I'm using pdfLaTeX. I really like the programming style of LuaTeX and may try to use it soon. – Trung Ta Feb 10 '17 at 8:38