Can I have a flexible partial derivative macro?

The cool package offers commands for writing partial derivates (and a lot of other stuff).

A short example:

enter image description here

\documentclass{article}
\usepackage{cool}
\begin{document}
\begin{equation*}
  \pderiv{f}{x} \qquad
  \pderiv[2,3]{f}{x,y} \qquad
  \pderiv[1,n,4]{f}{x,y,z}
\end{equation*}
\end{document}

Note

By default the last two of those will not work, as there is a bug in the package. This can be fixed by changing a \def to \edef in line 2059 of cool.sty, see Basic use of derivative with cool package fails with "Missing \endcsname inserted"


I have this, which is quite similar to what you want:

\documentclass[a4paper]{article} 

\makeatletter 
\newcommand{\der}[2]{\begingroup 
  \@tempswafalse\toks@={}\count@=\z@ 
  \@for\next:=#2\do 
    {\expandafter\check@var\next
     \advance\count@\der@exp 
     \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} 
\makeatother 

\begin{document} 
\[ 
\der{f}{x}\qquad \der{f}{*{2}{x},y}\qquad \der{f}{*{3}{x},y,*{4}{z}} 
\] 
\end{document}

enter image description here

EDIT: the following code will render correctly also

\pder{f}{\mathbf{x}}
\pder{f_1}{x_1}

Here it is:

\makeatletter 
\newcommand{\der}[2]{\begingroup 
  \@tempswafalse\toks@={}\count@=\z@ 
  \@for\next:=#2\do 
    {\expandafter\check@var\next\@nil
     \advance\count@\der@exp 
     \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\@nil{\def\der@var{#2^{#1}}\def\der@exp{#1}} 
\def\one@var#1\@nil{\def\der@var{#1}\chardef\der@exp\@ne} 
\makeatother 

The esdiff package should not go unmentioned in this context.

In the meantime there are two other packages which facilitate easy typesetting of derivatives.

  • bropd
  • physics