How to generate a matrix from an arbitrary sequence of arguments?












4














This answer shows me how I can handle an arbitrary number of arguments. I have adapted it a bit to my purpose:



usepackage{pgffor}
newcommand*{twolinematrix}[1]{%
foreach firstrowelement/secondrowelement in {#1} {%
<something should happen here!}%
}
}


The above macro should take input like this as an example: autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}.



I have become stuck because, normally, if you want to create a matrix (assuming you have amsmath loaded), you would so something like:



newcommand{manualtwolinematrix}[10]{%
begin{matrix}
#1 & #2 & #3 & #4 & #5 \
#6 & #7 & #8 & #9 & #10
end{matrix}%
}


So, this suggests that you'd need to have logic that knows when you are about to be done with the element pairs, because you have to put the \. I think if we are programmatically generating the matrix, it would be better to think of it like this:



newcommand{manualtwolinematrix}[10]{%
begin{matrix}
#1 & #2 & #3 & #4 & #5 \ #6 & #7 & #8 & #9 & #10
end{matrix}%
}


where #1 & #2 & #3 & #4 & #5 \ #6 & #7 & #8 & #9 & #10 is the string that has to be auto-generated within the body of our macro. Our macro gets input pairwise, so we have to generate this string given pairs. Okay, then in the body of our macro, we should have two variables: call it firstrow and secondrow.



The firstrow and secondrow should be built up sequentially, given each pair of elements, and then finally a variable called matrixbody should be generated like so:



matrixbody = firstrow \ secondrow



Can you help me put these thoughts together?










