TikZ matrix, style for combination or rows and columnsIs there any efficient way to add cell identifier to...

What should tie a collection of short-stories together?

Why does Bach not break the rules here?

What options are left, if Britain cannot decide?

Why is the BSI not using powers of two?

A limit with limit zero everywhere must be zero somewhere

How to change two letters closest to a string and one letter immediately after a string using notepad++

How Could an Airship Be Repaired Mid-Flight

How do I hide Chekhov's Gun?

Life insurance that covers only simultaneous/dual deaths

What is this large pipe coming out of my roof?

Happy pi day, everyone!

Who is flying the vertibirds?

My adviser wants to be the first author

Are there other languages, besides English, where the indefinite (or definite) article varies based on sound?

The difference between「N分で」and「後N分で」

Instead of Universal Basic Income, why not Universal Basic NEEDS?

Employee lack of ownership

Do the common programs (for example: "ls", "cat") in Linux and BSD come from the same source code?

Why do passenger jet manufacturers design their planes with stall prevention systems?

Gravity magic - How does it work?

Existence of subset with given Hausdorff dimension

Is it possible to upcast ritual spells?

Do I need life insurance if I can cover my own funeral costs?

A sequence that has integer values for prime indexes only:



TikZ matrix, style for combination or rows and columns


Is there any efficient way to add cell identifier to each cell in TikZ?How to have a specific style for empty cells in a tikz matrixIs it possible to add node labels to some matrix column or row when building it?TikZ Colorize a matrixThe simplest way to fill a matrix of nodes?Scaled TikZ matrix of nodesHow to overalap textbox in tikz?Why does a definition of macro containing multi raster column n/.style fail in tcbset of tcolorboxHow to annotate and shade a matrixIntersection of paths with constructed namesHow do I encircle a partial table in tikz?tikz matrix align/decorate cellpicturesNumerical conditional within tikz keys?how can I create keys such as “column … to …” for a TikZ matrixWhy do I get an extra white page before my TikZ picture?Table-like lines in tikz matrixStriking out matrix lines and columns: is Tikz the only way?How to draw a square and its diagonals with arrows?Limiting the scope of styles in TikZ matrix of nodes













11















In the TikZ manual we have the example below that shows how to apply an style to an entry of the matrix. It is also easy to apply it for an entire row and an entire column. My question is how can we achieve the same for several entries? Is there a simple way to specify a style, say for example, rows 1 & 3 and columns 1 & 2?



documentclass{beamer}
usepackage{tikz}
usetikzlibrary{matrix}
begin{document}
begin{tikzpicture}[row 2 column 3/.style=red]
matrix [matrix of nodes]
{
8 & 1 & 6 \
3 & 5 & 7 \
4 & 9 & 2 \
};
end{tikzpicture}
end{document}









share|improve this question




















  • 1





    How simple? [row 1/.style=red, row 3/.style=red]

    – John Kormylo
    May 6 '14 at 13:32











  • @JohnKormylo: Thanks but say the style is very long to type, is there a way to make it even simpler? Something like: row 1,3 /.style=... ?

    – Sergio Parreiras
    May 6 '14 at 15:57













  • You could create a new command, but it might get ugly.

    – John Kormylo
    May 6 '14 at 23:34
















11















In the TikZ manual we have the example below that shows how to apply an style to an entry of the matrix. It is also easy to apply it for an entire row and an entire column. My question is how can we achieve the same for several entries? Is there a simple way to specify a style, say for example, rows 1 & 3 and columns 1 & 2?



documentclass{beamer}
usepackage{tikz}
usetikzlibrary{matrix}
begin{document}
begin{tikzpicture}[row 2 column 3/.style=red]
matrix [matrix of nodes]
{
8 & 1 & 6 \
3 & 5 & 7 \
4 & 9 & 2 \
};
end{tikzpicture}
end{document}









share|improve this question




















  • 1





    How simple? [row 1/.style=red, row 3/.style=red]

    – John Kormylo
    May 6 '14 at 13:32











  • @JohnKormylo: Thanks but say the style is very long to type, is there a way to make it even simpler? Something like: row 1,3 /.style=... ?

    – Sergio Parreiras
    May 6 '14 at 15:57













  • You could create a new command, but it might get ugly.

    – John Kormylo
    May 6 '14 at 23:34














