6

In my preamble I use \newcommand\dx{\,dx}so that I can change formatting of all instances if it is needed. What I would really like is to add an optional argument so that I can type \dx whenever I need it and \dx[t] whenever I need a dt in my integration so I can change variables at will without writing separate code for each.

5
  • 3
    \newcommand\dx[1][x]{\,d#1} Sep 28, 2020 at 21:00
  • 2
    Better use a macro found on this site to type only the d with a correct spacing: \newcommand*{\dd}{\mathop{}\!\mathrm{d}} (the differential symbol should be typed in upshape) and type in your document just \dd x, \dd t &c.
    – Bernard
    Sep 28, 2020 at 21:01
  • 1
    @Bernard You know I disagree with your comment about “d” being upright. It's not the case in many typographical traditions.
    – egreg
    Sep 28, 2020 at 21:05
  • @egreg: it was only my opinion, and I added this comment to explain why there was a \mathrm.
    – Bernard
    Sep 28, 2020 at 21:07
  • 1
    Thank you! I know that there is some controversy over the typography. The current style I need is without the upright d, but when the paper goes to committee they may ask me to change it, so I just wanted this as a separate, editable, command.
    – Andrea
    Sep 28, 2020 at 21:12

5 Answers 5

8

While Bernard's advice may be preferred from a typesetting perspective, the direct answer to the OP's question concerning the use of an optional argument is

\documentclass{article}
\newcommand\dx[1][x]{\,d#1}
\begin{document}
$a \dx$ versus $a \dx[t]$
\end{document}

enter image description here

13

I'd prefer something like

\newcommand{\dx}[1][x]{\mathop{}\!d#1}

that does the trick much better than adding \, explicitly.

But if you just define a macro for d, you get easier input:

\newcommand{\diff}{\mathop{}\!d}

and then

\int f(x)\diff x
\int f(t)\diff t

Use \dd instead of \diff if you prefer. Compare with the syntax you propose

\int f(x)\dx
\int f(t)\dx[t]

and take your pick.

2
  • 1
    Could you add some explanation as to why "\mathop{}\! does the trick much better than adding \,"?
    – Werner
    Sep 28, 2020 at 22:24
  • 2
    @Werner one can use it for Leibniz derivative notation.
    – egreg
    Sep 29, 2020 at 7:29
7

Just I was trying another approach...but there are users as good as the lightning that precedes me :-).

\documentclass[12pt]{article}

\usepackage{amsmath,amssymb}
\newcommand{\df}[1]{\,\mathrm{d}{#1}}
\begin{document}

\[\int f(x)\df{x}, \int f(t)\df{t},\]
\end{document} 

enter image description here

Or this with the slanted variables:

\documentclass[12pt]{article}

\usepackage{amsmath,amssymb}
\newcommand{\df}[1]{\,d{#1}}
\begin{document}

\[\int f(x)\df{x}, \int f(t)\df{t},\]
\end{document} 

enter image description here

2

Check out the awesome package diffcoeff with a lot of customizations. For your question, the following excerpt is quoted from the manual:

diffcoeff provides a command \dl to write the ‘d’ in a differential in a manner consistent with the default form used in derivatives.

enter image description here

\documentclass{article}
\usepackage{diffcoeff}
\begin{document}
    $\dl x$\\
    $\dl x\dl3y$\\  
    $\dl x\dl3y = r\dl3r\dl3\theta$
\end{document}
0

The physics package and its \dd{} command

Why re-invent the wheel:

\usepackage{physics}
\begin{document}
$ \dv{f}{x} $ uses $ \dd{x} $ 
\end{document}

enter image description here

This has all the nice spacing, can have autoscaling parentheses (if you use \dd() instead of \dd{}), and typesets the "d" in as an upright symbol (as it should be).

5
  • 2
    Because physics does a lot of bad things.
    – egreg
    Sep 29, 2020 at 10:37
  • It's far from perfect, but it does touch on the issue of "Use a package that has some flaws" Vs "Re-invent the wheel".
    – oliversm
    Sep 29, 2020 at 12:04
  • 1
    “Some flaws” is, unfortunately, a gross understatement.
    – egreg
    Sep 29, 2020 at 12:11
  • How many times I reinvented the wheel because I didn't know there was a wheel. I almost recreated the whole derivative package just by "hey ... I could do this ... hmm that would be nice ... hey I may use an optional argument and ... ohhh interesting ...". I only found out about derivative when I noticed I didn't have enough knowledge to automatize properly the mixed partial derivatives. On the other hand, I'd also say it's good to have many people knowing how to recreate wheels just in case everyone who knows it today disappear.
    – FHZ
    Oct 5, 2020 at 3:46
  • Re-inventing the wheel is a useful learning exercise, no doubt, but certainly not something to make a habit out of. I frequently stumble on packages after I have done my own rather comparatively poor job at unintentionally mimicking them. However, similar to building you own random number generator, encryption routine, or matrix multiplier, it is useful to learn from once, but in practice should only really be left to experts/professionals.
    – oliversm
    Oct 5, 2020 at 16:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .