# Should I \mathrm the d in my integrals?

When writing an integral, it seems like something should be done to separate the "d", as in \int f(x) dx, so as not to confuse it with a variable. I've seen it left as-is, bolded, and straightened. Even among those options there are several ways to accomplish each task; e.g., I could do a \mathrm or a \operatorname. What is the preferred method of dealing with the "d"?

• See also What's the proper way to typeset a differential operator? and the comments at Top four LaTeX mistakes -- there appear to be some regional variances, and some attempts at standardization. Jun 20, 2012 at 13:10
• Using \mathrm or not depends on the traditions in your field. A thin space before the "d" in integrals is certainly required, Herbert's solution shows how to get it automatically (but using a macro for getting the "d"). Jun 20, 2012 at 13:10
• Related Question: new command for the dx of intergral. Nov 17, 2012 at 9:42
• The standard in mathematics is not to use mathrm on the d: just use \, dx. In engineering and physics, they do things differently. Nov 27, 2013 at 9:15
• If that's any indication, the amsmath manual does not \rm it's integral d's: mirrors.ctan.org/macros/latex/required/amsmath/amsldoc.pdf
– bers
Mar 26, 2019 at 16:18

\documentclass{article}
\usepackage{amsmath}
\newcommand*\diff{\mathop{}\!\mathrm{d}}
\newcommand*\Diff[1]{\mathop{}\!\mathrm{d^#1}}
\begin{document}

\begin{align*}
\biggl(\int_{-\infty}^\infty e^{-x^2}\diff x\biggr)^2
&= \int_{-\infty}^\infty\int_{-\infty}^\infty e^{-(x^2+y^2)}\diff x\diff y \\
&= \int_0^{2\pi}\int_0^\infty e^{-r^2}r\diff r\diff\theta                  \\
&= \int_0^{2\pi}\biggl(-{e^{-r^2}\over2}\bigg\vert_{r=0}^{r=\infty}\,\biggr)\diff\theta\\
&= \pi                                          \tag*{q.e.d.}\\
\end{align*}
%
$V(\mathbf{x}) = -\int_{\mathbf{R}^3} \frac{G}{|\mathbf{x}-\mathbf{y}|}\,\rho(\mathbf{y})\,\Diff3\mathbf{y}$

\end{document}


• Herbert, are you recommending what's on the rhs? It looks odd to me, especially when I have an inline $dy/dx$. Jun 20, 2012 at 13:07
• IMO it makes sense to add a small explanation of why this solution was chosen, rather than just providing uncommented source code. Jun 20, 2012 at 13:09
• if i'm not mistaken, the upright "d" is an iso standard. but it's not common practice in the u.s. (and perhaps elsewhere). certainly knuth uses -- intentionally -- an italic "d" as can be inferred from the italic correction "d" is given in the cmmi fonts, namely none. what i find peculiar in @Herbert's example is the italic "d" on the left side while upright is used on the right. in my opinion, whichever is chosen should be used consistently. Jun 20, 2012 at 13:19
• @JimHefferon: in inline mode I use only \mathrm{d}
– user2478
Jun 21, 2012 at 6:39
– Jost
Nov 5, 2013 at 13:16

I found a TUGboat article some years ago which seems to deal with the spacing around the differential operator in the correct way (at least to me).

Example

\documentclass{article}

\makeatletter
\providecommand*{\dif}%
{\@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} \makeatother \begin{document} \[ \int x \dif x$

\end{document}


Update

As pointed out by Enrico Gregorio and implemented by Herbert Voß, the following will do:

\documentclass{article}

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

\begin{document}

$\int x \dif x$

\end{document}

• Claudio Beccari later discovered that \newcommand\dif{\mathop{}\!\mathrm{d}} does the same with much less effort. Jan 28, 2013 at 16:37
• According to tug.org/pipermail/texhax/2009-August/013018.html, the following by Morten Høgholm is an improved version of the large code chunk I posted: \newcommand*\dif{ \mathop{}\nobreak \mskip-\thinmuskip\nobreak \mathrm{d} } what is best of Morten's code and the code posted by @egreg ? Jan 28, 2013 at 17:03
• It's just the same, with two redundant \nobreak that do exactly nothing, because a line break is not possible in a math formula after a mathop atom or after \mskip glue. Jan 28, 2013 at 17:07

\end{document}


### Output

The main advantages of using this solution:

• When using brackets, it chooses the ideal separation depending on neighbours. If you write it without brackets, for example \dd x, there will be no separation.
• By default, it follows the ISO 80000-2:2019 standard. However, you can add a package option to set italic as default style: \usepackage[italicdiff]{physics}

• Sorry, but I disagree. The definition of \dd is uselessly complicated and doesn't work as it should. Feb 2, 2021 at 20:37
• Thanks both, Werner and egreg. I agree with Werner, this should be a comment. However, I have little reputation for now and decided to make an answer as I can't comment. I thought it would be useful. Anyway, I apologise if it wasn't supposed to be like this. Should I delete the answer? Or maybe fix it? And, well... I didn't know that the definition of \dd was so complicated :( I don't want to start a chat here, so is there anyway you could explain me that out of here, egreg? Again, thank you! Feb 2, 2021 at 21:40