Produce suitable spaces surround the differential symbol d in dx

macrosmath-modemath-operators

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

enter image description here

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

enter image description here

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

Best Answer

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}

enter image description here

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}

enter image description here

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}}
Related Question