Adding parameters, inside a newcommand for partial derivatives

Using \usepackage{xfp}, I have created the following command:

\newcommand{\ppdv}[5]{\dfrac{\partial^{\fpeval{#3 + #5}} #1}{\partial {#2}^{#3} \partial {#4}^{#5}}}

So that, for example, if I type: \ppdv{f}{x}{2}{y}{3} I will get: \dfrac{\partial^5 f}{\partial x^2 \partial y^3}

I have two problems:

If I write \ppdv{f}{x}{2}{y}{}, meaning I only want to take the first derivative of variable y (I don't want to write number 1),

I won't get: \dfrac{\partial^3 f}{\partial x^2 \partial y}

And if I write \ppdv{f}{x}{n}{y}{4}, meaning I want to calculate the n-th derivative of variable x,

I won't get: \dfrac{\partial^{n+4} f}{\partial x^2 \partial y}

Tex doesn't compile these tasks using xfp package. Is there a way to solve this problem? I won't bother changing xfp package for another one anyway.

Also, If I write, for example, \ppdv{f}{x}{2}{y}{m}, meaning I want to calculate the m-th derivative of variable y,

I would like to get, if possible: \dfrac{\partial^{m+2} f}{\partial x^2 \partial y} with de unknown letter m, being the first one to appear in the sum.

If anyone knows how to solve this problem I will appreciate it.

• In case you didn't know, you can use LuaTeX or expl3 for the programming. Dec 5, 2021 at 5:35
• I didn't know, thank you! Dec 5, 2021 at 17:00

Here is one way to do it:

Code:

\documentclass{article}
\usepackage{mathtools}
\usepackage{xfp}
%\usepackage{pgfmath}
\usepackage{xstring}

\makeatletter
\def\@IntergerSum{0}%
\def\@NonIntergerSum{}%
\newcommand{\@GetSumAux}[1]{%
\IfStrEq{#1}{}{%
%% Ignore if this is empty
}{%
\IfInteger{#1}{%
%\pgfmathtruncatemacro\@IntergerSum{\@IntergerSum + #1}% pgfmath version
\edef\@IntergerSum{\fpeval{\@IntergerSum + #1}}%         xfp version
}{%
\IfStrEq{\@NonIntergerSum}{}{%
}{%
}%
}%
}%
}%
\newcommand{\@GetSum}[5]{%
%% If inputs are integers their values are summed, otherwise we return
%% an expression for the sum. Thus, if the last four parameters are
%%
%%      {1}{2}{3}{4} = 10
%%      {1}{2}{3}{n} = n + 6
%%      {1}{m}{3}{n} = m + n + 4
%%
%% #1 = macro to set
%% #2 = parameter
%% #3 = parameter
%% #4 = parameter
%% #5 = parameter
%% -------------
\gdef\@IntergerSum{0}%
\gdef\@NonIntergerSum{}%
%% ----------
\@GetSumAux{#2}%
\@GetSumAux{#3}%
\@GetSumAux{#4}%
\@GetSumAux{#5}%
\IfStrEq{\@NonIntergerSum}{}{%
%\edef#1{\@IntergerSum}%
\@GetExponent{#1}{\@IntergerSum}%
}{%
\IfStrEq{\@IntergerSum}{0}{%
\edef#1{^{\@NonIntergerSum}}%
}{%
\edef#1{^{\@NonIntergerSum + \@IntergerSum}}%
}%
}%
}%
\newcommand{\@GetExponent}[2]{%
%% hides exponent of 1
%% #1 = macro to set
%% #2 = value
\IfStrEq{#2}{}{%
\def#1{}%%% No exponent if this is empty
}{%
\IfStrEq{#2}{1}{%
\def#1{}% Suppress exponent of 1
}{%
\def#1{^{#2}}%
}%
}%
}%
\newcommand{\@ShowVar}[2]{%
%% #1 = variable
%5 #2 = exponenet
\IfStrEq{#1}{}{}{\partial#1#2}% %% If no var given, we skip this variable
}%
\newcommand*{\@SumExponenet}{}%
\newcommand{\@PPDV}[9]{%
\begingroup
\@GetSum{\@SumExponenet}{#3}{#5}{#7}{#9}%
%% ----------
\@GetExponent{\@ExponenentV}{#3}%
\@GetExponent{\@ExponenentX}{#5}%
\@GetExponent{\@ExponenentY}{#7}%
\@GetExponent{\@ExponenentZ}{#9}%
%% ----------
\dfrac
{\partial\@SumExponenet#1}
{
\@ShowVar{#2}{\@ExponenentV}%
\@ShowVar{#4}{\@ExponenentX}%
\@ShowVar{#6}{\@ExponenentY}%
\@ShowVar{#8}{\@ExponenentZ}%
}%
\endgroup
}

\newcommand{\pdv}[3]{\@PPDV{#1}{#2}{#3}{}{}{}{}{}{}}
\newcommand{\ppdv}[5]{\@PPDV{#1}{#2}{#3}{#4}{#5}{}{}{}{}}
\newcommand{\pppdv}[7]{\@PPDV{#1}{#2}{#3}{#4}{#5}{#6}{#7}{}{}}%
\newcommand{\ppppdv}[9]{\@PPDV{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}}%
\makeatother

\begin{document}
\begin{gather*}
\ppdv{f}{x}{1}{y}{1}% test exponent of 1
\;
\ppdv{f}{x}{2}{y}{3}% all numerical not 1
\;
\ppdv{f}{x}{n}{y}{4}% first non-numerical
\;
\ppdv{f}{x}{2}{y}{m}% second nun-numerical (desired output "m+2", not "2+m")
\;
\ppdv{f}{x}{n}{y}{m}% bot non numerical
\\[2.0ex]
\pppdv{f}{x}{1}{y}{1}{z}{1}% test exponent of 1
\;
\pppdv{f}{x}{2}{y}{3}{z}{4}% all numerical not 1
\;
\pppdv{f}{x}{m}{y}{1}{z}{2}% first non-numerical
\;
\pppdv{f}{x}{1}{y}{n}{z}{3}% second non-numerical
\\[0.5ex]
\pppdv{f}{x}{1}{y}{2}{z}{p}% third non-numerical
\;
\pppdv{f}{x}{m}{y}{2}{z}{p}% first & third  non-numerical
\;
\pppdv{f}{x}{2}{y}{m}{z}{p}% second & third  non-numerical
\\[2.0ex]
\ppppdv{f}{u}{1}{x}{1}{y}{1}{z}{1}% test exponent of 1
\;
\ppppdv{f}{u}{1}{x}{n}{y}{m}{z}{1}% second and third exponent of 1
\;
\ppppdv{f}{u}{m}{x}{n}{y}{p}{z}{r}% all non-numerical
\end{gather*}
\end{document}

• Thank you very much! That's perfect. Dec 5, 2021 at 17:01
• Now, I wonder, how can I type the same for the command \newcommand{\pppdv}[7]{\dfrac{\partial^{\fpeval{#3 + #5 + #7}} #1}{\partial {#2}^{#3} \partial {#4}^{#5} \partial {#6}^{#7}}} which takes three partial derivatives and get the same result? I tried modifying the new code, but I can't make it work. Dec 5, 2021 at 21:09
• @FacuO.Z.: Don't perform the addtion #3 + #5 + #7 unless you know they are all integers. Similarily, the expoenent ^{#3}, ^{#5} and ^{#7} should be suppressed unless you know they are not 1. Dec 5, 2021 at 23:07
• @FacuO.Z.: Please have a look at the updated solution and do more through testing. This should work for all cases up to four variables. Dec 6, 2021 at 4:55

I suggest to use directly derivative package that it has the specific macro for the your tastes.

\documentclass[a4paper,12pt]{article}
\usepackage{derivative}

\begin{document}
$\pdv[sort-numerical=last, order={-n,2}]{f}{x,y}$
\end{document}