share|improve this question



























    4














    This answer shows me how I can handle an arbitrary number of arguments. I have adapted it a bit to my purpose:



    usepackage{pgffor}
    newcommand*{twolinematrix}[1]{%
    foreach firstrowelement/secondrowelement in {#1} {%
    <something should happen here!}%
    }
    }


    The above macro should take input like this as an example: autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}.



    I have become stuck because, normally, if you want to create a matrix (assuming you have amsmath loaded), you would so something like:



    newcommand{manualtwolinematrix}[10]{%
    begin{matrix}
    #1 & #2 & #3 & #4 & #5 \
    #6 & #7 & #8 & #9 & #10
    end{matrix}%
    }


    So, this suggests that you'd need to have logic that knows when you are about to be done with the element pairs, because you have to put the \. I think if we are programmatically generating the matrix, it would be better to think of it like this:



    newcommand{manualtwolinematrix}[10]{%
    begin{matrix}
    #1 & #2 & #3 & #4 & #5 \ #6 & #7 & #8 & #9 & #10
    end{matrix}%
    }


    where #1 & #2 & #3 & #4 & #5 \ #6 & #7 & #8 & #9 & #10 is the string that has to be auto-generated within the body of our macro. Our macro gets input pairwise, so we have to generate this string given pairs. Okay, then in the body of our macro, we should have two variables: call it firstrow and secondrow.



    The firstrow and secondrow should be built up sequentially, given each pair of elements, and then finally a variable called matrixbody should be generated like so:



    matrixbody = firstrow \ secondrow



    Can you help me put these thoughts together?










    share|improve this question

























      4












      4








      4


      2





      This answer shows me how I can handle an arbitrary number of arguments. I have adapted it a bit to my purpose:



      usepackage{pgffor}
      newcommand*{twolinematrix}[1]{%
      foreach firstrowelement/secondrowelement in {#1} {%
      <something should happen here!}%
      }
      }


      The above macro should take input like this as an example: autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}.



      I have become stuck because, normally, if you want to create a matrix (assuming you have amsmath loaded), you would so something like:



      newcommand{manualtwolinematrix}[10]{%
      begin{matrix}
      #1 & #2 & #3 & #4 & #5 \
      #6 & #7 & #8 & #9 & #10
      end{matrix}%
      }


      So, this suggests that you'd need to have logic that knows when you are about to be done with the element pairs, because you have to put the \. I think if we are programmatically generating the matrix, it would be better to think of it like this:



      newcommand{manualtwolinematrix}[10]{%
      begin{matrix}
      #1 & #2 & #3 & #4 & #5 \ #6 & #7 & #8 & #9 & #10
      end{matrix}%
      }


      where #1 & #2 & #3 & #4 & #5 \ #6 & #7 & #8 & #9 & #10 is the string that has to be auto-generated within the body of our macro. Our macro gets input pairwise, so we have to generate this string given pairs. Okay, then in the body of our macro, we should have two variables: call it firstrow and secondrow.



      The firstrow and secondrow should be built up sequentially, given each pair of elements, and then finally a variable called matrixbody should be generated like so:



      matrixbody = firstrow \ secondrow



      Can you help me put these thoughts together?










      share|improve this question













      This answer shows me how I can handle an arbitrary number of arguments. I have adapted it a bit to my purpose:



      usepackage{pgffor}
      newcommand*{twolinematrix}[1]{%
      foreach firstrowelement/secondrowelement in {#1} {%
      <something should happen here!}%
      }
      }


      The above macro should take input like this as an example: autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}.



      I have become stuck because, normally, if you want to create a matrix (assuming you have amsmath loaded), you would so something like:



      newcommand{manualtwolinematrix}[10]{%
      begin{matrix}
      #1 & #2 & #3 & #4 & #5 \
      #6 & #7 & #8 & #9 & #10
      end{matrix}%
      }


      So, this suggests that you'd need to have logic that knows when you are about to be done with the element pairs, because you have to put the \. I think if we are programmatically generating the matrix, it would be better to think of it like this:



      newcommand{manualtwolinematrix}[10]{%
      begin{matrix}
      #1 & #2 & #3 & #4 & #5 \ #6 & #7 & #8 & #9 & #10
      end{matrix}%
      }


      where #1 & #2 & #3 & #4 & #5 \ #6 & #7 & #8 & #9 & #10 is the string that has to be auto-generated within the body of our macro. Our macro gets input pairwise, so we have to generate this string given pairs. Okay, then in the body of our macro, we should have two variables: call it firstrow and secondrow.



      The firstrow and secondrow should be built up sequentially, given each pair of elements, and then finally a variable called matrixbody should be generated like so:



      matrixbody = firstrow \ secondrow



      Can you help me put these thoughts together?







      macros






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 24 '17 at 19:20









      user89user89

      1,5362933




      1,5362933






















          3 Answers
          3






          active

          oldest

          votes


















          4














          Perhaps less attractive than hand-made recursive macros, here is an implementation in expl3.



          I also added an optional argument (default empty) for producing different matrix types; the optional argument should be among p, b, B, v or V, for the corresponding type.



          documentclass{article}
          usepackage{amsmath}
          usepackage{xparse}

          setcounter{MaxMatrixCols}{20} % or maybe more

          ExplSyntaxOn
          NewDocumentCommand{twolinematrix}{O{}m}
          {
          twoline_matrix:nn { #1 } { #2 }
          }

          seq_new:N l__twoline_i_seq
          seq_new:N l__twoline_ii_seq

          cs_new_protected:Nn twoline_matrix:nn
          {
          seq_clear:N l__twoline_i_seq
          seq_clear:N l__twoline_ii_seq
          clist_map_function:nN { #2 } twoline_add:n
          begin{#1matrix}
          seq_use:Nn l__twoline_i_seq { & }
          \
          seq_use:Nn l__twoline_ii_seq { & }
          end{#1matrix}
          }
          cs_new_protected:Nn twoline_add:n
          {
          __twoline_add:w #1 q_stop
          }
          cs_new_protected:Npn __twoline_add:w #1/#2 q_stop
          {
          seq_put_right:Nn l__twoline_i_seq { #1 }
          seq_put_right:Nn l__twoline_ii_seq { #2 }
          }
          ExplSyntaxOff

          begin{document}

          [
          twolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}
          qquad
          twolinematrix[b]{1/6, 2/7, 3/8, 4/9, 5/10, 6/11, 7/12, 8/13, 9/14, 10/15}
          ]

          end{document}


          This isn't really so different from wipet's code.




          1. We clear the containers for the rows (here sequences)

          2. We map the input given as a comma separated list, calling the internal function __twoline_add:w that splits each item at the slash and adds the pieces to the sequences

          3. We use the sequences, placing & between items


          There is no need to do different things for the first item and the others, this is taken care of by seq_use:Nn, which only adds between items.



          The advantage over the seemingly more compact code is that we don't need to reinvent the wheel, but just to give an appropriate definition for the action on each item of the comma separated list.



          enter image description here



          For a deeper explanation of the code, see this: https://tug.org/TUGboat/tb39-1/tb121gregorio-expl3.pdf






          share|improve this answer























          • wow...look ma! latex3e!
            – user89
            Sep 25 '17 at 1:38



















          3














          For example, you can use this code:



          defaddto#1#2{expandafterdefexpandafter#1expandafter{#1#2}}
          defautotwolinematrix#1{deffirstrow{}defsecondrow{}atwolmA#1,/,}
          defatwolmA#1/#2,{ifx,#2,pmatrix{firstrowcrsecondrow}else
          ifxfirstrowempty deffirstrow{#1}defsecondrow{#2}else
          addtofirstrow{&#1}addtosecondrow{&#2}fi
          expandafter atwolmA fi
          }

          test:
          $$
          autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}
          $$
          bye


          First step: firstrow and secondrow are set in the loop while parameters are read. Second step: these macros are used in pmatrix.






          share|improve this answer





























            3














            Inside alignments it is best to use an expandable loop construct, so using expl3 or here I just code it directly.



            enter image description here



            documentclass{article}

            makeatletter
            defautotwolinematrix#1{%
            begin{bmatrix}%
            firstrow,#1,relax/%
            secondrow,#1,relax/%
            }
            deffirstrow#1,#2/{ifxrelax#2\elseifxrelax#1relaxelse&fi#2expandafterfirstrowfi}
            defsecondrow#1,#2/{ifxrelax#2#1end{bmatrix}else#1ifxrelax#1relaxelse&fiexpandaftersecondrowfi}
            makeatother
            usepackage{amsmath}

            begin{document}

            $autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}$
            end{document}





            share|improve this answer























            • Your second loop ends by ifxrelaxelse ...do somethingfi. This was not probably your intend... Moreover, there is empty column at the end of the matrix.
              – wipet
              Sep 24 '17 at 20:32










            • @wipet the ifx was OK I think but I did have an extra & as you said. Thanks. Fixed now (not as elegant as it was but I wanted to keep the expandable loop to keep the distinction between the answers:-) used bmatrix to make the column count clearer.
              – David Carlisle
              Sep 24 '17 at 21:33











            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "85"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f393053%2fhow-to-generate-a-matrix-from-an-arbitrary-sequence-of-arguments%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            Perhaps less attractive than hand-made recursive macros, here is an implementation in expl3.



            I also added an optional argument (default empty) for producing different matrix types; the optional argument should be among p, b, B, v or V, for the corresponding type.



            documentclass{article}
            usepackage{amsmath}
            usepackage{xparse}

            setcounter{MaxMatrixCols}{20} % or maybe more

            ExplSyntaxOn
            NewDocumentCommand{twolinematrix}{O{}m}
            {
            twoline_matrix:nn { #1 } { #2 }
            }

            seq_new:N l__twoline_i_seq
            seq_new:N l__twoline_ii_seq

            cs_new_protected:Nn twoline_matrix:nn
            {
            seq_clear:N l__twoline_i_seq
            seq_clear:N l__twoline_ii_seq
            clist_map_function:nN { #2 } twoline_add:n
            begin{#1matrix}
            seq_use:Nn l__twoline_i_seq { & }
            \
            seq_use:Nn l__twoline_ii_seq { & }
            end{#1matrix}
            }
            cs_new_protected:Nn twoline_add:n
            {
            __twoline_add:w #1 q_stop
            }
            cs_new_protected:Npn __twoline_add:w #1/#2 q_stop
            {
            seq_put_right:Nn l__twoline_i_seq { #1 }
            seq_put_right:Nn l__twoline_ii_seq { #2 }
            }
            ExplSyntaxOff

            begin{document}

            [
            twolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}
            qquad
            twolinematrix[b]{1/6, 2/7, 3/8, 4/9, 5/10, 6/11, 7/12, 8/13, 9/14, 10/15}
            ]

            end{document}


            This isn't really so different from wipet's code.




            1. We clear the containers for the rows (here sequences)

            2. We map the input given as a comma separated list, calling the internal function __twoline_add:w that splits each item at the slash and adds the pieces to the sequences

            3. We use the sequences, placing & between items


            There is no need to do different things for the first item and the others, this is taken care of by seq_use:Nn, which only adds between items.



            The advantage over the seemingly more compact code is that we don't need to reinvent the wheel, but just to give an appropriate definition for the action on each item of the comma separated list.



            enter image description here



            For a deeper explanation of the code, see this: https://tug.org/TUGboat/tb39-1/tb121gregorio-expl3.pdf






            share|improve this answer























            • wow...look ma! latex3e!
              – user89
              Sep 25 '17 at 1:38
















            4














            Perhaps less attractive than hand-made recursive macros, here is an implementation in expl3.



            I also added an optional argument (default empty) for producing different matrix types; the optional argument should be among p, b, B, v or V, for the corresponding type.



            documentclass{article}
            usepackage{amsmath}
            usepackage{xparse}

            setcounter{MaxMatrixCols}{20} % or maybe more

            ExplSyntaxOn
            NewDocumentCommand{twolinematrix}{O{}m}
            {
            twoline_matrix:nn { #1 } { #2 }
            }

            seq_new:N l__twoline_i_seq
            seq_new:N l__twoline_ii_seq

            cs_new_protected:Nn twoline_matrix:nn
            {
            seq_clear:N l__twoline_i_seq
            seq_clear:N l__twoline_ii_seq
            clist_map_function:nN { #2 } twoline_add:n
            begin{#1matrix}
            seq_use:Nn l__twoline_i_seq { & }
            \
            seq_use:Nn l__twoline_ii_seq { & }
            end{#1matrix}
            }
            cs_new_protected:Nn twoline_add:n
            {
            __twoline_add:w #1 q_stop
            }
            cs_new_protected:Npn __twoline_add:w #1/#2 q_stop
            {
            seq_put_right:Nn l__twoline_i_seq { #1 }
            seq_put_right:Nn l__twoline_ii_seq { #2 }
            }
            ExplSyntaxOff

            begin{document}

            [
            twolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}
            qquad
            twolinematrix[b]{1/6, 2/7, 3/8, 4/9, 5/10, 6/11, 7/12, 8/13, 9/14, 10/15}
            ]

            end{document}


            This isn't really so different from wipet's code.




            1. We clear the containers for the rows (here sequences)

            2. We map the input given as a comma separated list, calling the internal function __twoline_add:w that splits each item at the slash and adds the pieces to the sequences

            3. We use the sequences, placing & between items


            There is no need to do different things for the first item and the others, this is taken care of by seq_use:Nn, which only adds between items.



            The advantage over the seemingly more compact code is that we don't need to reinvent the wheel, but just to give an appropriate definition for the action on each item of the comma separated list.



            enter image description here



            For a deeper explanation of the code, see this: https://tug.org/TUGboat/tb39-1/tb121gregorio-expl3.pdf






            share|improve this answer























            • wow...look ma! latex3e!
              – user89
              Sep 25 '17 at 1:38














            4












            4








            4






            Perhaps less attractive than hand-made recursive macros, here is an implementation in expl3.



            I also added an optional argument (default empty) for producing different matrix types; the optional argument should be among p, b, B, v or V, for the corresponding type.



            documentclass{article}
            usepackage{amsmath}
            usepackage{xparse}

            setcounter{MaxMatrixCols}{20} % or maybe more

            ExplSyntaxOn
            NewDocumentCommand{twolinematrix}{O{}m}
            {
            twoline_matrix:nn { #1 } { #2 }
            }

            seq_new:N l__twoline_i_seq
            seq_new:N l__twoline_ii_seq

            cs_new_protected:Nn twoline_matrix:nn
            {
            seq_clear:N l__twoline_i_seq
            seq_clear:N l__twoline_ii_seq
            clist_map_function:nN { #2 } twoline_add:n
            begin{#1matrix}
            seq_use:Nn l__twoline_i_seq { & }
            \
            seq_use:Nn l__twoline_ii_seq { & }
            end{#1matrix}
            }
            cs_new_protected:Nn twoline_add:n
            {
            __twoline_add:w #1 q_stop
            }
            cs_new_protected:Npn __twoline_add:w #1/#2 q_stop
            {
            seq_put_right:Nn l__twoline_i_seq { #1 }
            seq_put_right:Nn l__twoline_ii_seq { #2 }
            }
            ExplSyntaxOff

            begin{document}

            [
            twolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}
            qquad
            twolinematrix[b]{1/6, 2/7, 3/8, 4/9, 5/10, 6/11, 7/12, 8/13, 9/14, 10/15}
            ]

            end{document}


            This isn't really so different from wipet's code.




            1. We clear the containers for the rows (here sequences)

            2. We map the input given as a comma separated list, calling the internal function __twoline_add:w that splits each item at the slash and adds the pieces to the sequences

            3. We use the sequences, placing & between items


            There is no need to do different things for the first item and the others, this is taken care of by seq_use:Nn, which only adds between items.



            The advantage over the seemingly more compact code is that we don't need to reinvent the wheel, but just to give an appropriate definition for the action on each item of the comma separated list.



            enter image description here



            For a deeper explanation of the code, see this: https://tug.org/TUGboat/tb39-1/tb121gregorio-expl3.pdf






            share|improve this answer














            Perhaps less attractive than hand-made recursive macros, here is an implementation in expl3.



            I also added an optional argument (default empty) for producing different matrix types; the optional argument should be among p, b, B, v or V, for the corresponding type.



            documentclass{article}
            usepackage{amsmath}
            usepackage{xparse}

            setcounter{MaxMatrixCols}{20} % or maybe more

            ExplSyntaxOn
            NewDocumentCommand{twolinematrix}{O{}m}
            {
            twoline_matrix:nn { #1 } { #2 }
            }

            seq_new:N l__twoline_i_seq
            seq_new:N l__twoline_ii_seq

            cs_new_protected:Nn twoline_matrix:nn
            {
            seq_clear:N l__twoline_i_seq
            seq_clear:N l__twoline_ii_seq
            clist_map_function:nN { #2 } twoline_add:n
            begin{#1matrix}
            seq_use:Nn l__twoline_i_seq { & }
            \
            seq_use:Nn l__twoline_ii_seq { & }
            end{#1matrix}
            }
            cs_new_protected:Nn twoline_add:n
            {
            __twoline_add:w #1 q_stop
            }
            cs_new_protected:Npn __twoline_add:w #1/#2 q_stop
            {
            seq_put_right:Nn l__twoline_i_seq { #1 }
            seq_put_right:Nn l__twoline_ii_seq { #2 }
            }
            ExplSyntaxOff

            begin{document}

            [
            twolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}
            qquad
            twolinematrix[b]{1/6, 2/7, 3/8, 4/9, 5/10, 6/11, 7/12, 8/13, 9/14, 10/15}
            ]

            end{document}


            This isn't really so different from wipet's code.




            1. We clear the containers for the rows (here sequences)

            2. We map the input given as a comma separated list, calling the internal function __twoline_add:w that splits each item at the slash and adds the pieces to the sequences

            3. We use the sequences, placing & between items


            There is no need to do different things for the first item and the others, this is taken care of by seq_use:Nn, which only adds between items.



            The advantage over the seemingly more compact code is that we don't need to reinvent the wheel, but just to give an appropriate definition for the action on each item of the comma separated list.



            enter image description here



            For a deeper explanation of the code, see this: https://tug.org/TUGboat/tb39-1/tb121gregorio-expl3.pdf







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 mins ago









            user89

            1,5362933




            1,5362933










            answered Sep 24 '17 at 20:56









            egregegreg

            711k8618893173




            711k8618893173












            • wow...look ma! latex3e!
              – user89
              Sep 25 '17 at 1:38


















            • wow...look ma! latex3e!
              – user89
              Sep 25 '17 at 1:38
















            wow...look ma! latex3e!
            – user89
            Sep 25 '17 at 1:38




            wow...look ma! latex3e!
            – user89
            Sep 25 '17 at 1:38











            3














            For example, you can use this code:



            defaddto#1#2{expandafterdefexpandafter#1expandafter{#1#2}}
            defautotwolinematrix#1{deffirstrow{}defsecondrow{}atwolmA#1,/,}
            defatwolmA#1/#2,{ifx,#2,pmatrix{firstrowcrsecondrow}else
            ifxfirstrowempty deffirstrow{#1}defsecondrow{#2}else
            addtofirstrow{&#1}addtosecondrow{&#2}fi
            expandafter atwolmA fi
            }

            test:
            $$
            autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}
            $$
            bye


            First step: firstrow and secondrow are set in the loop while parameters are read. Second step: these macros are used in pmatrix.






            share|improve this answer


























              3














              For example, you can use this code:



              defaddto#1#2{expandafterdefexpandafter#1expandafter{#1#2}}
              defautotwolinematrix#1{deffirstrow{}defsecondrow{}atwolmA#1,/,}
              defatwolmA#1/#2,{ifx,#2,pmatrix{firstrowcrsecondrow}else
              ifxfirstrowempty deffirstrow{#1}defsecondrow{#2}else
              addtofirstrow{&#1}addtosecondrow{&#2}fi
              expandafter atwolmA fi
              }

              test:
              $$
              autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}
              $$
              bye


              First step: firstrow and secondrow are set in the loop while parameters are read. Second step: these macros are used in pmatrix.






              share|improve this answer
























                3












                3








                3






                For example, you can use this code:



                defaddto#1#2{expandafterdefexpandafter#1expandafter{#1#2}}
                defautotwolinematrix#1{deffirstrow{}defsecondrow{}atwolmA#1,/,}
                defatwolmA#1/#2,{ifx,#2,pmatrix{firstrowcrsecondrow}else
                ifxfirstrowempty deffirstrow{#1}defsecondrow{#2}else
                addtofirstrow{&#1}addtosecondrow{&#2}fi
                expandafter atwolmA fi
                }

                test:
                $$
                autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}
                $$
                bye


                First step: firstrow and secondrow are set in the loop while parameters are read. Second step: these macros are used in pmatrix.






                share|improve this answer












                For example, you can use this code:



                defaddto#1#2{expandafterdefexpandafter#1expandafter{#1#2}}
                defautotwolinematrix#1{deffirstrow{}defsecondrow{}atwolmA#1,/,}
                defatwolmA#1/#2,{ifx,#2,pmatrix{firstrowcrsecondrow}else
                ifxfirstrowempty deffirstrow{#1}defsecondrow{#2}else
                addtofirstrow{&#1}addtosecondrow{&#2}fi
                expandafter atwolmA fi
                }

                test:
                $$
                autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}
                $$
                bye


                First step: firstrow and secondrow are set in the loop while parameters are read. Second step: these macros are used in pmatrix.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 24 '17 at 20:20









                wipetwipet

                35k4881




                35k4881























                    3














                    Inside alignments it is best to use an expandable loop construct, so using expl3 or here I just code it directly.



                    enter image description here



                    documentclass{article}

                    makeatletter
                    defautotwolinematrix#1{%
                    begin{bmatrix}%
                    firstrow,#1,relax/%
                    secondrow,#1,relax/%
                    }
                    deffirstrow#1,#2/{ifxrelax#2\elseifxrelax#1relaxelse&fi#2expandafterfirstrowfi}
                    defsecondrow#1,#2/{ifxrelax#2#1end{bmatrix}else#1ifxrelax#1relaxelse&fiexpandaftersecondrowfi}
                    makeatother
                    usepackage{amsmath}

                    begin{document}

                    $autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}$
                    end{document}





                    share|improve this answer























                    • Your second loop ends by ifxrelaxelse ...do somethingfi. This was not probably your intend... Moreover, there is empty column at the end of the matrix.
                      – wipet
                      Sep 24 '17 at 20:32










                    • @wipet the ifx was OK I think but I did have an extra & as you said. Thanks. Fixed now (not as elegant as it was but I wanted to keep the expandable loop to keep the distinction between the answers:-) used bmatrix to make the column count clearer.
                      – David Carlisle
                      Sep 24 '17 at 21:33
















                    3














                    Inside alignments it is best to use an expandable loop construct, so using expl3 or here I just code it directly.



                    enter image description here



                    documentclass{article}

                    makeatletter
                    defautotwolinematrix#1{%
                    begin{bmatrix}%
                    firstrow,#1,relax/%
                    secondrow,#1,relax/%
                    }
                    deffirstrow#1,#2/{ifxrelax#2\elseifxrelax#1relaxelse&fi#2expandafterfirstrowfi}
                    defsecondrow#1,#2/{ifxrelax#2#1end{bmatrix}else#1ifxrelax#1relaxelse&fiexpandaftersecondrowfi}
                    makeatother
                    usepackage{amsmath}

                    begin{document}

                    $autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}$
                    end{document}





                    share|improve this answer























                    • Your second loop ends by ifxrelaxelse ...do somethingfi. This was not probably your intend... Moreover, there is empty column at the end of the matrix.
                      – wipet
                      Sep 24 '17 at 20:32










                    • @wipet the ifx was OK I think but I did have an extra & as you said. Thanks. Fixed now (not as elegant as it was but I wanted to keep the expandable loop to keep the distinction between the answers:-) used bmatrix to make the column count clearer.
                      – David Carlisle
                      Sep 24 '17 at 21:33














                    3












                    3








                    3






                    Inside alignments it is best to use an expandable loop construct, so using expl3 or here I just code it directly.



                    enter image description here



                    documentclass{article}

                    makeatletter
                    defautotwolinematrix#1{%
                    begin{bmatrix}%
                    firstrow,#1,relax/%
                    secondrow,#1,relax/%
                    }
                    deffirstrow#1,#2/{ifxrelax#2\elseifxrelax#1relaxelse&fi#2expandafterfirstrowfi}
                    defsecondrow#1,#2/{ifxrelax#2#1end{bmatrix}else#1ifxrelax#1relaxelse&fiexpandaftersecondrowfi}
                    makeatother
                    usepackage{amsmath}

                    begin{document}

                    $autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}$
                    end{document}





                    share|improve this answer














                    Inside alignments it is best to use an expandable loop construct, so using expl3 or here I just code it directly.



                    enter image description here



                    documentclass{article}

                    makeatletter
                    defautotwolinematrix#1{%
                    begin{bmatrix}%
                    firstrow,#1,relax/%
                    secondrow,#1,relax/%
                    }
                    deffirstrow#1,#2/{ifxrelax#2\elseifxrelax#1relaxelse&fi#2expandafterfirstrowfi}
                    defsecondrow#1,#2/{ifxrelax#2#1end{bmatrix}else#1ifxrelax#1relaxelse&fiexpandaftersecondrowfi}
                    makeatother
                    usepackage{amsmath}

                    begin{document}

                    $autotwolinematrix{1/6, 2/7, 3/8, 4/9, 5/10}$
                    end{document}






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 24 '17 at 21:30

























                    answered Sep 24 '17 at 20:19









                    David CarlisleDavid Carlisle

                    484k4011171858




                    484k4011171858












                    • Your second loop ends by ifxrelaxelse ...do somethingfi. This was not probably your intend... Moreover, there is empty column at the end of the matrix.
                      – wipet
                      Sep 24 '17 at 20:32










                    • @wipet the ifx was OK I think but I did have an extra & as you said. Thanks. Fixed now (not as elegant as it was but I wanted to keep the expandable loop to keep the distinction between the answers:-) used bmatrix to make the column count clearer.
                      – David Carlisle
                      Sep 24 '17 at 21:33


















                    • Your second loop ends by ifxrelaxelse ...do somethingfi. This was not probably your intend... Moreover, there is empty column at the end of the matrix.
                      – wipet
                      Sep 24 '17 at 20:32










                    • @wipet the ifx was OK I think but I did have an extra & as you said. Thanks. Fixed now (not as elegant as it was but I wanted to keep the expandable loop to keep the distinction between the answers:-) used bmatrix to make the column count clearer.
                      – David Carlisle
                      Sep 24 '17 at 21:33
















                    Your second loop ends by ifxrelaxelse ...do somethingfi. This was not probably your intend... Moreover, there is empty column at the end of the matrix.
                    – wipet
                    Sep 24 '17 at 20:32




                    Your second loop ends by ifxrelaxelse ...do somethingfi. This was not probably your intend... Moreover, there is empty column at the end of the matrix.
                    – wipet
                    Sep 24 '17 at 20:32












                    @wipet the ifx was OK I think but I did have an extra & as you said. Thanks. Fixed now (not as elegant as it was but I wanted to keep the expandable loop to keep the distinction between the answers:-) used bmatrix to make the column count clearer.
                    – David Carlisle
                    Sep 24 '17 at 21:33




                    @wipet the ifx was OK I think but I did have an extra & as you said. Thanks. Fixed now (not as elegant as it was but I wanted to keep the expandable loop to keep the distinction between the answers:-) used bmatrix to make the column count clearer.
                    – David Carlisle
                    Sep 24 '17 at 21:33


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f393053%2fhow-to-generate-a-matrix-from-an-arbitrary-sequence-of-arguments%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Accessing regular linux commands in Huawei's Dopra Linux

                    Can't connect RFCOMM socket: Host is down

                    Kernel panic - not syncing: Fatal Exception in Interrupt