On the definition of differential operator
Disclaimer: I use \mathrm{d}
to conform to the OP's wish, but this doesn't mean I endorse in any way whatsoever the practice, which I consider mathematically wrong.
Ceterum censeo differentiale signum non esse operatorem. is my signature on the GuIT forum.
Cato was known to always end his speeches in the Senate by Ceterum censeo Carthaginem esse delendam.
I mentioned the complicated code in a paper on TUGboat. However, I did not really analyze the code and it's a good occasion to.
The surface definition is “check whether a ^
follows and follow the branches”.
If no ^
follows, just \DIfF^{}
is executed, which is responsible for the small additional space you see, due to TeX adding \scriptspace
after an atom with a superscript or subscript (even if empty).
Otherwise just \DIfF
is executed, which requires being followed by ^
(after removal of spaces).
It adds \mathop{\mathrm{\mathstrut d}}\nolimits^{#1}
(the possible superscript to the “d” and then does \gobblespace
. Why is this needed? Because TeX will add a thin space if an Op atom is followed by an ordinary atom or another Op atom. Hence, without this, we'd get “d<thin space>x” which would be wrong.
The code for \gobblespace
consists in examining the following nonspace token (spaces are recursively removed): if it is (
or [
or \{
, nothing needs to be done, because TeX won't insert a thin space in those cases. Otherwise (and this is where the code is weak) \!
is added to compensate for the added thin space.
With \mathop{}\!\mathrm{d}
all this is completely useless: a possible superscript would by to the “d” and not to the Op atom. The thin space between the empty Op atom and the ordinary atom \mathrm{d}
can be safely removed. TeX will then obey the spacing rules after an ordinary atom.
A visual proof for the problems:
\begin{document}
$x\dif x$ $x \dif\langle x\rangle$
$x\diff x$ $x\diff\langle x\rangle$
\scriptspace=0pt
$x\dif x$
$x\diff x$
\end{document}
You can see that in the second line there is a small space between “d” and “x” that disappears in the fourth line due to setting \scriptspace
to zero. In the second line you also see the effect of the forced negative thin space in a situation where it shouldn't be inserted.
Similarly, if you do \diff\bigl(x+y\bigr)
you'd get a wrong negative space.
It is possible to use diffcoeff
package to realize a differential operator if you want to define your derivative symbol using upirght ‘d’x
using \dl
with the spacing fixed of the package. The spaces can be changed.
Here there is a short MWE:
\documentclass[a4paper,12pt]{article}
\usepackage{diffcoeff}
\begin{document}
$\dl.up.x$
\end{document}