0

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?

M. Logic
  • 4,214
Novelli
  • 35
  • I believe that \DeclareMathOperator makes that space. You can look through the various suggestions at https://tex.stackexchange.com/q/244109/107497, https://tex.stackexchange.com/q/14821/107497, and https://tex.stackexchange.com/q/178946/107497. – Teepeemm May 05 '22 at 01:28

2 Answers2

4

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}

enter image description here

egreg
  • 1,121,712
2

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}}
Davislor
  • 44,045
  • Second example is based on an example by egreg, but he prefers an italic d operator. – Davislor May 05 '22 at 02: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. – M. Logic May 05 '22 at 02:18
  • @M.Logic If you want spaces on both sides, the OP already shows how. – Davislor May 05 '22 at 02:19
  • @M.Logic I said \mathord was optional but unnecessary. You might or might not think it makes the command easier to read. – Davislor May 05 '22 at 03:05
  • This is exactly what I was looking for. Thanks for the help – Novelli May 05 '22 at 03:08
  • For me personally \mathop{}\mathrm{d} is a bit too generously spaced. I prefer \mathop{}\!\mathrm{d}. – Henri Menke May 05 '22 at 07:54
  • @Davislor Try \dd(x+y) to see why your definition is disputable. – egreg May 05 '22 at 09:44
  • @egreg How would you say d(x+y) should be spaced? It’s honestly not a notation I’ve seen very often. – Davislor May 05 '22 at 13:01
  • @Davislor Sorry, it was a bad example. But your definition provides too wide a space anyway. – egreg May 05 '22 at 13:13
  • @egreg Henri Menke has posted a possible solution for that in a comment. Although I’m not completely sure it will never produce a negative space. – Davislor May 05 '22 at 13:17