0

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)

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

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

\verb|\pderiv2| doesn't work  :(
\end{document}
Claudia
  • 757

2 Answers2

1

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
\providecommand{\pd}[3][]{\ensuremath{
\ifinner
\tfrac{\partial{^{#1}}#2}{\partial{#3^{#1}}}
\else
\dfrac{\partial{^{#1}}#2}{\partial{#3^{#1}}}
\fi
}}

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

Fato39
  • 296
  • 2
    The \ifinner tests don't do anything remotely useful there. commath is full of such tests unfortunately – David Carlisle Apr 03 '15 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 03 '15 at 13:45
  • 1
    @Fato39 See http://tex.stackexchange.com/questions/135944/commath-and-ifinner – egreg Apr 03 '15 at 14:06
1

Problem solved... thanks for the fast response.

\documentclass{article}
\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}}}
\begin{document}
$\pdev{^2 Y}{X^2}$ is ok!!

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

Even better with $\pd[3]{Y}{X}$ 
\end{document}
Claudia
  • 757