I made some macros shown below, which outputs ordinary/partial derivative. I was checking whether they work as I expected, and found that \dif{y}{x} expands to \tfrac{\difd y}{\difd x} in align environment.
I want to use \tfrac only in sentences, not in any display math modes. How should I change the definition in order to expands \dif{y}{x} to \dfrac{\difd y}{\difd x} in align environment, gather environment and so on?
I have another question. I think higher derivatives written with my command (especially partial ones) make a little much space before f.
In terms of mathematical typography, which is proper: \frac{〈operator〉^{#1}#2}{〈operator〉#3^{#1}} or \frac{〈operator〉^{#1}\!#2}{〈operator〉#3^{#1}}?

EDIT (June 13, 2015 at 23:28 JST):
The first question has been settled.
EDIT (June 15, 2015 at 14:21 JST):
As David Carlisle points out, my question was a duplicate of commath and \ifinner and commath package should not be used. Then, I have another question. In the linked page, there is a macro named \spx, the definition of which is in the below. Since we can see {^{#1}} in the definition, it seems to be an improved ^. What is the difference between this \spx and \^ and which should I use, "\(p)dif@nth and \@(p)dif" or "\spx without \@ifnextchar"?
My macros
% ----- differential operator -----
\def\difd@rm{\mathop{\mathrm{d}\!}\mathstrut}
\def\difd@it{\mathop{d\!}\mathstrut}
\def\makedifdrm{\let\difd=\difd@rm}
\def\makedifdit{\let\difd=\difd@it}
\makedifdit % default setting
%
% ----- ordinary derivative -----
\def\dif{\@ifnextchar[\dif@nth\@dif}
\def\dif@nth[#1]#2#3{
\ifinner
\tfrac{\difd^{#1}#2}{\difd#3^{#1}}
\else
\dfrac{\difd^{#1}#2}{\difd#3^{#1}}
\fi
}
\def\@dif#1#2{
\ifinner
\tfrac{\difd#1}{\difd#2}
\else
\dfrac{\difd#1}{\difd#2}
\fi
}
%
% ----- partial derivative -----
\def\pdif{\@ifnextchar[\pdif@nth\@pdif}
\def\pdif@nth[#1]#2#3{
\ifinner
\tfrac{\partial^{#1}#2}{\partial#3^{#1}}
\else
\dfrac{\partial^{#1}#2}{\partial#3^{#1}}
\fi
}
\def\@pdif#1#2{
\ifinner
\tfrac{\partial#1}{\partial#2}
\else
\dfrac{\partial#1}{\partial#2}
\fi
}
Definition of \spx and its usage
\newcommand{\spx}[1]{%
\if\relax\detokenize{#1}\relax
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{^{#1}}%
}
\newcommand\pd[3][]{\frac{\partial\spx{#1}#2}{\partial#3\spx{#1}}}
\frac!! the default is to switch styles,\tfracis to force text style and\dfracis to force display style. Not test is needed. – David Carlisle Jun 13 '15 at 13:50\ifinneris unrelated to the display/text style distinction. Are these macros based on the ones fromcommathwhich have similar tests? – David Carlisle Jun 13 '15 at 13:54commath.styand arranged it. I suppose\fracin display math mode, in some cases, could be text style (though I cannot take an example immediately). I want to avoid that. – Merzong Jun 13 '15 at 14:03\ifinnerdoes anything useful here. – David Carlisle Jun 13 '15 at 14:10\ifinner's function incommathpackage. I'm going to read the linked page and try to understand what\ifinneractually does. – Merzong Jun 13 '15 at 14:16\ifinnerin commath is simply an error. – David Carlisle Jun 13 '15 at 14:17