Rud Faden bio photo

Rud Faden

He who knows not and knows not he knows not: he is a fool - shun him. He who knows not and knows he knows not: he is simple - teach him. He who knows and knows not he knows: he is asleep - wake him. He who knows and knows he knows: he is wise - follow him.

Email Twitter Facebook Google+ LinkedIn Github Stackoverflow

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

\pder{x}
\pder[f]{x}
\pder[f][2]{x}

the result is

partial derivative

partial derivative

partial derivative

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