# Followup regarding flexible partial derivative macros

With reference to Can I have a flexible partial derivative macro?, maybe I should just be happy with the cool package and move on. But...

It sure would be nice if one could specify inset/outset and shorten=true/false as options to a specific instance rather than as separate style options. For example: \pderiv[inset][n]{f}{x}, or perhaps \pderiv[n,inset]{f}{x}. Has anyone tried to modify the package to behave like that?

Also, how about the op's request in the original article to be able to display in the form f_{xxy}?

-

Here is a modification of my answer to get a choice key: \pder{f}{x} or \pder{f}{*{2}{x},y}} will choose automatically between display mode or inline mode; in the former case we'll get

\frac{\partial f}{\partial x}
\frac{\partial^{3} f}{\partial x^{2}\partial y}


and in the latter

f_{x}
f_{xxy}


One can also say \pder[display]{f}{...} to get the fraction form or \pder[inline]{f}{...} for the subscript form.

\documentclass[a4paper]{article}
\usepackage{xkeyval}

\makeatletter
\define@choicekey{pder}{style}[\der@val\der@nr]{auto,display,inline}{%
\ifcase\der@nr
\let\pder@do\pder@choose
\or
\let\pder@do\pder@display
\or
\let\pder@do\pder@inline
\fi}
\newcommand\pder[1][auto]{\setkeys{pder}{style=#1}\pder@do}
\def\pder@choose#1#2{%
\mathchoice{\pder@display{#1}{#2}}{\pder@inline{#1}{#2}}
{\pder@inline{#1}{#2}}{\pder@inline{#1}{#2}}}
\newcommand{\pder@display}[2]{\begingroup
\@tempswafalse\toks@={}\count@=\z@
\@for\next:=#2\do
{\expandafter\check@var\next
\if@tempswa
\toks@=\expandafter{\the\toks@\,}%
\else
\@tempswatrue
\fi
\toks@=\expandafter{\the\expandafter\toks@\expandafter\partial\der@var}}%
\frac{\partial\ifnum\count@=\@ne\else^{\number\count@}\fi#1}{\the\toks@}%
\endgroup}
\def\check@var{\@ifstar{\mult@var}{\one@var}}
\def\mult@var#1#2{\def\der@var{#2^{#1}}\def\der@exp{#1}}
\def\one@var#1{\def\der@var{#1}\chardef\der@exp\@ne}
\newcommand{\pder@inline}[2]{\begingroup
\toks@={}%
\@for\next:=#2\do
{\expandafter\check@varinline\next
\toks@=\expandafter{\the\expandafter\toks@\der@varinline}}%
#1_{\the\toks@}%
\endgroup}
\def\check@varinline{\@ifstar\mult@varinline\one@varinline}
\def\one@varinline#1{\def\der@varinline{#1}}
\def\mult@varinline#1#2{%
\def\der@varinline{}\count@\z@ % initialize
\loop\ifnum\count@<#1\relax
\expandafter\def\expandafter\der@varinline\expandafter{%
\der@varinline#2}%
\repeat}
\makeatother

\begin{document}
$\pder{f}{x}\qquad \pder{f}{*{2}{x},y}\qquad \pder[inline]{f}{*{3}{x},y,*{4}{z}}$

$\pder{f}{x}\qquad \pder{f}{*{2}{x},y}\qquad \pder[display]{f}{*{3}{x},y,*{4}{z}}$
\end{document}

-

Here is what I use to write total and partial derivates.

% Sources :
%    * http://forum.mathematex.net/latex-f6/en-tete-de-ds-t12933.html#p124908
%    * http://forum.mathematex.net/latex-f6/derivee-avec-un-d-droit-et-espace-t12932.html#p124930
%    * http://forum.mathematex.net/latex-f6/remplacer-des-espaces-par-autre-chose-t12952.html#p125062
%    * http://forum.mathematex.net/latex-f6/probleme-de-remplacement-de-cdots-t13047.html#p125782

\documentclass[a4paper,10pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}

\usepackage{xstring}
\noexpandarg % This is necessary so as to  '' \derFrac[3]{\cos}{x} ''  works.

% Power writing of total derivate
\newcommand{\derPow}[2]{
#2^{\left( #1 \right)}
}

% Fractional writing of total derivate
\DeclareRobustCommand{\dder}{
\mathop{}\mathopen{}\mathrm{d}
}

\newcommand{\dd}[2][0]{
\IfStrEq{#1}{0}{
\dder #2
}{
\IfBeginWith{#2}{f}{
\dder^{#1} \! #2
}{
\dder^{#1}  #2
}
}
}

\newcommand{\derFrac}[3][0]{
\IfStrEq{#1}{0}{
\ensuremath{\frac{\dd{#2}}{\dd{#3}}}
}{
\ensuremath{\frac{\dd[#1]{#2}}{\dd{#3}^{#1}}}
}
}

% Subscript writing of partial derivate
\makeatletter
\let\original@partial\partial
\renewcommand{\partial}{
\original@partial\mathopen{}
}
\makeatother

\newcommand\partialSub[2]{
\def\indPartial{\StrSubstitute{#2}{^}{\addPar}} % This works because xstring traits {...} like a single character.
\ensuremath{\partial_{\indPartial} #1}
}

% Prime writing of partial derivate
\newcommand\partialPrime[2]{
\def\indPartial{\StrSubstitute{#2}{^}{\addPar}} % This works because xstring traits {...} like a single character.
\ensuremath{#1^{\prime}_{\indPartial}}
}

% Fractional writing of partial derivate
\newcommand{\pp}[2][0]{
\IfStrEq{#1}{0}{
\partial #2
}{
\IfBeginWith{#2}{f}{
\partial^{#1} \! #2
}{
\partial^{#1} #2
}
}
}

\newcommand\partialFrac[3][0]{%
\frac{\partial\IfStrEq{#1}0{}{^{#1}}#2}
{%
\StrSubstitute{\partial#3}{ }\partial[\temp]%
\expandafter\StrSubstitute\expandafter{\temp}{\partial\cdots}{\,\cdots{}\,\partial}
}
}

\begin{document}
\setlength{\parindent}{0pt}
\newcommand{\HH}{
\mathrm{H}
}

\section{Total derivate}

$\cos'(x) = \derFrac{\cos}{x} (x)$

$f'(x) = \derFrac{f}{x} (x)$

$\derPow{5}{\HH} (x) = \derFrac[5]{\HH}{x} (x)$

$\derPow{n}{G} (x) = \derFrac[n]{G}{x} (x)$

$f'''(x) = \derFrac[3]{f}{x} (x)$

$\cos'''(x) = \derFrac[3]{\cos}{x} (x)$

\section{Partial derivate}

$\partialPrime{\cos}{x} (x) = \partialFrac{\cos}{x} (x)$

$\partialPrime{f}{x} (x) = \partialFrac{f}{x} (x)$

$\partialPrime{\HH}{x} (x) = \partialFrac{\HH}{x} (x)$

$\partialPrime{f}{x^r y^s} (x,y) = \partialFrac[r + s]{f}{x^r y^s} (x,y)$

$\partialPrime{f}{x^{5 + 2} y^{4} z} = \partialFrac[13]{f}{x^{5 + 2} y^{4} z} (x,y)$

$\partialSub{G}{f^{5^2} h^4 r} (x,y) = \partialFrac[30]{G}{f^{5^2} h^4 r} (x,y)$

$\partialSub{F}{x^n \cdots z^r} (x,\ldots,y) = \partialFrac[N + \cdots + r]{F}{x^n \cdots z^r} (x,\ldots,y)$

\end{document}


-
Good! Nice complete example. I added the output so it's easy to see for readers. –  Stefan Kottwitz Nov 3 '11 at 17:09