\documentclass{article}


\DeclareRobustCommand{\tmspace}[3]{%
  \ifmmode\mskip#1#2\else\kern#1#3\fi\relax}
\providecommand*{\negmedspace}{\tmspace-\medmuskip{.2222em}} 
%
% written by Claudio Beccari 
% published in TUGboat, Volume 18 (1997), No. 1, 39--48
%
% slightly modified by FW
\makeatletter
\providecommand*{\diff}%
  {\@ifnextchar^{\DIfF}{\DIfF^{}}}
\def\DIfF^#1{%
  \mathop{\mathrm{d{}}}%%% original version: {\mathstrut d}}%
    \nolimits^{#1}\gobblespace}
\def\gobblespace{%
  \futurelet\diffarg\opspace}
\def\opspace{%
  \let\DiffSpace\negmedspace%%% original version: \,
  \ifx\diffarg(%
    \let\DiffSpace\relax
  \else
    \ifx\diffarg[%
      \let\DiffSpace\relax
    \else
      \ifx\diffarg\{%
        \let\DiffSpace\relax
      \fi\fi\fi\DiffSpace}
\makeatother

\providecommand*{\deriv}[3][]{%
    \frac{\diff^{#1}#2}{\diff #3^{#1}}}

\begin{document}

$\diff x\diff y = r\diff r\diff\varphi$,
$\deriv[n]{y}{x}$,  
\[
   a\deriv[2]yx+b\deriv yx+c,  \quad
   \int_0^\infty f(t)\diff t
\]

\end{document}