11












11








11


4






In the TikZ manual we have the example below that shows how to apply an style to an entry of the matrix. It is also easy to apply it for an entire row and an entire column. My question is how can we achieve the same for several entries? Is there a simple way to specify a style, say for example, rows 1 & 3 and columns 1 & 2?



documentclass{beamer}
usepackage{tikz}
usetikzlibrary{matrix}
begin{document}
begin{tikzpicture}[row 2 column 3/.style=red]
matrix [matrix of nodes]
{
8 & 1 & 6 \
3 & 5 & 7 \
4 & 9 & 2 \
};
end{tikzpicture}
end{document}









share|improve this question
















In the TikZ manual we have the example below that shows how to apply an style to an entry of the matrix. It is also easy to apply it for an entire row and an entire column. My question is how can we achieve the same for several entries? Is there a simple way to specify a style, say for example, rows 1 & 3 and columns 1 & 2?



documentclass{beamer}
usepackage{tikz}
usetikzlibrary{matrix}
begin{document}
begin{tikzpicture}[row 2 column 3/.style=red]
matrix [matrix of nodes]
{
8 & 1 & 6 \
3 & 5 & 7 \
4 & 9 & 2 \
};
end{tikzpicture}
end{document}






tikz-pgf tikz-matrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 7 '14 at 8:14







Sergio Parreiras

















asked May 6 '14 at 12:31









Sergio ParreirasSergio Parreiras

6681825




6681825








  • 1





    How simple? [row 1/.style=red, row 3/.style=red]

    – John Kormylo
    May 6 '14 at 13:32











  • @JohnKormylo: Thanks but say the style is very long to type, is there a way to make it even simpler? Something like: row 1,3 /.style=... ?

    – Sergio Parreiras
    May 6 '14 at 15:57













  • You could create a new command, but it might get ugly.

    – John Kormylo
    May 6 '14 at 23:34














  • 1





    How simple? [row 1/.style=red, row 3/.style=red]

    – John Kormylo
    May 6 '14 at 13:32











  • @JohnKormylo: Thanks but say the style is very long to type, is there a way to make it even simpler? Something like: row 1,3 /.style=... ?

    – Sergio Parreiras
    May 6 '14 at 15:57













  • You could create a new command, but it might get ugly.

    – John Kormylo
    May 6 '14 at 23:34








1




1





How simple? [row 1/.style=red, row 3/.style=red]

– John Kormylo
May 6 '14 at 13:32





How simple? [row 1/.style=red, row 3/.style=red]

– John Kormylo
May 6 '14 at 13:32













@JohnKormylo: Thanks but say the style is very long to type, is there a way to make it even simpler? Something like: row 1,3 /.style=... ?

– Sergio Parreiras
May 6 '14 at 15:57







@JohnKormylo: Thanks but say the style is very long to type, is there a way to make it even simpler? Something like: row 1,3 /.style=... ?

– Sergio Parreiras
May 6 '14 at 15:57















You could create a new command, but it might get ugly.

– John Kormylo
May 6 '14 at 23:34





You could create a new command, but it might get ugly.

– John Kormylo
May 6 '14 at 23:34










5 Answers
5






active

oldest

votes


















14














You can use the /.list feature John Kormylo mentioned in his answer to repeat a style for different inputs.



