2

I defined a custom command with 25 parameters with the "newcommand" that I called "commandM".

I am working with block matrices. You need to repeat them in the text with one or two modified entries. I didn't want to have to repeat all the code in a 5 by 5 block matrix at all times in the text. So I made this command with 25 arguments.

enter image description here

To define "commandM" I used 4 other custom commands defined also with "newcommand". They are: "commandA" (with 9 parameters), "commandB" (with 6 parameters), "commandC" (with 6 parameters) and "commandD" (with 4 parameters).

But when I compile the LaTex code, Texmaker 5.0.2 compiles for several minutes and never finish processing the code. It seems to go into Looping. The same happened with TeXstudio 2.12.22. I also tried to compile on overleaf.com and the problem persisted.

The the mimimal exemple of problem is the LaTeX below. Try to make the smallest illustrative example of the problem that I could. But as you can see, it was long.

Question. How to define a custom command with parameters recursively from several other custom commands (each with several parameters) without the compilation problem described above and working normally?

Thanks in advance.

 \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}
10
  • 1
    the idea is to post a minimal example that shows the problem you shouldn't include unrelated packages longtable... it just makes it harder for people tracing the code to debug May 23 '20 at 20:22
  • @DavidCarlisle I hesitated whether to put all the LaTeX code here. But I think there may be a conflict with some package I'm using. So I understood that I must put it all here. May 23 '20 at 20:23
  • 2
    Is there any real advantage in defining a command with 25 arguments? How do you remember the role of each? What's your aim? It's probably solvable with a less barbaric method.
    – egreg
    May 23 '20 at 20:24
  • 2
    Why on earth do you want to define a command with 25 arguments? I normally stop at 4. May 23 '20 at 20:25
  • 1
    It seems that you want to solve an unknown problem (said it X) and think that the ideal solution will be Y(=nested command with endless arguments) so you ask here to solve Y, but probably your question will be more profitable explaining X and left people propose solutions for X, not for Y.
    – Fran
    May 23 '20 at 20:49
2

If you really want a command with 25 mandatory arguments (why not a more usable synatx like a comma separated list of arbitrary length) then you can do something like

enter image description here

\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}
3
  • if you need to nest calls to this then you would need to add some grouping so that the definitions of A... do not over-write each other. May 23 '20 at 20:40
  • Where is your favorite z:)? I mean \Az, \z, \zz etc.
    – wipet
    May 25 '20 at 19:14
  • @wipet every now and then I give Ulrike a chance. It's so hard to find z on a German keyboard:-) May 25 '20 at 19:17
1

You might want a friendlier syntax:

\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}

enter image description here

1
  • Truly a simple ...long code :-))) ahahah.
    – Sebastiano
    May 23 '20 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.