TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

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

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 is 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.

share|improve this question
    
Possible. But the meaning will change. Do you really want that? – Johannes_B 4 hours ago
    
One option is to define \newcommand\p{{+}} and then write $a\p b\p c\p d\p e$. – Andrew 4 hours ago
    
Are you using \fooplus inside math mode? – Werner 4 hours ago
    
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 4 hours ago
    
@Johannes_B: it will be very useful in my opinion since we can easily enclose or remove the command \fooplus. – Trung Ta 3 hours ago
up vote 4 down vote accepted

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.

enter image description here

share|improve this answer
    
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 24 mins ago
1  
@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 21 mins ago
    
Thanks a lot! Now I understand. Previously, I misunderstood the number as a spacing metrics :-D – Trung Ta 13 mins ago

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.

enter image description here

% !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}
share|improve this answer
    
Thanks! This is very neat. I wonder why Latex cannot do the same thing? – Trung Ta 32 mins ago
    
@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 6 mins ago

Your Answer

 
discard

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.