[Tex/LaTex] have a flexible partial derivative macro

macrosmath-modewolfram-mathematica

I was thinking about writing a partial derivative macro that has a similar syntax to the Mathematica D function. (example)

The syntax could be like this:

  • \D{f}{x} expands to \frac{\partial f}{\partial x}
  • \D{f}{x,y} expands to \frac{\partial^2 f}{\partial x\,\partial y}
  • \D{f}{{x,2},y} expands to \frac{\partial^3 f}{\partial x^2\,\partial y}
  • et cetera for any number of derivatives.

Bonus points for an optional argument so that for instance

  • \D[subscript]{f}{{x,2},y} expands to f_{xxy}

I looked at the commath package but the syntax is a little tedious. For instance, to get \frac{\partial^3 f}{\partial x^2\,\partial y} with that package I would need to do \md{f}{3}{x}{2}{y}{}. And it doesn't generalize to three variables.

Before I dive in I thought I'd ask the hive and see what's been done.

Best Answer

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"

Related Question