# Operator with different spacing for binary operations

I want to create a macro

\d


where something like

$4+\d x$


is interpreted as

$4+d\! x$


but

$\int f(x) \d x$


is interpreted as

$\int f(x) \,d\! x$


In essence, I want the macro to behave differently if it is preceded by a binary operation.

• Are you really sure you want d\!x? – egreg Dec 15 '15 at 15:08
• The standard \newcommand{\diff}{\mathop{}\!d} does what you want. Don't redefine \d. – egreg Dec 15 '15 at 15:09
• So the \mathop takes care of the \, before the differential? – Bart Snapp Dec 15 '15 at 15:19
• Yes, with \mathop you get the same behavior as with \sin or \log, that leave a thin space if preceded by an ordinary or closing symbol, but not with a binary operation or relation symbol. – egreg Dec 15 '15 at 15:25

The trick is to define

\newcommand{\diff}{\mathop{}\!d}


Example:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\diff}{\mathop{}\!d}

\begin{document}

$4+dx$ % for comparison

$4+\diff x$

\bigskip

$\displaystyle\int f(x) dx$ % for comparison

$\displaystyle\int f(x)\diff x$

\end{document}


Note that the output of d\!x is

which is clearly wrong.

## Note

Don't redefine \d, which is a command for accents. You'd regret doing it, sooner or later, for instance when you cite some Indian author.