Here my try. I prefer a more simply input format: \Der{<var>}{<var><num>,<var><num>,...}
e.g. \Der{f}{x3,y,z4}
. This also works for more complicated input like \Der{f}{{\hat{x}}3,y105}
.
\documentclass{article}
\makeatletter
\newcommand\Der[2]{%
\begingroup
\@temptokena{\@gobble}%
\@tempcnta\z@
\@for\var:=#2\do{%
\expandafter\@Der\var\relax
}%
\ensuremath{\frac{\partial
\ifnum\@tempcnta>\@ne
^{\the\@tempcnta}%
\fi
#1}{\the\@temptokena}}%
\endgroup
}
\def\@Der#1#2\relax{%
\ifx\relax#2\relax
\advance\@tempcnta by \@ne
\@temptokena\expandafter{\the\@temptokena\,\partial{#1}}%
\else
\advance\@tempcnta by #2\relax
\@temptokena\expandafter{\the\@temptokena\,\partial{#1}^{#2}}%
\fi
}
\makeatother
\usepackage{amsmath}
\begin{document}
\[
\Der{f}{x}
\qquad
\Der{f}{x,y}
\qquad
\Der{f}{x2,y}
\]
\[
\Der{f}{x}\qquad \Der{f}{x2,y}\qquad \Der{f}{x3,y,z4}
\]
\[
\Der{f}{{\hat{x}}3,y1,z10}
\]
\end{document}
It is also possible to avoid the need for commas. I'm thought first this is more readable but I'm not sure about that anymore. This version doesn't support negative numbers (no loss) and might be a little more sensitive than the first.
\documentclass{article}
\makeatletter
\newcommand\Der[2]{%
\begingroup
\@temptokena{\@gobble}%
\@tempcnta\z@
\expandafter\@Der@var#2\relax
\ensuremath{\frac{\partial
\ifnum\@tempcnta>\@ne
^{\the\@tempcnta}%
\fi
#1}{\the\@temptokena}}%
\endgroup
}
\def\@Der@var#1{%
\ifx\relax#1\empty\else
\def\next{\expandafter\@Der@num\expandafter{\the\@tempcntb}{#1}}%
\afterassignment\next
\@tempcntb=0%
\fi
}
\def\@Der@num#1#2{%
\ifnum#1=\z@
\advance\@tempcnta by \@ne
\@temptokena\expandafter{\the\@temptokena\,\partial{#2}}%
\else
\advance\@tempcnta by #1\relax
\@temptokena\expandafter{\the\@temptokena\,\partial{#2}^{#1}}%
\fi
\@Der@var
}
\makeatother
\usepackage{amsmath}
\begin{document}
\[
\Der{f}{x}
\qquad
\Der{f}{xy}
\qquad
\Der{f}{x2y}
\]
\[
\Der{f}{x}\qquad \Der{f}{x2y}\qquad \Der{f}{x3 y z4}
\]
\[
\Der{f}{{\hat{x}}3y1z10}
\]
\end{document}
Result (for both implementations):
cool
package?cool
is cool indeed! So I can use\pderiv[2,1]{f}{x,y}
to get\frac{\partial^3 f}{\partial x^2\,\partial y}
.\pderiv
macro? Specifically, when having more than one power in the optional argument. (See the comments to my answer.)