# Better automatic spacing of differential d?

This question was basically about whether the differential d should be upright or italicized, not about how to achieve that in tex. However, this useful answer did suggest something like the following for typesetting it:

\newcommand{\der}{\operatorname{d\!}}


This is what I've been doing for a while, but I find that there are a few cases where the spacing is wrong. This code

\der(x^7) \quad \der x \quad \operatorname{d} (x^7) \quad \operatorname{d} x


gives output that looks like this:

The negative thin space looks good for typesetting dx, but bad for d(x^7). Is there a good way to define a single \der macro that automagically gets both cases right, or is my best option to define two macros and pick one or the other as needed?

• You should have a look at package physics. May 18, 2014 at 19:07
• A common definition seen in many, many places is \newcommand*\der{\mathop{}\!\mathrm{d}}. May 18, 2014 at 19:23

\operatorname turns d into an operator. If an ordinary math atom follows (x), then TeX sets a thin space that is negated by \!. However, TeX does not set a space after an operator if the following math atom belongs to categories "open", "close", "punct", or "inner" (\scriptstyle/\scriptscriptstyle). A fix would be to add an empty math ord atom (\mathord{} or an empty sub formula {}), then TeX always sets a thin space, canceled by \!. Macro \der behaves on the left-hand side as an operator and on its right-hand side as an ordinary math atom. Now both cases work as expected:

\documentclass{article}
\usepackage{amsmath}
\newcommand{\der}{\operatorname{d\!}{}}
\begin{document}
$\der(x^7) \quad \der x$
\end{document}


## Variation

The d can be put both in the operator atom or the ordinary atom, as suggested in the comment of Manuel and the comment of egreg (without \mathrm):

\mathop{}\!\mathrm{d}


## Fonts for d

• \lim, \sin and friends are using the font \operator@font. Package amsmath then provides macro \operatorname. But the former can also be used without an additional package:

\makeatletter
\newcommand*{\der}{%
\mathop{\kern\z@\operator@font d}\!{}%
}
\makeatother


\kern\z@ prevents that \mathop centers the symbol.

Or with d in the ordinary atom:

\makeatletter
\newcommand*{\der}{%
\mathop{}\!{\operator@font d}%
}
\makeatother

• Font \mathrm is easier to use (no @ in the name):

\newcommand*{\der}{%
\mathop{}\!\mathrm{d}%
}


or a little more complex, again the \kern prevents vertical centering:

\newcommand*{\der}{%
\mathop{\kern0pt\mathrm{d}}\!{}%
}

• The italics variant:

\newcommand*{\der}{%
\mathop{}\!d%
}%


or

\newcommand*{\der}{%
\mathop{\kern0pt d}\!{}%
}


• Is there any advantage or disadvantage of this compared to the definition suggested by Manuel in his comment?
– user6853
May 18, 2014 at 19:30
• @BenCrowell: Both versions consist of a math operator atom, negative kerning that cancels the automatically inserted thin space, and an ordinary atom. In the answer the d is put int the operator atom, the comment puts it in the ordinary atom, but the spacing is the same. May 18, 2014 at 19:34
• @HeikoOberdiek Did you try something like $\der(x+y)$? May 18, 2014 at 19:34
• @HeikoOberdiek Oh, I didn't see the {}. I still prefer \mathop{}\!d; it's shorter and doesn't require other packages. May 18, 2014 at 20:03
• @egreg: I have now added lots of variations. May 18, 2014 at 20:45

Packages are written by good and nice people keeping lazy and ignorant people like me in mind. Instead of re-inventing things, it is better start finding a suitable package and use it. In this case, physics package (as noted by Johannes) offers \dd macro. A screen shot of the relevant part of the physics documentation:

And a sample code:

\documentclass{article}
\usepackage{physics}
\begin{document}
$\dd(x^7) \quad \dd x$
\end{document}


• After seeing how \dd is defined in the package, I wish I didn't try reading it. May 19, 2014 at 7:53
• @egreg ???. Is it so wrong? Can you add explanations to the answers please, if you have some time? It is always good to know what is wrong in what I am using :(. I use physics too much.
– user11232
May 19, 2014 at 22:38
• I can't see what's the point in having a complicated definition when the usual one (\mathop{}\!d, add \mathrm, if you wish) works flawlessly in all cases; there's no advantage having \dd[3]{x} instead of the simpler (and clearer) \dd^3 x. There's surely no advantage in requiring \dd{x} instead of \dd x; of course with the usual definition, braces are optional. May 20, 2014 at 9:34