6

I've always been using

\newcommand*{\dif}{\mathop{}\!\mathrm{d}}

to type differential operator. It is sufficient for most cases.

However I just saw a very complicated definition here:

\makeatletter
\providecommand*{\diff}%
    {\@ifnextchar^{\DIfF}{\DIfF^{}}} 
\def\DIfF^#1{%
    \mathop{\mathrm{\mathstrut d}}% 
        \nolimits^{#1}\gobblespace}
\def\gobblespace{% 
        \futurelet\diffarg\opspace}
\def\opspace{%
        \let\DiffSpace\!%
        \ifx\diffarg(%
            \let\DiffSpace\relax
        \else
            \ifx\diffarg[%
               \let\DiffSpace\relax
            \else
               \ifx\diffarg\{%
                   \let\DiffSpace\relax
               \fi\fi\fi\DiffSpace}

I can't understand this code, but it seems that \diff creates more whitespace than \dif.

Which one is better? Or is there an even better one?

2
  • 3
    That code has a history. Claudio Beccari wrote both, the complicated one first, before realizing that the result could be achieved much more simply. And the additional space is due to ^{} in line 3, which makes TeX add \scriptspace that's not actually needed.
    – egreg
    Feb 2, 2021 at 13:39
  • 1
    I wrote about it in a paper on TUGboat, pages 47–48.
    – egreg
    Feb 2, 2021 at 13:42

2 Answers 2

4

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}

enter image description here

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.

2
  • I think I found one instance where \dif introduces to much spacing: If we write a differential quotient (inline) as \dif x / \dif y, compare to \mathrm{d}x/\mathrm{d}y. What would be the best practice for this use case?
    – gerw
    Nov 17, 2021 at 8:44
  • @gerw Inline fractions often need \! to fill up holes. The choice is yours: either always adding \, in differential forms or always adding \! in inline derivatives. Alternatively, define a command for inline derivatives.
    – egreg
    Nov 17, 2021 at 9:36
2

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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.