How do I update the macro for partial derivative in order to accept standard second or bigger order?

I have so far \newcommand{\pderiv}[2]{\frac{\partial #1}{\partial #2}} for the first order derivative. I tried to make second order as a different command (\newcommand{\pderiv2}[2]{\frac{\partial^2 #1}{\partial {#2}^2}}) but I get an error because the argument #2 does not accept sub or superscripts. How to solve this?

I know I could use \pderiv{^2 Y}{X^2} to get the proper output but I want to make it easier. (MWE bellow)

\newcommand{\pderiv}[2]{\frac{\partial #1}{\partial #2}}
%\newcommand{\pderiv2}[2]{\frac{\partial^2 #1}{\partial {#2}^2}}

$\pderiv{^2 Y}{X^2}$ is ok!!

\verb|\pderiv2| doesn't work  :(
  • 1
    Don't use numbers in macro names. Use \pderivtwo for example.
    – user11232
    Apr 3, 2015 at 13:22
  • @Harish Kumar ok. Is it wrong and return error or is it just a recommendation?
    – Claudia
    Apr 3, 2015 at 13:26
  • It is wrong and won't work.
    – user11232
    Apr 3, 2015 at 13:26
  • 2
    Have you seen Can I have a flexible partial derivative macro? Apr 3, 2015 at 13:28

2 Answers 2


commath has the higher-order partial derivatives implemented with the command \pd[order]{first argument}{second argument}

Quoting from their .sty if you want to adapt it:

% Command for partial derivatives. The first argument denotes the function and the second argument denotes the variable with respect to which the derivative is taken. The optional argument denotes the order of differentiation. The style (text style/display style) is determined automatically

EDIT: Indeed, as noted by @HarishKumar, the only problem with your custom-defined command was using a number in the macro name.

  • 2
    The \ifinner tests don't do anything remotely useful there. commath is full of such tests unfortunately Apr 3, 2015 at 13:33
  • @DavidCarlisle As I understand, this sets the appearance of the derivative into inline or display math. Would you suggest simply using \frac instead?
    – Fato39
    Apr 3, 2015 at 13:45
  • 1

Problem solved... thanks for the fast response.

\newcommand{\pdev}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\pdevII}[2]{\frac{\partial^2 #1}{\partial {#2}^2}}
\newcommand{\pd}[3][]{\frac{\partial{^{#1}}#2}{\partial #3^{#1}}}
$\pdev{^2 Y}{X^2}$ is ok!!

$\pdevII{Y}{X}$ does now work :)

Even better with $\pd[3]{Y}{X}$ 

Not the answer you're looking for? Browse other questions tagged .