documentclass{article}
usepackage{tikz}
usetikzlibrary{matrix}
begin{document}
tikzset{myset/.style={row 2 column #1/.style={nodes={text=red}}}}
begin{tikzpicture}[myset/.list={1,3,5}]
matrix [matrix of nodes]
{
8 & 1 & 6 & 8 & 1 & 6 \
3 & 5 & 7 & 3 & 5 & 7 \
4 & 9 & 2 & 4 & 9 & 2 \
};
end{tikzpicture}
end{document}


enter image description here






share|improve this answer
























  • list will be very useful for other projects, thanks! By any chance, besides the manual do you have any suggestions on references for TikZ programming?

    – Sergio Parreiras
    May 7 '14 at 8:32






  • 1





    @SergioParreiras Manual is very very good. I would say answer questions here :P It gives you a lot of practice and gives a chance to see others do their black magic.

    – percusse
    May 7 '14 at 8:33



















8














More a long comment than a real answer... Based on percusse's answer, this style allows to specify both row and column.



documentclass{standalone}
usepackage{tikz}
usetikzlibrary{matrix}
begin{document}
tikzset{myset/.style args = {(#1,#2)}{%
row #1 column #2/.style={nodes={text=red}}}}
begin{tikzpicture}[myset/.list={(2,1),(3,3),(1,5)}]
matrix [matrix of nodes]
{
8 & 1 & 6 & 8 & 1 & 6 \
3 & 5 & 7 & 3 & 5 & 7 \
4 & 9 & 2 & 4 & 9 & 2 \
};
end{tikzpicture}
end{document}


enter image description here






share|improve this answer

































    7














    This can be done via foreach indeed, but since the keys set with pgfkeys are not global, their assignment are lost when the loop is exited. Unless you mess with globaldef, as in the following example:



    documentclass{article}
    usepackage{tikz}
    usetikzlibrary{matrix}
    begin{document}

    newcommand{rows}[2]{% #1 = rows, #2 = style
    foreach r in {#1} {%
    globaldefs=1relax
    tikzset{row r/.style={#2}}
    }%
    }

    newcommand{cols}[2]{% #1 = columns, #2 = style
    foreach r in {#1} {%
    globaldefs=1relax
    tikzset{column r/.style={#2}}
    }%
    }

    begin{tikzpicture}
    cols{1,3}{blue}
    rows{1,3}{red}
    matrix [matrix of nodes]
    {
    8 & 1 & 6 \
    3 & 5 & 7 \
    4 & 9 & 2 \
    };
    end{tikzpicture}
    end{document}


    Result






    share|improve this answer
























    • Consider me considerably less fuzzy now.

      – John Kormylo
      May 7 '14 at 14:06



















    2














    Interestingly, you cannot do this using foreach. I understand this is because foreach is actually implemented using pgfkeys using the /.list={...} feature, but I'm a bit fuzzy on the details.



    documentclass{standalone}
    usepackage{tikz}
    usetikzlibrary{matrix}

    newcommand{row}[2]{% #1 = row, #2 = style
    tikzset{row #1/.style={#2}}}

    newcommand{col}[2]{% #1 = column, #2 = style
    tikzset{column #1/.style={#2}}}

    begin{document}
    begin{tikzpicture}
    row{1}{red}
    row{3}{red}
    col{1}{blue}
    col{3}{blue}
    matrix [matrix of nodes]
    {
    8 & 1 & 6 \
    3 & 5 & 7 \
    4 & 9 & 2 \
    };
    end{tikzpicture}
    end{document}





    share|improve this answer































      0














      I reopen this old questions because, from percusse's and cjorssen's answers, I managed to assign style to a block of cells only specifying its (top,left to bottom,right) coordinates. I share here my solution, hoping it will help someone else.



      documentclass{standalone}
      usepackage{tikz}
      usetikzlibrary{matrix}
      begin{document}
      tikzset{myblocks/.style args = {(#1,#2 to #3,#4)}{
      blockrows/.style={
      block/.style={
      row ##1 column ####1/.style={nodes={text=red}}
      },
      block/.list={#2,...,#4}
      },
      blockrows/.list={#1,...,#3}
      }
      }
      begin{tikzpicture}[myblocks/.list={(1,1 to 2,2),(1,4 to 3,5)}]
      matrix [matrix of nodes]
      {
      8 & 1 & 6 & 8 & 1 & 6 \
      3 & 5 & 7 & 3 & 5 & 7 \
      4 & 9 & 2 & 4 & 9 & 2 \
      };
      end{tikzpicture}
      end{document}


      enter image description here






      share|improve this answer

























        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%2f175616%2ftikz-matrix-style-for-combination-or-rows-and-columns%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        14














        You can use the /.list feature John Kormylo mentioned in his answer to repeat a style for different inputs.



        documentclass{article}
        usepackage{tikz}
        usetikzlibrary{matrix}
        begin{document}
        tikzset{myset/.style={row 2 column #1/.style={nodes={text=red}}}}
        begin{tikzpicture}[myset/.list={1,3,5}]
        matrix [matrix of nodes]
        {
        8 & 1 & 6 & 8 & 1 & 6 \
        3 & 5 & 7 & 3 & 5 & 7 \
        4 & 9 & 2 & 4 & 9 & 2 \
        };
        end{tikzpicture}
        end{document}


        enter image description here






        share|improve this answer
























        • list will be very useful for other projects, thanks! By any chance, besides the manual do you have any suggestions on references for TikZ programming?

          – Sergio Parreiras
          May 7 '14 at 8:32






        • 1





          @SergioParreiras Manual is very very good. I would say answer questions here :P It gives you a lot of practice and gives a chance to see others do their black magic.

          – percusse
          May 7 '14 at 8:33
















        14














        You can use the /.list feature John Kormylo mentioned in his answer to repeat a style for different inputs.



        documentclass{article}
        usepackage{tikz}
        usetikzlibrary{matrix}
        begin{document}
        tikzset{myset/.style={row 2 column #1/.style={nodes={text=red}}}}
        begin{tikzpicture}[myset/.list={1,3,5}]
        matrix [matrix of nodes]
        {
        8 & 1 & 6 & 8 & 1 & 6 \
        3 & 5 & 7 & 3 & 5 & 7 \
        4 & 9 & 2 & 4 & 9 & 2 \
        };
        end{tikzpicture}
        end{document}


        enter image description here






        share|improve this answer
























        • list will be very useful for other projects, thanks! By any chance, besides the manual do you have any suggestions on references for TikZ programming?

          – Sergio Parreiras
          May 7 '14 at 8:32






        • 1





          @SergioParreiras Manual is very very good. I would say answer questions here :P It gives you a lot of practice and gives a chance to see others do their black magic.

          – percusse
          May 7 '14 at 8:33














        14












        14








        14







        You can use the /.list feature John Kormylo mentioned in his answer to repeat a style for different inputs.



        documentclass{article}
        usepackage{tikz}
        usetikzlibrary{matrix}
        begin{document}
        tikzset{myset/.style={row 2 column #1/.style={nodes={text=red}}}}
        begin{tikzpicture}[myset/.list={1,3,5}]
        matrix [matrix of nodes]
        {
        8 & 1 & 6 & 8 & 1 & 6 \
        3 & 5 & 7 & 3 & 5 & 7 \
        4 & 9 & 2 & 4 & 9 & 2 \
        };
        end{tikzpicture}
        end{document}


        enter image description here






        share|improve this answer













        You can use the /.list feature John Kormylo mentioned in his answer to repeat a style for different inputs.



        documentclass{article}
        usepackage{tikz}
        usetikzlibrary{matrix}
        begin{document}
        tikzset{myset/.style={row 2 column #1/.style={nodes={text=red}}}}
        begin{tikzpicture}[myset/.list={1,3,5}]
        matrix [matrix of nodes]
        {
        8 & 1 & 6 & 8 & 1 & 6 \
        3 & 5 & 7 & 3 & 5 & 7 \
        4 & 9 & 2 & 4 & 9 & 2 \
        };
        end{tikzpicture}
        end{document}


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 7 '14 at 8:23









        percussepercusse

        138k14258496




        138k14258496













        • list will be very useful for other projects, thanks! By any chance, besides the manual do you have any suggestions on references for TikZ programming?

          – Sergio Parreiras
          May 7 '14 at 8:32






        • 1





          @SergioParreiras Manual is very very good. I would say answer questions here :P It gives you a lot of practice and gives a chance to see others do their black magic.

          – percusse
          May 7 '14 at 8:33



















        • list will be very useful for other projects, thanks! By any chance, besides the manual do you have any suggestions on references for TikZ programming?

          – Sergio Parreiras
          May 7 '14 at 8:32






        • 1





          @SergioParreiras Manual is very very good. I would say answer questions here :P It gives you a lot of practice and gives a chance to see others do their black magic.

          – percusse
          May 7 '14 at 8:33

















        list will be very useful for other projects, thanks! By any chance, besides the manual do you have any suggestions on references for TikZ programming?

        – Sergio Parreiras
        May 7 '14 at 8:32





        list will be very useful for other projects, thanks! By any chance, besides the manual do you have any suggestions on references for TikZ programming?

        – Sergio Parreiras
        May 7 '14 at 8:32




        1




        1





        @SergioParreiras Manual is very very good. I would say answer questions here :P It gives you a lot of practice and gives a chance to see others do their black magic.

        – percusse
        May 7 '14 at 8:33





        @SergioParreiras Manual is very very good. I would say answer questions here :P It gives you a lot of practice and gives a chance to see others do their black magic.

        – percusse
        May 7 '14 at 8:33











        8














        More a long comment than a real answer... Based on percusse's answer, this style allows to specify both row and column.



        documentclass{standalone}
        usepackage{tikz}
        usetikzlibrary{matrix}
        begin{document}
        tikzset{myset/.style args = {(#1,#2)}{%
        row #1 column #2/.style={nodes={text=red}}}}
        begin{tikzpicture}[myset/.list={(2,1),(3,3),(1,5)}]
        matrix [matrix of nodes]
        {
        8 & 1 & 6 & 8 & 1 & 6 \
        3 & 5 & 7 & 3 & 5 & 7 \
        4 & 9 & 2 & 4 & 9 & 2 \
        };
        end{tikzpicture}
        end{document}


        enter image description here






        share|improve this answer






























          8














          More a long comment than a real answer... Based on percusse's answer, this style allows to specify both row and column.



          documentclass{standalone}
          usepackage{tikz}
          usetikzlibrary{matrix}
          begin{document}
          tikzset{myset/.style args = {(#1,#2)}{%
          row #1 column #2/.style={nodes={text=red}}}}
          begin{tikzpicture}[myset/.list={(2,1),(3,3),(1,5)}]
          matrix [matrix of nodes]
          {
          8 & 1 & 6 & 8 & 1 & 6 \
          3 & 5 & 7 & 3 & 5 & 7 \
          4 & 9 & 2 & 4 & 9 & 2 \
          };
          end{tikzpicture}
          end{document}


          enter image description here






          share|improve this answer




























            8












            8








            8







            More a long comment than a real answer... Based on percusse's answer, this style allows to specify both row and column.



            documentclass{standalone}
            usepackage{tikz}
            usetikzlibrary{matrix}
            begin{document}
            tikzset{myset/.style args = {(#1,#2)}{%
            row #1 column #2/.style={nodes={text=red}}}}
            begin{tikzpicture}[myset/.list={(2,1),(3,3),(1,5)}]
            matrix [matrix of nodes]
            {
            8 & 1 & 6 & 8 & 1 & 6 \
            3 & 5 & 7 & 3 & 5 & 7 \
            4 & 9 & 2 & 4 & 9 & 2 \
            };
            end{tikzpicture}
            end{document}


            enter image description here






            share|improve this answer















            More a long comment than a real answer... Based on percusse's answer, this style allows to specify both row and column.



            documentclass{standalone}
            usepackage{tikz}
            usetikzlibrary{matrix}
            begin{document}
            tikzset{myset/.style args = {(#1,#2)}{%
            row #1 column #2/.style={nodes={text=red}}}}
            begin{tikzpicture}[myset/.list={(2,1),(3,3),(1,5)}]
            matrix [matrix of nodes]
            {
            8 & 1 & 6 & 8 & 1 & 6 \
            3 & 5 & 7 & 3 & 5 & 7 \
            4 & 9 & 2 & 4 & 9 & 2 \
            };
            end{tikzpicture}
            end{document}


            enter image description here







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 13 '17 at 12:35









            Community

            1




            1










            answered May 7 '14 at 9:09









            cjorssencjorssen

            5,646225104




            5,646225104























                7














                This can be done via foreach indeed, but since the keys set with pgfkeys are not global, their assignment are lost when the loop is exited. Unless you mess with globaldef, as in the following example:



                documentclass{article}
                usepackage{tikz}
                usetikzlibrary{matrix}
                begin{document}

                newcommand{rows}[2]{% #1 = rows, #2 = style
                foreach r in {#1} {%
                globaldefs=1relax
                tikzset{row r/.style={#2}}
                }%
                }

                newcommand{cols}[2]{% #1 = columns, #2 = style
                foreach r in {#1} {%
                globaldefs=1relax
                tikzset{column r/.style={#2}}
                }%
                }

                begin{tikzpicture}
                cols{1,3}{blue}
                rows{1,3}{red}
                matrix [matrix of nodes]
                {
                8 & 1 & 6 \
                3 & 5 & 7 \
                4 & 9 & 2 \
                };
                end{tikzpicture}
                end{document}


                Result






                share|improve this answer
























                • Consider me considerably less fuzzy now.

                  – John Kormylo
                  May 7 '14 at 14:06
















                7














                This can be done via foreach indeed, but since the keys set with pgfkeys are not global, their assignment are lost when the loop is exited. Unless you mess with globaldef, as in the following example:



                documentclass{article}
                usepackage{tikz}
                usetikzlibrary{matrix}
                begin{document}

                newcommand{rows}[2]{% #1 = rows, #2 = style
                foreach r in {#1} {%
                globaldefs=1relax
                tikzset{row r/.style={#2}}
                }%
                }

                newcommand{cols}[2]{% #1 = columns, #2 = style
                foreach r in {#1} {%
                globaldefs=1relax
                tikzset{column r/.style={#2}}
                }%
                }

                begin{tikzpicture}
                cols{1,3}{blue}
                rows{1,3}{red}
                matrix [matrix of nodes]
                {
                8 & 1 & 6 \
                3 & 5 & 7 \
                4 & 9 & 2 \
                };
                end{tikzpicture}
                end{document}


                Result






                share|improve this answer
























                • Consider me considerably less fuzzy now.

                  – John Kormylo
                  May 7 '14 at 14:06














                7












                7








                7







                This can be done via foreach indeed, but since the keys set with pgfkeys are not global, their assignment are lost when the loop is exited. Unless you mess with globaldef, as in the following example:



                documentclass{article}
                usepackage{tikz}
                usetikzlibrary{matrix}
                begin{document}

                newcommand{rows}[2]{% #1 = rows, #2 = style
                foreach r in {#1} {%
                globaldefs=1relax
                tikzset{row r/.style={#2}}
                }%
                }

                newcommand{cols}[2]{% #1 = columns, #2 = style
                foreach r in {#1} {%
                globaldefs=1relax
                tikzset{column r/.style={#2}}
                }%
                }

                begin{tikzpicture}
                cols{1,3}{blue}
                rows{1,3}{red}
                matrix [matrix of nodes]
                {
                8 & 1 & 6 \
                3 & 5 & 7 \
                4 & 9 & 2 \
                };
                end{tikzpicture}
                end{document}


                Result






                share|improve this answer













                This can be done via foreach indeed, but since the keys set with pgfkeys are not global, their assignment are lost when the loop is exited. Unless you mess with globaldef, as in the following example:



                documentclass{article}
                usepackage{tikz}
                usetikzlibrary{matrix}
                begin{document}

                newcommand{rows}[2]{% #1 = rows, #2 = style
                foreach r in {#1} {%
                globaldefs=1relax
                tikzset{row r/.style={#2}}
                }%
                }

                newcommand{cols}[2]{% #1 = columns, #2 = style
                foreach r in {#1} {%
                globaldefs=1relax
                tikzset{column r/.style={#2}}
                }%
                }

                begin{tikzpicture}
                cols{1,3}{blue}
                rows{1,3}{red}
                matrix [matrix of nodes]
                {
                8 & 1 & 6 \
                3 & 5 & 7 \
                4 & 9 & 2 \
                };
                end{tikzpicture}
                end{document}


                Result







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 7 '14 at 8:36









                JLDiazJLDiaz

                50.1k4123186




                50.1k4123186













                • Consider me considerably less fuzzy now.

                  – John Kormylo
                  May 7 '14 at 14:06



















                • Consider me considerably less fuzzy now.

                  – John Kormylo
                  May 7 '14 at 14:06

















                Consider me considerably less fuzzy now.

                – John Kormylo
                May 7 '14 at 14:06





                Consider me considerably less fuzzy now.

                – John Kormylo
                May 7 '14 at 14:06











                2














                Interestingly, you cannot do this using foreach. I understand this is because foreach is actually implemented using pgfkeys using the /.list={...} feature, but I'm a bit fuzzy on the details.



                documentclass{standalone}
                usepackage{tikz}
                usetikzlibrary{matrix}

                newcommand{row}[2]{% #1 = row, #2 = style
                tikzset{row #1/.style={#2}}}

                newcommand{col}[2]{% #1 = column, #2 = style
                tikzset{column #1/.style={#2}}}

                begin{document}
                begin{tikzpicture}
                row{1}{red}
                row{3}{red}
                col{1}{blue}
                col{3}{blue}
                matrix [matrix of nodes]
                {
                8 & 1 & 6 \
                3 & 5 & 7 \
                4 & 9 & 2 \
                };
                end{tikzpicture}
                end{document}





                share|improve this answer




























                  2














                  Interestingly, you cannot do this using foreach. I understand this is because foreach is actually implemented using pgfkeys using the /.list={...} feature, but I'm a bit fuzzy on the details.



                  documentclass{standalone}
                  usepackage{tikz}
                  usetikzlibrary{matrix}

                  newcommand{row}[2]{% #1 = row, #2 = style
                  tikzset{row #1/.style={#2}}}

                  newcommand{col}[2]{% #1 = column, #2 = style
                  tikzset{column #1/.style={#2}}}

                  begin{document}
                  begin{tikzpicture}
                  row{1}{red}
                  row{3}{red}
                  col{1}{blue}
                  col{3}{blue}
                  matrix [matrix of nodes]
                  {
                  8 & 1 & 6 \
                  3 & 5 & 7 \
                  4 & 9 & 2 \
                  };
                  end{tikzpicture}
                  end{document}





                  share|improve this answer


























                    2












                    2








                    2







                    Interestingly, you cannot do this using foreach. I understand this is because foreach is actually implemented using pgfkeys using the /.list={...} feature, but I'm a bit fuzzy on the details.



                    documentclass{standalone}
                    usepackage{tikz}
                    usetikzlibrary{matrix}

                    newcommand{row}[2]{% #1 = row, #2 = style
                    tikzset{row #1/.style={#2}}}

                    newcommand{col}[2]{% #1 = column, #2 = style
                    tikzset{column #1/.style={#2}}}

                    begin{document}
                    begin{tikzpicture}
                    row{1}{red}
                    row{3}{red}
                    col{1}{blue}
                    col{3}{blue}
                    matrix [matrix of nodes]
                    {
                    8 & 1 & 6 \
                    3 & 5 & 7 \
                    4 & 9 & 2 \
                    };
                    end{tikzpicture}
                    end{document}





                    share|improve this answer













                    Interestingly, you cannot do this using foreach. I understand this is because foreach is actually implemented using pgfkeys using the /.list={...} feature, but I'm a bit fuzzy on the details.



                    documentclass{standalone}
                    usepackage{tikz}
                    usetikzlibrary{matrix}

                    newcommand{row}[2]{% #1 = row, #2 = style
                    tikzset{row #1/.style={#2}}}

                    newcommand{col}[2]{% #1 = column, #2 = style
                    tikzset{column #1/.style={#2}}}

                    begin{document}
                    begin{tikzpicture}
                    row{1}{red}
                    row{3}{red}
                    col{1}{blue}
                    col{3}{blue}
                    matrix [matrix of nodes]
                    {
                    8 & 1 & 6 \
                    3 & 5 & 7 \
                    4 & 9 & 2 \
                    };
                    end{tikzpicture}
                    end{document}






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 7 '14 at 3:34









                    John KormyloJohn Kormylo

                    45.2k12570




                    45.2k12570























                        0














                        I reopen this old questions because, from percusse's and cjorssen's answers, I managed to assign style to a block of cells only specifying its (top,left to bottom,right) coordinates. I share here my solution, hoping it will help someone else.



                        documentclass{standalone}
                        usepackage{tikz}
                        usetikzlibrary{matrix}
                        begin{document}
                        tikzset{myblocks/.style args = {(#1,#2 to #3,#4)}{
                        blockrows/.style={
                        block/.style={
                        row ##1 column ####1/.style={nodes={text=red}}
                        },
                        block/.list={#2,...,#4}
                        },
                        blockrows/.list={#1,...,#3}
                        }
                        }
                        begin{tikzpicture}[myblocks/.list={(1,1 to 2,2),(1,4 to 3,5)}]
                        matrix [matrix of nodes]
                        {
                        8 & 1 & 6 & 8 & 1 & 6 \
                        3 & 5 & 7 & 3 & 5 & 7 \
                        4 & 9 & 2 & 4 & 9 & 2 \
                        };
                        end{tikzpicture}
                        end{document}


                        enter image description here






                        share|improve this answer






























                          0














                          I reopen this old questions because, from percusse's and cjorssen's answers, I managed to assign style to a block of cells only specifying its (top,left to bottom,right) coordinates. I share here my solution, hoping it will help someone else.



                          documentclass{standalone}
                          usepackage{tikz}
                          usetikzlibrary{matrix}
                          begin{document}
                          tikzset{myblocks/.style args = {(#1,#2 to #3,#4)}{
                          blockrows/.style={
                          block/.style={
                          row ##1 column ####1/.style={nodes={text=red}}
                          },
                          block/.list={#2,...,#4}
                          },
                          blockrows/.list={#1,...,#3}
                          }
                          }
                          begin{tikzpicture}[myblocks/.list={(1,1 to 2,2),(1,4 to 3,5)}]
                          matrix [matrix of nodes]
                          {
                          8 & 1 & 6 & 8 & 1 & 6 \
                          3 & 5 & 7 & 3 & 5 & 7 \
                          4 & 9 & 2 & 4 & 9 & 2 \
                          };
                          end{tikzpicture}
                          end{document}


                          enter image description here






                          share|improve this answer




























                            0












                            0








                            0







                            I reopen this old questions because, from percusse's and cjorssen's answers, I managed to assign style to a block of cells only specifying its (top,left to bottom,right) coordinates. I share here my solution, hoping it will help someone else.



                            documentclass{standalone}
                            usepackage{tikz}
                            usetikzlibrary{matrix}
                            begin{document}
                            tikzset{myblocks/.style args = {(#1,#2 to #3,#4)}{
                            blockrows/.style={
                            block/.style={
                            row ##1 column ####1/.style={nodes={text=red}}
                            },
                            block/.list={#2,...,#4}
                            },
                            blockrows/.list={#1,...,#3}
                            }
                            }
                            begin{tikzpicture}[myblocks/.list={(1,1 to 2,2),(1,4 to 3,5)}]
                            matrix [matrix of nodes]
                            {
                            8 & 1 & 6 & 8 & 1 & 6 \
                            3 & 5 & 7 & 3 & 5 & 7 \
                            4 & 9 & 2 & 4 & 9 & 2 \
                            };
                            end{tikzpicture}
                            end{document}


                            enter image description here






                            share|improve this answer















                            I reopen this old questions because, from percusse's and cjorssen's answers, I managed to assign style to a block of cells only specifying its (top,left to bottom,right) coordinates. I share here my solution, hoping it will help someone else.



                            documentclass{standalone}
                            usepackage{tikz}
                            usetikzlibrary{matrix}
                            begin{document}
                            tikzset{myblocks/.style args = {(#1,#2 to #3,#4)}{
                            blockrows/.style={
                            block/.style={
                            row ##1 column ####1/.style={nodes={text=red}}
                            },
                            block/.list={#2,...,#4}
                            },
                            blockrows/.list={#1,...,#3}
                            }
                            }
                            begin{tikzpicture}[myblocks/.list={(1,1 to 2,2),(1,4 to 3,5)}]
                            matrix [matrix of nodes]
                            {
                            8 & 1 & 6 & 8 & 1 & 6 \
                            3 & 5 & 7 & 3 & 5 & 7 \
                            4 & 9 & 2 & 4 & 9 & 2 \
                            };
                            end{tikzpicture}
                            end{document}


                            enter image description here







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 4 mins ago

























                            answered 10 mins ago









                            JPGJPG

                            1,392413




                            1,392413






























                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f175616%2ftikz-matrix-style-for-combination-or-rows-and-columns%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

                                IEEEtran - How to include ORCID in TeX/PDF with PdfLatexIs there a standard way to include ORCID in TeX /...

                                Cicindela nigrior Przypisy | Menu nawigacyjneCicindela varians unicolorManual for the Identification of the...

                                Glossaries-extra: Adding glossaries package to “Clas­sicTh­e­sis” template by Dr. André Miede v. 4.6 ...