less than 1 minute read

In a recent attempt to make a general partial derivative macro i came across the xparse package.

The problem is that the default latex macro command \newcommand only allows for one optional argument and I wanted to make a command that could take 3 argument. The variable, the function, and the order of derivative. I turns out that this is not so easy as it might sound. At lest not without using xparse. With xparse it is quit easy.

\usepackage{xparse} %needed for DeclareDocumentCommand
\DeclareDocumentCommand{\pder}{ O{} O{} m }{\frac{\partial^{#2}#1}{\partial#3^{#2}}}

First we call the xparse package as usual with \usepackage{}. Then we use \DeclareDocumentCommand which takes on 3 values. First we have to give your macro a new name. I choose \pder for partial derivative.1 The second command defines optional and mandatory arguments. I choose the first 2 variables to be optional by o{}. You can put a default value in {} if you desire. The last file is simply the latex formula with #1 for the first variable, #2 for the second and so on.

If we then call


the result is

partial derivative

partial derivative

partial derivative

  1. Don’t use \pd. It does not work.