# Conditional branch with \ifinner and Spacing in higher derivative (2 independent questions) [duplicate]

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}}}


## marked as duplicate by egreg conditionals StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Jun 15 '15 at 6:40

• just use \frac !! the default is to switch styles, \tfrac is to force text style and \dfrac is to force display style. Not test is needed. – David Carlisle Jun 13 '15 at 13:50
• \ifinner is unrelated to the display/text style distinction. Are these macros based on the ones from commath which have similar tests? – David Carlisle Jun 13 '15 at 13:54
• @DavidCarlisle Yes, I copied from commath.sty and arranged it. I suppose \frac in 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
• I would not copy any of the macros in there, it is telling that I recognised the style just from the error you are asking about in your question. – David Carlisle Jun 13 '15 at 14:05
• there is no case where \ifinner does anything useful here. – David Carlisle Jun 13 '15 at 14:10

The primitive \over as used by \frac automatically switches styles. The \tfrac and \dfrac macros are for special cases to force text or display style. \ifinner does not test for text/display distinction so the solution is simple, replace all

\ifinner
\tfrac{..}{..}
\else
\dfrac{..}{..}
\fi


by

\frac{..}{..}


For your second question, I wouldn't use the negative spacing, certainly your image shows \! is too much, you can almost get away with it with f but other characters won't fit under the superscript so well.

• Sorry, I missed your answer for the second quesiton. I tried \(p)dif[2]{y}{x} and realized the problem is a matter of the glyph of "f" itself rather than that of spacing. – Merzong Jun 13 '15 at 16:55