How to produce suitable spaces surround the differential symbol d in dx?

I'm creating a list with common math commands that I use, but \newcommand and \DeclareMathOperator both add a space after the symbol I just defined. For example, comparing the two commands defined below,

\DeclareMathOperator{\dd}{d}

% Derivartive in Leibniz notation d{#1}/d{#2}
\newcommand{\deriv}[2]{ \frac{\mathrm{d}}#1 }{ \mathrm{d}#2^} }
\newcommand{\dderiv}[2]{ \frac{\dd #1 }{ \dd #2 } }


\deriv{f}{x} gives me the usual

while \dderiv{f}{x} is compiled to

I'm using Overleaf. Is there any way to avoid this?

The \DeclareMathOperator command gives the string \mathop spacing, like the \log or \cos operators.

To get \mathord (ordinary math atom) spacing, wrap it in braces:

\newcommand{\dderiv}[2]{ \frac{{\dd} #1 }{{\dd} #2 } }


The \mathord{\dd} command will also work, so it’s up to you whether this overcomplicates things or makes it easier to understand why you are wrapping \dd.

So, for a MWE:

\documentclass{article}
\usepackage{amsmath}

\DeclareMathOperator{\dd}{d}

% Derivative in Leibniz notation d{#1}/d{#2}
\newcommand{\deriv}[2]{ \frac{\mathrm{d}#1 }{ \mathrm{d}#2} }
\newcommand{\dderiv}[2]{ \frac{{\dd} #1 }{{\dd} #2 } }

\begin{document}
$\dderiv{x}{t}$
\end{document}


You might, however, want operator-like spacing on the left but ordinary spacing on the right of \dd, for use cases like \dd x \dd y For example:

\documentclass{article}
\usepackage{amsmath}

\newcommand\dd{\mathop{}\mathrm{d}}

% Derivartive in Leibniz notation d{#1}/d{#2}
\newcommand{\deriv}[2]{ \frac{\mathrm{d}#1 }{ \mathrm{d}#2} }
\newcommand{\dderiv}[2]{ \frac{\dd #1 }{\dd #2 } }

\begin{document}
$\iint 1 \dd x \dd y$
\end{document}


ETA:

Several commenters thought the second example added excessive space, so I’ll reprint Henri Menke’s tweak from the comments:

\newcommand\dd{\mathop{}\!\mathrm{d}}

• Second example is based on an example by egreg, but he prefers an italic d operator. May 5, 2022 at 2:09
• The second solution is better. By the way, in fact, there should be spaces at the both sides of d, while people forms the habit that there is no space at the right side. But of course, we can think that d is not an operator. May 5, 2022 at 2:18
• @M.Logic If you want spaces on both sides, the OP already shows how. May 5, 2022 at 2:19
• @M.Logic I said \mathord was optional but unnecessary. You might or might not think it makes the command easier to read. May 5, 2022 at 3:05
• This is exactly what I was looking for. Thanks for the help May 5, 2022 at 3:08

Sorry, but

\DeclareMathOperator{\dd}{d}


is wrong under many respects and won't work anywhere as intended.

I'm not referring to the “upright d” (which I consider mathematically wrong, but that's not the point), but to the space that \dd will automatically add when followed by an ordinary symbol.

What you want is

\newcommand{\dd}{\mathop{}\!\mathrm{d}}


and all will go smooth.

\documentclass{article}
\usepackage{amsmath}

\newcommand\dd{\mathop{}\!\mathrm{d}}

% Derivartive in Leibniz notation d{#1}/d{#2}
\newcommand{\dderiv}[2]{ \frac{\dd #1 }{\dd #2 } }

\begin{document}

$\dderiv{f}{t} + \iint f(x,y) \dd x \dd y$

\end{document}