![通过其他“\newcommand”递归定义的具有 25 个参数的“\newcommand”不起作用](https://linux22.com/image/414625/%E9%80%9A%E8%BF%87%E5%85%B6%E4%BB%96%E2%80%9C%5Cnewcommand%E2%80%9D%E9%80%92%E5%BD%92%E5%AE%9A%E4%B9%89%E7%9A%84%E5%85%B7%E6%9C%89%2025%20%E4%B8%AA%E5%8F%82%E6%95%B0%E7%9A%84%E2%80%9C%5Cnewcommand%E2%80%9D%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8.png)
我使用“newcommand”定义了一个具有 25 个参数的自定义命令,我称之为“commandM”。
我正在使用块矩阵。您需要在文本中重复它们,并修改一两个条目。我不想在文本中一直重复 5 x 5 块矩阵中的所有代码。所以我用 25 个参数创建了这个命令。
为了定义“commandM”,我使用了同样通过“newcommand”定义的另外 4 个自定义命令。它们是:“commandA”(带有 9 个参数)、“commandB”(带有 6 个参数)、“commandC”(带有 6 个参数)和“commandD”(带有 4 个参数)。
但是当我编译 LaTex 代码时,Texmaker 5.0.2 编译了几分钟,但始终无法完成代码处理。它似乎进入了循环。TeXstudio 2.12.22 也发生了同样的情况。我也尝试在 overleaf.com 上进行编译,但问题仍然存在。
问题的最小示例是下面的 LaTeX。尝试制作我能制作的最小问题示例。但如您所见,它很长。
问题。如何从几个其他自定义命令(每个命令都有多个参数)递归地定义一个带参数的自定义命令,而不会出现上述编译问题并正常工作?
提前致谢。
\documentclass[a4paper]{article}
\begin{document}
%\begin{comment}
\newcommand{\commandA}[9]{
%parameters
\def\Aaa{#1}\def\Aab{#2}\def\Aac{#3}
\def\Aba{#4}\def\Abb{#5}\def\Abc{#6}
\def\Aca{#7}\def\Acb{#8}\def\Acc{#9}
%%%
\left[
\begin{array}{r r r}
\Aaa & \Aab & \Aac \\
\Aba & \Abb & \Abc \\
\Aca & \Acb & \Acc \\
\end{array}
\right]
}
$$
\commandA{A_{11}}{A_{12}}{A_{13}}{A_{21}}{A_{22}}{A_{23}}{A_{31}}{A_{32}}{A_{33}}
$$
%\end{comment}
%\begin{comment}
\newcommand{\commandB}[6]{
%parameters
\def\Baa{#1}\def\Bab{#2}
\def\Bba{#3}\def\Bbb{#4}
\def\Bca{#5}\def\Bcb{#6}
%%%
\left[
\begin{array}{r r }
\Baa & \Bab \\
\Bba & \Bbb \\
\Bca & \Bcb \\
\end{array}
\right]
}
$$
\commandB{B_{11}}{B_{12}}{B_{21}}{B_{22}}{B_{31}}{B_{32}}
$$
%\end{comment}
%\begin{comment}
\newcommand{\commandC}[6]{
%parameters
\def\Caa{#1}\def\Cab{#2}\def\Cac{#3}
\def\Cba{#4}\def\Cbb{#5}\def\Cbc{#6}
%%%
\left[
\begin{array}{r r r}
\Caa & \Cab & \Cac \\
\Cba & \Cbb & \Cbc
\end{array}
\right]
}
$$
\commandC{C_{11}}{C_{12}}{C_{13}}{C_{21}}{C_{22}}{C_{23}}
$$
%\end{comment}
%\begin{comment}
\newcommand{\commandD}[4]{
%parameters
\def\Daa{#1}\def\Dab{#2}
\def\Dba{#3}\def\Dbb{#4}
%%%
\left[
\begin{array}{r r}
\Daa & \Dab \\
\Dba & \Dbb
\end{array}
\right]
}
$$
\commandD{D_{11}}{D_{12}}{D_{21}}{D_{22}}
$$
%\end{comment}
$$
\commandD{\commandA{A_{11}}{A_{12}}{A_{13}}{A_{21}}{A_{22}}{A_{23}}{A_{31}}{A_{32}}{A_{33}}}{\commandB{B_{11}}{B_{12}}{B_{21}}{B_{22}}{B_{31}}{B_{32}}}{\commandC{C_{11}}{C_{12}}{C_{13}}{C_{21}}{C_{22}}{C_{23}}}{\commandD{D_{11}}{D_{12}}{D_{21}}{D_{22}}}
$$
%\begin{comment}
\newcommand{\commandM}[9]{
%parameters
\def\Aaa{#1}\def\Aab{#2}\def\Aac{#3}
\def\Aba{#4}\def\Abb{#5}\def\Abc{#6}
\def\Aca{#7}\def\Acb{#8}\def\Acc{#9}
\newcommand{\scommandM}[6]{
%parameters
\def\Baa{#1}\def\Bab{#2}
\def\Bba{#3}\def\Bbb{#4}
\def\Bca{#5}\def\Bcb{#6}
\newcommand{\sscommandM}[6]{
%parameters
\def\Caa{#1}\def\Cab{#2}\def\Cac{#3}
\def\Cba{#4}\def\Cbb{#5}\def\Cbc{#6}
\newcommand{\ssscommandM}[4]{
%parameters
\def\Daa{#1}\def\Dab{#2}
\def\Dba{#3}\def\Dbb{#4}
%comando
\commandD{\commandA{\Aaa}{\Aab}{\Aac}{\Aba}{\Abb}{\Abc}{\Aca}{\Acb}{\Acc}}{\commandB{\Baa}{\Bab}{\Bba}{\Bbb}{\Bca}{\Bcb}}{\commandC{\Caa}{\Cab}{\Cac}{\Cba}{\Cbb}{\Cbc}}{\commandD{\Daa}{\Dab}{\Dba}{\Dbb}}
}\ssscommandM
}\sscommandM
}\scommandM
}
$$
\commandM{Aaa}{Aab}{Aac}{Aba}{Abb}{Abc}{Aca}{Acb}{Acc}{Baa}{Bab}{Bba}{Bbb}{Bca}{Bcb}{Caa}{Cab}{Cac}{Cba}{Cbb}{Cbc}{Daa}{Dab}{Dba}{Dbb}
$$
%\end{comment}
\end{document}
答案1
如果你真的想要一个带有 25 个强制参数的命令(为什么不使用更易用的语法,比如任意长度的逗号分隔列表),那么你可以执行以下操作
\documentclass[a4paper]{article}
\def\command#1#2#3#4#5#6#7#8#9{%
\def\Aa{#1}%
\def\Ab{#2}%
\def\Ac{#3}%
\def\Ad{#4}%
\def\Ae{#5}%
\def\Af{#6}%
\def\Ag{#7}%
\def\Ah{#8}%
\def\Ai{#9}%
\commandB}
\def\commandB#1#2#3#4#5#6#7#8#9{%
\def\Aj{#1}%
\def\Ak{#2}%
\def\Al{#3}%
\def\Am{#4}%
\def\An{#5}%
\def\Ao{#6}%
\def\Ap{#7}%
\def\Aq{#8}%
\def\Ar{#9}%
\commandC}
\def\commandC#1#2#3#4#5#6#7{%
\def\As{#1}%
\def\At{#2}%
\def\Au{#3}%
\def\Av{#4}%
\def\Aw{#5}%
\def\Ax{#6}%
\def\Ay{#7}%
\commandD}
\def\commandD{%
something with [\Aa] to [\Ay].%
}
\begin{document}
\command{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}{17}
{18}{19}{20}{21}{22}{23}{24}{25}
\end{document}
答案2
您可能需要更友好的语法:
\documentclass{article}
\usepackage{amsmath,blkarray}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\blocks}{mmmm}
{
\mo_blocks:nnnn { #1 } { #2 } { #3 } { #4 }
}
\seq_new:N \l__mo_blocks_a_seq
\seq_new:N \l__mo_blocks_b_seq
\seq_new:N \l__mo_blocks_c_seq
\seq_new:N \l__mo_blocks_d_seq
\tl_new:N \l__mo_blocks_i_tl
\tl_new:N \l__mo_blocks_ii_tl
\tl_new:N \l__mo_blocks_iii_tl
\tl_new:N \l__mo_blocks_iv_tl
\tl_new:N \l__mo_blocks_v_tl
\cs_new_protected:Nn \mo_blocks:nnnn
{
\seq_set_split:Nnn \l__mo_blocks_a_seq { ; } { #1 }
\seq_set_split:Nnn \l__mo_blocks_b_seq { ; } { #2 }
\seq_set_split:Nnn \l__mo_blocks_c_seq { ; } { #3 }
\seq_set_split:Nnn \l__mo_blocks_d_seq { ; } { #4 }
% make the rows
% 1
\tl_set:Nx \l__mo_blocks_i_tl
{ \seq_item:Nn \l__mo_blocks_a_seq { 1 } , \seq_item:Nn \l__mo_blocks_b_seq { 1 } }
% 2
\tl_set:Nx \l__mo_blocks_ii_tl
{ \seq_item:Nn \l__mo_blocks_a_seq { 2 } , \seq_item:Nn \l__mo_blocks_b_seq { 2 } }
% 3
\tl_set:Nx \l__mo_blocks_iii_tl
{ \seq_item:Nn \l__mo_blocks_a_seq { 3 } , \seq_item:Nn \l__mo_blocks_b_seq { 3 } }
% 4
\tl_set:Nx \l__mo_blocks_iv_tl
{ \seq_item:Nn \l__mo_blocks_c_seq { 1 } , \seq_item:Nn \l__mo_blocks_d_seq { 1 } }
% 5
\tl_set:Nx \l__mo_blocks_v_tl
{ \seq_item:Nn \l__mo_blocks_c_seq { 2 } , \seq_item:Nn \l__mo_blocks_d_seq { 2 } }
% replace , with &
\tl_replace_all:Nnn \l__mo_blocks_i_tl { , } { & }
\tl_replace_all:Nnn \l__mo_blocks_ii_tl { , } { & }
\tl_replace_all:Nnn \l__mo_blocks_iii_tl { , } { & }
\tl_replace_all:Nnn \l__mo_blocks_iv_tl { , } { & }
\tl_replace_all:Nnn \l__mo_blocks_v_tl { , } { & }
% make the block matrix
\left[
\vcenter{\hbox{$
\begin{blockarray}{ccccc}
\begin{block}{[ccc][cc]}
\smash[b]{\vphantom{\Big|}}
\tl_use:N \l__mo_blocks_i_tl \\
\tl_use:N \l__mo_blocks_ii_tl \\
\smash[t]{\vphantom{\Big|}}
\tl_use:N \l__mo_blocks_iii_tl \\
\end{block}
\begin{block}{[ccc][cc]}
\smash[b]{\vphantom{\Big|}}
\tl_use:N \l__mo_blocks_iv_tl \\
\smash[t]{\vphantom{\Big|}}
\tl_use:N \l__mo_blocks_v_tl \\
\end{block}
\end{blockarray}
$}\vspace{-2.5ex}}
\right]
}
\ExplSyntaxOff
\begin{document}
\[
\blocks{
1,2,3;4,5,6;7,8,9
}{
10,11;12,13;14,15
}{
16,17,18;19,20,21
}{
22,23;24,25
}
+\blocks{
a_{11},a_{12},a_{13};a_{21},a_{22},a_{23};a_{31},a_{32},a_{33}
}{
b_{11},b_{12};b_{21},b_{22};b_{31},b_{32}
}{
c_{11},c_{12},c_{13};c_{21},c_{22},c_{23}
}{
d_{11},d_{12};d_{21},d_{22}
}
\]
\end{document}