在新命令中添加参数,用于偏导数

在新命令中添加参数,用于偏导数

使用\usepackage{xfp},我创建了以下命令:

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

例如,如果我输入:\ppdv{f}{x}{2}{y}{3} 我将得到:\dfrac{\partial^5 f}{\partial x^2 \partial y^3}

我有两个问题:

如果我写\ppdv{f}{x}{2}{y}{},意味着我只想取变量 y 的一阶导数(我不想写数字 1),

我不会得到:\dfrac{\partial^3 f}{\partial x^2 \partial y}

如果我写\ppdv{f}{x}{n}{y}{4},意味着我想计算变量 x 的 n 阶导数,

我不会得到:\dfrac{\partial^{n+4} f}{\partial x^2 \partial y}

Tex 不使用 xfp 包编译这些任务。有办法解决这个问题吗?无论如何,我不会费心将 xfp 包换成另一个包。

另外,如果我写,例如,\ppdv{f}{x}{2}{y}{m}意味着我想计算变量 y 的 m 阶导数,

如果可能的话,我希望得到:\dfrac{\partial^{m+2} f}{\partial x^2 \partial y}其中未知字母 m 是总和中第一个出现的字母。

如果有人知道如何解决这个问题,我将不胜感激。

答案1

以下是一种方法:

在此处输入图片描述

代码:

\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}{}{%
                \g@addto@macro\@NonIntergerSum{#1}%
            }{%
                \g@addto@macro\@NonIntergerSum{+ #1}%
            }%
        }%
    }%
}%
\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}

答案2

我建议直接使用derivative包,它具有适合您口味的特定宏。

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

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

在此处输入图片描述

在此处输入图片描述 https://ctan.mirror.garr.it/mirrors/ctan/macros/latex/contrib/derivative/derivative.pdf

相关内容