18

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:

screenshot

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?

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

2 Answers 2

21

\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}

Result

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}\!{}%
    }
    

    italics

5
  • 2
    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
  • 1
    @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)$?
    – egreg
    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.
    – egreg
    May 18, 2014 at 20:03
  • @egreg: I have now added lots of variations. May 18, 2014 at 20:45
8

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:

enter image description here

And a sample code:

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

enter image description here

3
  • 3
    After seeing how \dd is defined in the package, I wish I didn't try reading it.
    – egreg
    May 19, 2014 at 7:53
  • 2
    @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
  • 2
    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.
    – egreg
    May 20, 2014 at 9:34

Your Answer

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