請看下面的MWE:
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\pdiff{s o m O{1} g O{1} g O{1}}% Star, function, variable 1, how many differentiations, ...
{\newcount\pdiffn% Count the number of differentiations
\advance\pdiffn #4\relax%
\IfValueT{#5}{\advance\pdiffn #6\relax}%
\IfValueT{#7}{\advance\pdiffn #8\relax}%
\frac%
{\partial \ifnum\pdiffn >1\relax ^{\the\pdiffn} \fi%
\IfValueT{#2}{#2}}%
{\IfValueT{#7}{\mathinner{\partial #7 \ifnum#8 >1\relax ^{#8} \fi}}%
\IfValueT{#5}{\mathinner{\partial #5 \ifnum#6 >1\relax ^{#6} \fi}}%
\mathinner{\partial #3 \ifnum#4 >1\relax ^{#4} \fi}}}
\begin{document}
\Huge
\[ \pdiff[f(x,y,z)]{y} \]
\[ \pdiff[f(x,y,z)]{y}[3]{x}{z} \]
\[ \pdiff[f(x,y,z)]{y}[9999]{x}[3333]{z}[8888] \]
\end{document}
(忽略星型參數,我用它來創建平面壓裂(現在不需要)。)
如果微分僅限於 3 個變量,則此方法效果很好。是否有可能將該巨集擴展到更多變數?這樣是不行的,因為已經有8個參數了。
我知道“SplitList”和“ProcessList”,但這裡更複雜,因為我必須為每個變數添加微分數。
有任何想法嗎?
答案1
listofitems
使用鍵值的方法。語法例如是
\pdiff[y=3,x,z,p=2,d=3,q,r=2,s]{f(x,y,z,p,d,q,r,s)}
這是 MWE
\documentclass{article}
\usepackage{listofitems,tikz}
\newcounter{totalindex}
\newcommand\pdiff[2][1]{%
\setsepchar{,/=}%
\readlist\Partialvars{#1}%
\setcounter{totalindex}{0}%
\foreach \Varindex in {1,...,\listlen\Partialvars[]}%
{%
\ifnum\listlen\Partialvars[\Varindex]>1\relax%
\addtocounter{totalindex}{\Partialvars[\Varindex,2]}%
\else%
\stepcounter{totalindex}%
\fi%
}%
\frac{\partial\ifnum\thetotalindex>1\relax^{\thetotalindex}\fi#2}%
{%
\foreach\Varindex in {1,...,\listlen\Partialvars[]}%
{%
\partial\Partialvars[\Varindex,1]%
\ifnum\listlen\Partialvars[\Varindex]>1\relax^{\Partialvars[\Varindex,2]}\fi%
}%
}%
}
\begin{document}
\[ \pdiff[y]{f(x,y,z)} \]
\[ \pdiff[y=3,x,z]{f(x,y,z)} \]
\[ \pdiff[y=9999, x=3333, z=8888]{f(x,y,z)} \]
\[ \pdiff[y=3,x,z,p=2,d=3,q,r=2,s]{f(x,y,z,p,d,q,r,s)} \]
\end{document}
雖然tikz
\foreach
上面使用的循環是一種易於理解的用於循環索引的構造,但可以tikz
使用包中內建的循環構造來完全消除對包的使用listofitems
。但在這種情況下,循環是透過清單的項目,並且可以透過 間接存取索引號\<loop-variable>cnt
。
\documentclass{article}
\usepackage{listofitems}
\newcounter{totalindex}
\newcommand\pdiff[2][1]{%
\setsepchar{,/=}%
\readlist\Partialvars{#1}%
\setcounter{totalindex}{0}%
\foreachitem \Var \in \Partialvars[]%
{%
\ifnum\listlen\Partialvars[\Varcnt]>1\relax%
\addtocounter{totalindex}{\Partialvars[\Varcnt,2]}%
\else%
\stepcounter{totalindex}%
\fi%
}%
\frac{\partial\ifnum\thetotalindex>1\relax^{\thetotalindex}\fi#2}%
{%
\foreachitem \Var \in \Partialvars%
{%
\partial\Partialvars[\Varcnt,1]%
\ifnum\listlen\Partialvars[\Varcnt]>1\relax^{\Partialvars[\Varcnt,2]}\fi%
}%
}%
}
\begin{document}
\[ \pdiff[y]{f(x,y,z)} \]
\[ \pdiff[y=3,x,z]{f(x,y,z)} \]
\[ \pdiff[y=9999, x=3333, z=8888]{f(x,y,z)} \]
\[ \pdiff[y=3,x,z,p=2,d=3,q,r=2,s]{f(x,y,z,p,d,q,r,s)} \]
\end{document}
答案2
答案3
您可以使用逗號分隔的清單作為變數清單。我使用的約定是
x/2,y/3,z
代表“兩次X, 三次y還有一次z」。x/2,y/3,z/1
如果您願意,也可以使用。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\pdiff}{smm}
{
\IfBooleanTF{#1}
{% with \pdiff* call the inner function for inline
\egreg_pdiff_inline:nn { #2 } { #3 } % TO DO
}
{% with \pdiff call the inner function for display
\egreg_pdiff_frac:nn { #2 } { #3 }
}
}
% allocate some variables
\int_new:N \l__egreg_pdiff_total_int
\seq_new:N \l__egreg_pdiff_vars_seq
\seq_new:N \l__egreg_pdiff_var_seq
\tl_new:N \l__egreg_pdiff_denom_tl
\cs_new_protected:Nn \egreg_pdiff_frac:nn
{
% clear the variables to be given values later
\int_zero:N \l__egreg_pdiff_total_int
\tl_clear:N \l__egreg_pdiff_denom_tl
% split the second argumen at commas
\seq_set_split:Nnn \l__egreg_pdiff_vars_seq { , } { #2 }
% map this sequence for adding to the denominator and
% computing the number of derivatives
\seq_map_function:NN \l__egreg_pdiff_vars_seq \__egreg_pdiff_var:n
% now print:
% \l__egreg_pdiff_total_int is set to the number of derivatives
% \l__egreg_pdiff_denom_tl contains the denominator
\frac
{
\partial
\int_compare:nT { \l__egreg_pdiff_total_int > 1 }
{ \sp { \int_to_arabic:n { \l__egreg_pdiff_total_int } } }
#1
}
{
\tl_use:N \l__egreg_pdiff_denom_tl
}
}
\cs_new_protected:Nn \__egreg_pdiff_var:n
{
% split the argument at / (it should be in the form 'x' or 'x/2')
\seq_set_split:Nnn \l__egreg_pdiff_var_seq { / } { #1 }
% if the sequence has one term, no exponent is present
\int_compare:nTF { \seq_count:N \l__egreg_pdiff_var_seq < 2 }
{% simple variable: add to the denominator and increment the number
\tl_put_right:Nn \l__egreg_pdiff_denom_tl { \partial #1 }
\int_incr:N \l__egreg_pdiff_total_int
}
{% multiple: add to the denominator and increment the number
% item 1 contains the variable, item 2 the exponent
\tl_put_right:Nx \l__egreg_pdiff_denom_tl
{
\partial
\seq_item:Nn \l__egreg_pdiff_var_seq { 1 }
\sp { \seq_item:Nn \l__egreg_pdiff_var_seq { 2 } }
}
\int_add:Nn \l__egreg_pdiff_total_int { \seq_item:Nn \l__egreg_pdiff_var_seq { 2 } }
}
}
\ExplSyntaxOff
\begin{document}
\[
\pdiff{f(x,y)}{x}
\quad
\pdiff{f(x,y,z)}{x/20,y/32,z}
\]
\end{document}
答案4
更新最後查看更好的版本。
儘管有一些軟體包可以實現此目的,但我提出了另一種方法,因為xparse
這裡已經負責了;-)
expl3
拆分衍生說明符列表並建立標記列表的方法:
語法:\pdiff{x/3,y/4,z/17}{f(x,y,z)}
將指定三階導數。第 4 個關於 x,第 4 個關於 y,第 17 個關於 z。
省略權力或意志/
將被解釋為派生秩序1
。
(應加對意外負數的檢查)
不評估或檢查函數參數。
導數的數量是內部計算的。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \int_add:Nn {Nx}
\seq_new:N \l_pdiff_deriv_seq
\NewDocumentCommand{\pdiffs}{mm}{%
\int_zero:N \l_tmpa_int
\seq_set_from_clist:Nn \l_tmpa_seq {#1} % Split the comma list
\seq_map_inline:Nn \l_tmpa_seq {% Traverse the local entries
\seq_set_split:Nnn \l_tmpb_seq {/} {##1}% Split into variable and order
\tl_put_right:Nn \l_tmpa_tl {\partial}% Built the nominator list
\int_compare:nNnTF {\seq_count:N \l_tmpb_seq } = {\c_one} {% Is there only one split element?
\seq_put_right:Nn \l_tmpb_seq {\c_one}% Yes
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpb_seq {1}} % Add now 'power'
}{%
\int_compare:nNnTF {\seq_item:Nn \l_tmpb_seq {2}} = {\c_one } {
% Is the derivative order one --> no power?
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpb_seq {1}}
}{
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpb_seq {1}^{\seq_item:Nn \l_tmpb_seq {2}}}
}
}
\int_add:Nx \l_tmpa_int {\seq_item:Nn \l_tmpb_seq {2}}
}
% Display the list
\mathinner{\frac{\partial^{\int_use:N\l_tmpa_int}#2}{\tl_use:N \l_tmpa_tl}}
}
\ExplSyntaxOff
\begin{document}
\Huge
\[\pdiffs{x/5,y/3}{f(x,y,z)}\]
\[\pdiffs{x/4,y/8,z/17}{f(x,y,z)}\]
\end{document}
帶有變數檢查和自動擴展的改進版本:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \int_add:Nn {Nx}
\cs_generate_variant:Nn \int_set:Nn {Nx}
\seq_new:N \l_pdiff_var_seq
\msg_new:nnn {pdiff}{negativedifforder}{Error:~The~order~of~derivative~is~negative~for~variable~#1!}
\NewDocumentCommand{\pdiffs}{mm}{%
\int_zero:N \l_tmpa_int
\tl_clear:N \l_tmpa_tl
\seq_clear:N \l_pdiff_var_seq
\seq_set_from_clist:Nn \l_tmpa_seq {#1}
\seq_map_inline:Nn \l_tmpa_seq {%
\seq_set_split:Nnn \l_tmpb_seq {/} {##1}
\seq_put_right:Nx \l_pdiff_var_seq {\seq_item:Nn \l_tmpb_seq {1}}
\int_compare:nNnTF {\seq_count:N \l_tmpb_seq } = {\c_one} {
\int_set:Nn \l_tmpb_int {\c_one}
\seq_put_right:Nn \l_tmpb_seq {\c_one}
\tl_put_right:Nn \l_tmpa_tl {\partial}
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpb_seq {1}}
}{%
\int_set:Nx \l_tmpb_int {\seq_item:Nn \l_tmpb_seq {2}}
\int_compare:nNnTF { \l_tmpb_int } < {\c_zero} {
\msg_fatal:nnx {pdiff}{negativedifforder}{\seq_item:Nn \l_tmpb_seq {1}}
}{%
\int_compare:nNnF {\l_tmpb_int } = {\c_zero} {%
\tl_put_right:Nn \l_tmpa_tl {\partial}
\int_compare:nNnTF{ \l_tmpb_int } = {\c_one } {
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpb_seq {1}}
}{%
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpb_seq {1}^{\seq_item:Nn \l_tmpb_seq {2}}}
}
}
}
}
\int_add:Nx \l_tmpa_int {\l_tmpb_int}
}
\mathinner{\frac{\partial^{\int_use:N\l_tmpa_int}#2(\seq_use:Nn \l_pdiff_var_seq {,})}{\tl_use:N \l_tmpa_tl}}
}
\ExplSyntaxOff
\begin{document}
\Huge
\[\pdiffs{x/5,y/3,z/0}{f}\]
\[\pdiffs{x/4,y/8,z/17}{f}\]
\end{document}