Nuances of $\LaTeX$ typesetting - d for derivative

If you often use $\LaTeX$ for typesetting math then you probably have come across the issue of typesetting d of infinitesimal small quantity dx. As per the standards, the d should be typed upright. But if you write dx in $\LaTeX$ math mode as $dx$, it will result in tilted d. In this article, we will explore a few ways of typesetting the d correctly in $\LaTeX$ math environment.

The basic way

This is the first method that comes to my mind because it is straightforward and does not require loading additional packages. Use $\text{d}x$ instead of $dx$.

$ dx \ne \text{d}x $

RESULTS: $dx \ne \text{d}x$

One can also use $\mathrm{d}x$ instead of $\text{d}x$. Although there is slight difference in both typesetting logic, in this case with default fonts, both will have same output. I prefer $\mathrm{d}x$ over $\text{d}x$

$ dx \ne \mathrm{d}x $

RESULTS: $dx \ne \mathrm{d}x$

Though this method works perfectly, you will find it makes the equation look too cumbrous in it's source form. If you need to write too many derivatives, instead of writing upright d everytime using such big command, you can make use of $\LaTeX$'s \newcommand to define a new short command to typeset the above code. This can be done by

\newcommand{\d}{\mathrm{d}}

Now you can simply use \d and it will print upright d as shown below.

$ dx \ne \d x $

RESULTS: $dx \ne \mathrm{d}x$

But what if you are writing a differential equation? You need to repeat the \d macro multiple time along with order of derivative. Also, if you are also dealing with partial derivatives, then you may need to write a few more macros. Though doing it is possible, it is often easier and cleaner to use separate package for such tasks. One such package to handle derivatives is physics.

Using physics package

To use this package, simply use \usepackage{physics} in the preamble of the latex document. Now you have many commands which will help you in typesetting derivatives the right way. For example, the quantity dx can be written as \dd x or \dd{x}. Not just this but the package has many other useful macros which can be used to typeset the derivatives correctly and easily. Some of the most commonly used macros are given in table below.

$\LaTeX$ code Output
\dd x $\mathrm{d} x$
\dd[n]{x} $\mathrm{d}^{n}x$
\dv{x} $\dfrac{\mathrm{d}}{\mathrm{d}x}$
\dv{f}{x} $\dfrac{\mathrm{d}f}{\mathrm{d}x}$
\dv[n]{f}{x} $\dfrac{\mathrm{d}^{n}f}{\mathrm{d}x}$
\pdv{x} $\dfrac{\partial}{\partial x}$
\pdv{f}{x} $\dfrac{\partial f}{\partial x}$
\pdv[n]{f}{x} $\dfrac{\partial^{n}f}{\partial x}$

You can see that the physics package makes typesetting derivatives very intuitive. There are many other macros in this package that can be used to typeset some other important stuff. You can read documentation of the package here.