Add optional argument to newcommand for integration dx

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.


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


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