Is there some way put boxes around matrix elements without too much effort?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite
1












I know this question has been asked before but the examples that they give are too complicated to replicate, for example.



Is there some way to make boxes like these (dashes are not necessary but preferable):



enter image description here



for entries of a matrix?



Thanks!







share|improve this question


























    up vote
    5
    down vote

    favorite
    1












    I know this question has been asked before but the examples that they give are too complicated to replicate, for example.



    Is there some way to make boxes like these (dashes are not necessary but preferable):



    enter image description here



    for entries of a matrix?



    Thanks!







    share|improve this question
























      up vote
      5
      down vote

      favorite
      1









      up vote
      5
      down vote

      favorite
      1






      1





      I know this question has been asked before but the examples that they give are too complicated to replicate, for example.



      Is there some way to make boxes like these (dashes are not necessary but preferable):



      enter image description here



      for entries of a matrix?



      Thanks!







      share|improve this question














      I know this question has been asked before but the examples that they give are too complicated to replicate, for example.



      Is there some way to make boxes like these (dashes are not necessary but preferable):



      enter image description here



      for entries of a matrix?



      Thanks!









      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 12 at 0:27

























      asked Aug 12 at 0:16









      Enlightened One

      1,11721019




      1,11721019




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          11
          down vote



          accepted










          Here is TikZ proposal using fit. I've wrapped the code in a command I've called mymatrixbox. It takes 4 inputs which define the boundaries of the box using the cell coordinates (it also takes 1 optional input which allows you some control over the default style).



          The way it works is like this, say you want to draw a box from (1,1) to (3,1), then use mymatrixbox1131. To draw a box around a single cell then just repeat the cell coordinates - e.g. matrixbox2323. If I wanted to draw a box around the entire matrix the code for that would be mymatrixbox1133.



          enter image description here



          documentclass[margin=0.5cm]standalone

          usepackagetikz
          usetikzlibrarymatrix,fit
          pgfkeystikz/mymatrix/.style=matrix of math nodes,inner sep=0pt,row sep=0em,column sep=0em,nodes=inner sep=6pt

          newcommand*mymatrixbox[5]node [fit= (m-#2-#3) (m-#4-#5)] [draw=blue,thick,dashed,rounded corners,inner sep=-2pt,#1] ;

          begindocument

          begintikzpicture[baseline=0cm]
          matrix [mymatrix] (m)

          1 & 0 & 0 \
          0 & 1 & 0 \
          0 & 0 & 1 \
          ;

          mymatrixbox1131
          mymatrixbox3233
          mymatrixbox[red]2323

          endtikzpicture

          enddocument





          share|improve this answer





























            up vote
            4
            down vote













            A simple solution with pstricks and a plain matrix environment from amsmath:



            documentclass[11pt, svgnames]article
            usepackageamssymb, amsmath, array
            usepackagepst-node
            usepackageauto-pst-pdf

            begindocument

            beginequation*
            beginpostscript
            pssetnodesep=5pt, boxsize=0.7em, linearc=.1, framearc=0.2, linestyle=dashed, dash= 4pt 2pt, linecolor=LightBlue
            setlengtharraycolsep6pt
            setlengthextrarowheight3pt
            beginmatrix
            RnodeA1& 0 & 0 \
            0 &1 & rnodeE0 \
            RnodeB0& RnodeC0 & RnodeD1
            endmatrix
            ncboxAB
            ncboxCD
            fnode[framesize = 0.56 0.50](E)F
            endpostscript
            endequation*

            enddocument


            enter image description here






            share|improve this answer






















            • how to set the same width to all columns?
              – pzorba75
              Aug 12 at 14:50






            • 1




              @pzorba75: I didn't notice, but you're right: there's a width problem and a priori I don't see why (normally postscript code is dimensionless, so it doesn't modify the column widths). If I find a solution, I'll post a new code and warn you.
              – Bernard
              Aug 12 at 14:59






            • 1




              @pzorba: Problem solved: it came from incorporating postcript code directly in the matrix (for the frame in the second row) instead of writing code outside. I think now it's OK. Thanks for pointing it!
              – Bernard
              Aug 12 at 15:21

















            up vote
            1
            down vote













            Just for completeness: a slightly modified version of Milo's nice answer which works with arbitrary matrices, i.e. has that flexibility of Bernhards nice answer without having to go through the usual "how do I compile documents containing pstricks and can I be sure that the outcome does not depend on the compiler?" questions. That is, you do not have to write the matrix as a tikzpicture. The price you have to pay is that you have to "mark" the elements you want to box with tikznode beforehand.



            documentclassarticle
            usepackageamsmath,bbm
            usepackagetikz
            usetikzlibraryfit
            newcommandtikznode[2]relax
            ifmmode%
            tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) $#2$;
            else
            tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) #2;%
            fi
            tikzsetbox around/.style=
            draw,rounded corners,
            inner sep=2pt,outer sep=0pt,
            node contents=,fit=#1
            ,

            begindocument
            beginequation
            mathbbm1_3=beginpmatrix
            tikznodem-1-11 & 0 & 0\
            tikznodem-2-10 & 1 & tikznodem-2-30\
            tikznodem-3-10 & tikznodem-3-20 & tikznodem-3-31
            endpmatrix;.
            endequation
            tikz[overlay,remember picture]%
            node[blue,dash pattern=on 2pt off 1.25pt, thick,box around=(m-1-1)(m-3-1)];
            node[red,dash pattern=on 2pt off 1.25pt, thick,box around=(m-2-3)];
            node[purple,dash pattern=on 2pt off 1.25pt, thick,box around=(m-3-2)(m-3-3)];
            enddocument


            enter image description here



            Of course, you could also wrap this into a macro, i.e. the following yields the same output.



            documentclassarticle
            usepackageamsmath,bbm
            usepackagetikz
            usetikzlibraryfit
            newcommandtikznode[2]relax
            ifmmode%
            tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) $#2$;
            else
            tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) #2;%
            fi
            tikzsetbox around/.style=
            draw,rounded corners,
            inner sep=2pt,outer sep=0pt,
            node contents=,fit=#1
            ,

            newcommandBoxAround[2]
            tikz[overlay,remember picture]%
            node[blue,dash pattern=on 2pt off 1.25pt,thick,#1,box around=#2];
            begindocument
            beginequation
            mathbbm1_3=beginpmatrix
            tikznodem-1-11 & 0 & 0\
            tikznodem-2-10 & 1 & tikznodem-2-30\
            tikznodem-3-10 & tikznodem-3-20 & tikznodem-3-31
            endpmatrix;.
            endequation
            BoxAround(m-1-1)(m-3-1)
            BoxAround[red](m-2-3)
            BoxAround[purple](m-3-2)(m-3-3)
            enddocument





            share|improve this answer



























              up vote
              1
              down vote













              today i discover interesting package nicematrix. with it you can write:



              documentclass[margin=3mm, preview]standalone
              usepackagenicematrix
              NiceMatrixOptionstransparent
              usetikzlibraryfit

              begindocument
              [
              mathbfI_3 = beginbNiceMatrix[columns-width = 1em]
              [code-after= begintikzpicture[FIT/.style = %
              draw, semithick, rounded corners, densely dashed,
              inner sep=2pt, fit=#1, node contents=
              ]
              node[FIT=(1-1) (3-1),blue];
              node[FIT=(3-2) (3-3),blue];
              node[FIT=(1-3) (2-3),red];
              endtikzpicture

              ]
              1 & 0 & 0 \
              0 & 1 & 0 \
              0 & 0 & 1 \
              endbNiceMatrix
              ]
              enddocument


              for correct result you need to compile document at least twice.



              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',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                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%2f445701%2fis-there-some-way-put-boxes-around-matrix-elements-without-too-much-effort%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                11
                down vote



                accepted










                Here is TikZ proposal using fit. I've wrapped the code in a command I've called mymatrixbox. It takes 4 inputs which define the boundaries of the box using the cell coordinates (it also takes 1 optional input which allows you some control over the default style).



                The way it works is like this, say you want to draw a box from (1,1) to (3,1), then use mymatrixbox1131. To draw a box around a single cell then just repeat the cell coordinates - e.g. matrixbox2323. If I wanted to draw a box around the entire matrix the code for that would be mymatrixbox1133.



                enter image description here



                documentclass[margin=0.5cm]standalone

                usepackagetikz
                usetikzlibrarymatrix,fit
                pgfkeystikz/mymatrix/.style=matrix of math nodes,inner sep=0pt,row sep=0em,column sep=0em,nodes=inner sep=6pt

                newcommand*mymatrixbox[5]node [fit= (m-#2-#3) (m-#4-#5)] [draw=blue,thick,dashed,rounded corners,inner sep=-2pt,#1] ;

                begindocument

                begintikzpicture[baseline=0cm]
                matrix [mymatrix] (m)

                1 & 0 & 0 \
                0 & 1 & 0 \
                0 & 0 & 1 \
                ;

                mymatrixbox1131
                mymatrixbox3233
                mymatrixbox[red]2323

                endtikzpicture

                enddocument





                share|improve this answer


























                  up vote
                  11
                  down vote



                  accepted










                  Here is TikZ proposal using fit. I've wrapped the code in a command I've called mymatrixbox. It takes 4 inputs which define the boundaries of the box using the cell coordinates (it also takes 1 optional input which allows you some control over the default style).



                  The way it works is like this, say you want to draw a box from (1,1) to (3,1), then use mymatrixbox1131. To draw a box around a single cell then just repeat the cell coordinates - e.g. matrixbox2323. If I wanted to draw a box around the entire matrix the code for that would be mymatrixbox1133.



                  enter image description here



                  documentclass[margin=0.5cm]standalone

                  usepackagetikz
                  usetikzlibrarymatrix,fit
                  pgfkeystikz/mymatrix/.style=matrix of math nodes,inner sep=0pt,row sep=0em,column sep=0em,nodes=inner sep=6pt

                  newcommand*mymatrixbox[5]node [fit= (m-#2-#3) (m-#4-#5)] [draw=blue,thick,dashed,rounded corners,inner sep=-2pt,#1] ;

                  begindocument

                  begintikzpicture[baseline=0cm]
                  matrix [mymatrix] (m)

                  1 & 0 & 0 \
                  0 & 1 & 0 \
                  0 & 0 & 1 \
                  ;

                  mymatrixbox1131
                  mymatrixbox3233
                  mymatrixbox[red]2323

                  endtikzpicture

                  enddocument





                  share|improve this answer
























                    up vote
                    11
                    down vote



                    accepted







                    up vote
                    11
                    down vote



                    accepted






                    Here is TikZ proposal using fit. I've wrapped the code in a command I've called mymatrixbox. It takes 4 inputs which define the boundaries of the box using the cell coordinates (it also takes 1 optional input which allows you some control over the default style).



                    The way it works is like this, say you want to draw a box from (1,1) to (3,1), then use mymatrixbox1131. To draw a box around a single cell then just repeat the cell coordinates - e.g. matrixbox2323. If I wanted to draw a box around the entire matrix the code for that would be mymatrixbox1133.



                    enter image description here



                    documentclass[margin=0.5cm]standalone

                    usepackagetikz
                    usetikzlibrarymatrix,fit
                    pgfkeystikz/mymatrix/.style=matrix of math nodes,inner sep=0pt,row sep=0em,column sep=0em,nodes=inner sep=6pt

                    newcommand*mymatrixbox[5]node [fit= (m-#2-#3) (m-#4-#5)] [draw=blue,thick,dashed,rounded corners,inner sep=-2pt,#1] ;

                    begindocument

                    begintikzpicture[baseline=0cm]
                    matrix [mymatrix] (m)

                    1 & 0 & 0 \
                    0 & 1 & 0 \
                    0 & 0 & 1 \
                    ;

                    mymatrixbox1131
                    mymatrixbox3233
                    mymatrixbox[red]2323

                    endtikzpicture

                    enddocument





                    share|improve this answer














                    Here is TikZ proposal using fit. I've wrapped the code in a command I've called mymatrixbox. It takes 4 inputs which define the boundaries of the box using the cell coordinates (it also takes 1 optional input which allows you some control over the default style).



                    The way it works is like this, say you want to draw a box from (1,1) to (3,1), then use mymatrixbox1131. To draw a box around a single cell then just repeat the cell coordinates - e.g. matrixbox2323. If I wanted to draw a box around the entire matrix the code for that would be mymatrixbox1133.



                    enter image description here



                    documentclass[margin=0.5cm]standalone

                    usepackagetikz
                    usetikzlibrarymatrix,fit
                    pgfkeystikz/mymatrix/.style=matrix of math nodes,inner sep=0pt,row sep=0em,column sep=0em,nodes=inner sep=6pt

                    newcommand*mymatrixbox[5]node [fit= (m-#2-#3) (m-#4-#5)] [draw=blue,thick,dashed,rounded corners,inner sep=-2pt,#1] ;

                    begindocument

                    begintikzpicture[baseline=0cm]
                    matrix [mymatrix] (m)

                    1 & 0 & 0 \
                    0 & 1 & 0 \
                    0 & 0 & 1 \
                    ;

                    mymatrixbox1131
                    mymatrixbox3233
                    mymatrixbox[red]2323

                    endtikzpicture

                    enddocument






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 12 at 1:00

























                    answered Aug 12 at 0:35









                    Milo

                    4,30121242




                    4,30121242




















                        up vote
                        4
                        down vote













                        A simple solution with pstricks and a plain matrix environment from amsmath:



                        documentclass[11pt, svgnames]article
                        usepackageamssymb, amsmath, array
                        usepackagepst-node
                        usepackageauto-pst-pdf

                        begindocument

                        beginequation*
                        beginpostscript
                        pssetnodesep=5pt, boxsize=0.7em, linearc=.1, framearc=0.2, linestyle=dashed, dash= 4pt 2pt, linecolor=LightBlue
                        setlengtharraycolsep6pt
                        setlengthextrarowheight3pt
                        beginmatrix
                        RnodeA1& 0 & 0 \
                        0 &1 & rnodeE0 \
                        RnodeB0& RnodeC0 & RnodeD1
                        endmatrix
                        ncboxAB
                        ncboxCD
                        fnode[framesize = 0.56 0.50](E)F
                        endpostscript
                        endequation*

                        enddocument


                        enter image description here






                        share|improve this answer






















                        • how to set the same width to all columns?
                          – pzorba75
                          Aug 12 at 14:50






                        • 1




                          @pzorba75: I didn't notice, but you're right: there's a width problem and a priori I don't see why (normally postscript code is dimensionless, so it doesn't modify the column widths). If I find a solution, I'll post a new code and warn you.
                          – Bernard
                          Aug 12 at 14:59






                        • 1




                          @pzorba: Problem solved: it came from incorporating postcript code directly in the matrix (for the frame in the second row) instead of writing code outside. I think now it's OK. Thanks for pointing it!
                          – Bernard
                          Aug 12 at 15:21














                        up vote
                        4
                        down vote













                        A simple solution with pstricks and a plain matrix environment from amsmath:



                        documentclass[11pt, svgnames]article
                        usepackageamssymb, amsmath, array
                        usepackagepst-node
                        usepackageauto-pst-pdf

                        begindocument

                        beginequation*
                        beginpostscript
                        pssetnodesep=5pt, boxsize=0.7em, linearc=.1, framearc=0.2, linestyle=dashed, dash= 4pt 2pt, linecolor=LightBlue
                        setlengtharraycolsep6pt
                        setlengthextrarowheight3pt
                        beginmatrix
                        RnodeA1& 0 & 0 \
                        0 &1 & rnodeE0 \
                        RnodeB0& RnodeC0 & RnodeD1
                        endmatrix
                        ncboxAB
                        ncboxCD
                        fnode[framesize = 0.56 0.50](E)F
                        endpostscript
                        endequation*

                        enddocument


                        enter image description here






                        share|improve this answer






















                        • how to set the same width to all columns?
                          – pzorba75
                          Aug 12 at 14:50






                        • 1




                          @pzorba75: I didn't notice, but you're right: there's a width problem and a priori I don't see why (normally postscript code is dimensionless, so it doesn't modify the column widths). If I find a solution, I'll post a new code and warn you.
                          – Bernard
                          Aug 12 at 14:59






                        • 1




                          @pzorba: Problem solved: it came from incorporating postcript code directly in the matrix (for the frame in the second row) instead of writing code outside. I think now it's OK. Thanks for pointing it!
                          – Bernard
                          Aug 12 at 15:21












                        up vote
                        4
                        down vote










                        up vote
                        4
                        down vote









                        A simple solution with pstricks and a plain matrix environment from amsmath:



                        documentclass[11pt, svgnames]article
                        usepackageamssymb, amsmath, array
                        usepackagepst-node
                        usepackageauto-pst-pdf

                        begindocument

                        beginequation*
                        beginpostscript
                        pssetnodesep=5pt, boxsize=0.7em, linearc=.1, framearc=0.2, linestyle=dashed, dash= 4pt 2pt, linecolor=LightBlue
                        setlengtharraycolsep6pt
                        setlengthextrarowheight3pt
                        beginmatrix
                        RnodeA1& 0 & 0 \
                        0 &1 & rnodeE0 \
                        RnodeB0& RnodeC0 & RnodeD1
                        endmatrix
                        ncboxAB
                        ncboxCD
                        fnode[framesize = 0.56 0.50](E)F
                        endpostscript
                        endequation*

                        enddocument


                        enter image description here






                        share|improve this answer














                        A simple solution with pstricks and a plain matrix environment from amsmath:



                        documentclass[11pt, svgnames]article
                        usepackageamssymb, amsmath, array
                        usepackagepst-node
                        usepackageauto-pst-pdf

                        begindocument

                        beginequation*
                        beginpostscript
                        pssetnodesep=5pt, boxsize=0.7em, linearc=.1, framearc=0.2, linestyle=dashed, dash= 4pt 2pt, linecolor=LightBlue
                        setlengtharraycolsep6pt
                        setlengthextrarowheight3pt
                        beginmatrix
                        RnodeA1& 0 & 0 \
                        0 &1 & rnodeE0 \
                        RnodeB0& RnodeC0 & RnodeD1
                        endmatrix
                        ncboxAB
                        ncboxCD
                        fnode[framesize = 0.56 0.50](E)F
                        endpostscript
                        endequation*

                        enddocument


                        enter image description here







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Aug 12 at 15:18

























                        answered Aug 12 at 10:30









                        Bernard

                        154k762188




                        154k762188











                        • how to set the same width to all columns?
                          – pzorba75
                          Aug 12 at 14:50






                        • 1




                          @pzorba75: I didn't notice, but you're right: there's a width problem and a priori I don't see why (normally postscript code is dimensionless, so it doesn't modify the column widths). If I find a solution, I'll post a new code and warn you.
                          – Bernard
                          Aug 12 at 14:59






                        • 1




                          @pzorba: Problem solved: it came from incorporating postcript code directly in the matrix (for the frame in the second row) instead of writing code outside. I think now it's OK. Thanks for pointing it!
                          – Bernard
                          Aug 12 at 15:21
















                        • how to set the same width to all columns?
                          – pzorba75
                          Aug 12 at 14:50






                        • 1




                          @pzorba75: I didn't notice, but you're right: there's a width problem and a priori I don't see why (normally postscript code is dimensionless, so it doesn't modify the column widths). If I find a solution, I'll post a new code and warn you.
                          – Bernard
                          Aug 12 at 14:59






                        • 1




                          @pzorba: Problem solved: it came from incorporating postcript code directly in the matrix (for the frame in the second row) instead of writing code outside. I think now it's OK. Thanks for pointing it!
                          – Bernard
                          Aug 12 at 15:21















                        how to set the same width to all columns?
                        – pzorba75
                        Aug 12 at 14:50




                        how to set the same width to all columns?
                        – pzorba75
                        Aug 12 at 14:50




                        1




                        1




                        @pzorba75: I didn't notice, but you're right: there's a width problem and a priori I don't see why (normally postscript code is dimensionless, so it doesn't modify the column widths). If I find a solution, I'll post a new code and warn you.
                        – Bernard
                        Aug 12 at 14:59




                        @pzorba75: I didn't notice, but you're right: there's a width problem and a priori I don't see why (normally postscript code is dimensionless, so it doesn't modify the column widths). If I find a solution, I'll post a new code and warn you.
                        – Bernard
                        Aug 12 at 14:59




                        1




                        1




                        @pzorba: Problem solved: it came from incorporating postcript code directly in the matrix (for the frame in the second row) instead of writing code outside. I think now it's OK. Thanks for pointing it!
                        – Bernard
                        Aug 12 at 15:21




                        @pzorba: Problem solved: it came from incorporating postcript code directly in the matrix (for the frame in the second row) instead of writing code outside. I think now it's OK. Thanks for pointing it!
                        – Bernard
                        Aug 12 at 15:21










                        up vote
                        1
                        down vote













                        Just for completeness: a slightly modified version of Milo's nice answer which works with arbitrary matrices, i.e. has that flexibility of Bernhards nice answer without having to go through the usual "how do I compile documents containing pstricks and can I be sure that the outcome does not depend on the compiler?" questions. That is, you do not have to write the matrix as a tikzpicture. The price you have to pay is that you have to "mark" the elements you want to box with tikznode beforehand.



                        documentclassarticle
                        usepackageamsmath,bbm
                        usepackagetikz
                        usetikzlibraryfit
                        newcommandtikznode[2]relax
                        ifmmode%
                        tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) $#2$;
                        else
                        tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) #2;%
                        fi
                        tikzsetbox around/.style=
                        draw,rounded corners,
                        inner sep=2pt,outer sep=0pt,
                        node contents=,fit=#1
                        ,

                        begindocument
                        beginequation
                        mathbbm1_3=beginpmatrix
                        tikznodem-1-11 & 0 & 0\
                        tikznodem-2-10 & 1 & tikznodem-2-30\
                        tikznodem-3-10 & tikznodem-3-20 & tikznodem-3-31
                        endpmatrix;.
                        endequation
                        tikz[overlay,remember picture]%
                        node[blue,dash pattern=on 2pt off 1.25pt, thick,box around=(m-1-1)(m-3-1)];
                        node[red,dash pattern=on 2pt off 1.25pt, thick,box around=(m-2-3)];
                        node[purple,dash pattern=on 2pt off 1.25pt, thick,box around=(m-3-2)(m-3-3)];
                        enddocument


                        enter image description here



                        Of course, you could also wrap this into a macro, i.e. the following yields the same output.



                        documentclassarticle
                        usepackageamsmath,bbm
                        usepackagetikz
                        usetikzlibraryfit
                        newcommandtikznode[2]relax
                        ifmmode%
                        tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) $#2$;
                        else
                        tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) #2;%
                        fi
                        tikzsetbox around/.style=
                        draw,rounded corners,
                        inner sep=2pt,outer sep=0pt,
                        node contents=,fit=#1
                        ,

                        newcommandBoxAround[2]
                        tikz[overlay,remember picture]%
                        node[blue,dash pattern=on 2pt off 1.25pt,thick,#1,box around=#2];
                        begindocument
                        beginequation
                        mathbbm1_3=beginpmatrix
                        tikznodem-1-11 & 0 & 0\
                        tikznodem-2-10 & 1 & tikznodem-2-30\
                        tikznodem-3-10 & tikznodem-3-20 & tikznodem-3-31
                        endpmatrix;.
                        endequation
                        BoxAround(m-1-1)(m-3-1)
                        BoxAround[red](m-2-3)
                        BoxAround[purple](m-3-2)(m-3-3)
                        enddocument





                        share|improve this answer
























                          up vote
                          1
                          down vote













                          Just for completeness: a slightly modified version of Milo's nice answer which works with arbitrary matrices, i.e. has that flexibility of Bernhards nice answer without having to go through the usual "how do I compile documents containing pstricks and can I be sure that the outcome does not depend on the compiler?" questions. That is, you do not have to write the matrix as a tikzpicture. The price you have to pay is that you have to "mark" the elements you want to box with tikznode beforehand.



                          documentclassarticle
                          usepackageamsmath,bbm
                          usepackagetikz
                          usetikzlibraryfit
                          newcommandtikznode[2]relax
                          ifmmode%
                          tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) $#2$;
                          else
                          tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) #2;%
                          fi
                          tikzsetbox around/.style=
                          draw,rounded corners,
                          inner sep=2pt,outer sep=0pt,
                          node contents=,fit=#1
                          ,

                          begindocument
                          beginequation
                          mathbbm1_3=beginpmatrix
                          tikznodem-1-11 & 0 & 0\
                          tikznodem-2-10 & 1 & tikznodem-2-30\
                          tikznodem-3-10 & tikznodem-3-20 & tikznodem-3-31
                          endpmatrix;.
                          endequation
                          tikz[overlay,remember picture]%
                          node[blue,dash pattern=on 2pt off 1.25pt, thick,box around=(m-1-1)(m-3-1)];
                          node[red,dash pattern=on 2pt off 1.25pt, thick,box around=(m-2-3)];
                          node[purple,dash pattern=on 2pt off 1.25pt, thick,box around=(m-3-2)(m-3-3)];
                          enddocument


                          enter image description here



                          Of course, you could also wrap this into a macro, i.e. the following yields the same output.



                          documentclassarticle
                          usepackageamsmath,bbm
                          usepackagetikz
                          usetikzlibraryfit
                          newcommandtikznode[2]relax
                          ifmmode%
                          tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) $#2$;
                          else
                          tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) #2;%
                          fi
                          tikzsetbox around/.style=
                          draw,rounded corners,
                          inner sep=2pt,outer sep=0pt,
                          node contents=,fit=#1
                          ,

                          newcommandBoxAround[2]
                          tikz[overlay,remember picture]%
                          node[blue,dash pattern=on 2pt off 1.25pt,thick,#1,box around=#2];
                          begindocument
                          beginequation
                          mathbbm1_3=beginpmatrix
                          tikznodem-1-11 & 0 & 0\
                          tikznodem-2-10 & 1 & tikznodem-2-30\
                          tikznodem-3-10 & tikznodem-3-20 & tikznodem-3-31
                          endpmatrix;.
                          endequation
                          BoxAround(m-1-1)(m-3-1)
                          BoxAround[red](m-2-3)
                          BoxAround[purple](m-3-2)(m-3-3)
                          enddocument





                          share|improve this answer






















                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            Just for completeness: a slightly modified version of Milo's nice answer which works with arbitrary matrices, i.e. has that flexibility of Bernhards nice answer without having to go through the usual "how do I compile documents containing pstricks and can I be sure that the outcome does not depend on the compiler?" questions. That is, you do not have to write the matrix as a tikzpicture. The price you have to pay is that you have to "mark" the elements you want to box with tikznode beforehand.



                            documentclassarticle
                            usepackageamsmath,bbm
                            usepackagetikz
                            usetikzlibraryfit
                            newcommandtikznode[2]relax
                            ifmmode%
                            tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) $#2$;
                            else
                            tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) #2;%
                            fi
                            tikzsetbox around/.style=
                            draw,rounded corners,
                            inner sep=2pt,outer sep=0pt,
                            node contents=,fit=#1
                            ,

                            begindocument
                            beginequation
                            mathbbm1_3=beginpmatrix
                            tikznodem-1-11 & 0 & 0\
                            tikznodem-2-10 & 1 & tikznodem-2-30\
                            tikznodem-3-10 & tikznodem-3-20 & tikznodem-3-31
                            endpmatrix;.
                            endequation
                            tikz[overlay,remember picture]%
                            node[blue,dash pattern=on 2pt off 1.25pt, thick,box around=(m-1-1)(m-3-1)];
                            node[red,dash pattern=on 2pt off 1.25pt, thick,box around=(m-2-3)];
                            node[purple,dash pattern=on 2pt off 1.25pt, thick,box around=(m-3-2)(m-3-3)];
                            enddocument


                            enter image description here



                            Of course, you could also wrap this into a macro, i.e. the following yields the same output.



                            documentclassarticle
                            usepackageamsmath,bbm
                            usepackagetikz
                            usetikzlibraryfit
                            newcommandtikznode[2]relax
                            ifmmode%
                            tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) $#2$;
                            else
                            tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) #2;%
                            fi
                            tikzsetbox around/.style=
                            draw,rounded corners,
                            inner sep=2pt,outer sep=0pt,
                            node contents=,fit=#1
                            ,

                            newcommandBoxAround[2]
                            tikz[overlay,remember picture]%
                            node[blue,dash pattern=on 2pt off 1.25pt,thick,#1,box around=#2];
                            begindocument
                            beginequation
                            mathbbm1_3=beginpmatrix
                            tikznodem-1-11 & 0 & 0\
                            tikznodem-2-10 & 1 & tikznodem-2-30\
                            tikznodem-3-10 & tikznodem-3-20 & tikznodem-3-31
                            endpmatrix;.
                            endequation
                            BoxAround(m-1-1)(m-3-1)
                            BoxAround[red](m-2-3)
                            BoxAround[purple](m-3-2)(m-3-3)
                            enddocument





                            share|improve this answer












                            Just for completeness: a slightly modified version of Milo's nice answer which works with arbitrary matrices, i.e. has that flexibility of Bernhards nice answer without having to go through the usual "how do I compile documents containing pstricks and can I be sure that the outcome does not depend on the compiler?" questions. That is, you do not have to write the matrix as a tikzpicture. The price you have to pay is that you have to "mark" the elements you want to box with tikznode beforehand.



                            documentclassarticle
                            usepackageamsmath,bbm
                            usepackagetikz
                            usetikzlibraryfit
                            newcommandtikznode[2]relax
                            ifmmode%
                            tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) $#2$;
                            else
                            tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) #2;%
                            fi
                            tikzsetbox around/.style=
                            draw,rounded corners,
                            inner sep=2pt,outer sep=0pt,
                            node contents=,fit=#1
                            ,

                            begindocument
                            beginequation
                            mathbbm1_3=beginpmatrix
                            tikznodem-1-11 & 0 & 0\
                            tikznodem-2-10 & 1 & tikznodem-2-30\
                            tikznodem-3-10 & tikznodem-3-20 & tikznodem-3-31
                            endpmatrix;.
                            endequation
                            tikz[overlay,remember picture]%
                            node[blue,dash pattern=on 2pt off 1.25pt, thick,box around=(m-1-1)(m-3-1)];
                            node[red,dash pattern=on 2pt off 1.25pt, thick,box around=(m-2-3)];
                            node[purple,dash pattern=on 2pt off 1.25pt, thick,box around=(m-3-2)(m-3-3)];
                            enddocument


                            enter image description here



                            Of course, you could also wrap this into a macro, i.e. the following yields the same output.



                            documentclassarticle
                            usepackageamsmath,bbm
                            usepackagetikz
                            usetikzlibraryfit
                            newcommandtikznode[2]relax
                            ifmmode%
                            tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) $#2$;
                            else
                            tikz[remember picture,baseline=(#1.base),inner sep=0pt] node (#1) #2;%
                            fi
                            tikzsetbox around/.style=
                            draw,rounded corners,
                            inner sep=2pt,outer sep=0pt,
                            node contents=,fit=#1
                            ,

                            newcommandBoxAround[2]
                            tikz[overlay,remember picture]%
                            node[blue,dash pattern=on 2pt off 1.25pt,thick,#1,box around=#2];
                            begindocument
                            beginequation
                            mathbbm1_3=beginpmatrix
                            tikznodem-1-11 & 0 & 0\
                            tikznodem-2-10 & 1 & tikznodem-2-30\
                            tikznodem-3-10 & tikznodem-3-20 & tikznodem-3-31
                            endpmatrix;.
                            endequation
                            BoxAround(m-1-1)(m-3-1)
                            BoxAround[red](m-2-3)
                            BoxAround[purple](m-3-2)(m-3-3)
                            enddocument






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 12 at 16:56









                            marmot

                            51.9k352113




                            51.9k352113




















                                up vote
                                1
                                down vote













                                today i discover interesting package nicematrix. with it you can write:



                                documentclass[margin=3mm, preview]standalone
                                usepackagenicematrix
                                NiceMatrixOptionstransparent
                                usetikzlibraryfit

                                begindocument
                                [
                                mathbfI_3 = beginbNiceMatrix[columns-width = 1em]
                                [code-after= begintikzpicture[FIT/.style = %
                                draw, semithick, rounded corners, densely dashed,
                                inner sep=2pt, fit=#1, node contents=
                                ]
                                node[FIT=(1-1) (3-1),blue];
                                node[FIT=(3-2) (3-3),blue];
                                node[FIT=(1-3) (2-3),red];
                                endtikzpicture

                                ]
                                1 & 0 & 0 \
                                0 & 1 & 0 \
                                0 & 0 & 1 \
                                endbNiceMatrix
                                ]
                                enddocument


                                for correct result you need to compile document at least twice.



                                enter image description here






                                share|improve this answer
























                                  up vote
                                  1
                                  down vote













                                  today i discover interesting package nicematrix. with it you can write:



                                  documentclass[margin=3mm, preview]standalone
                                  usepackagenicematrix
                                  NiceMatrixOptionstransparent
                                  usetikzlibraryfit

                                  begindocument
                                  [
                                  mathbfI_3 = beginbNiceMatrix[columns-width = 1em]
                                  [code-after= begintikzpicture[FIT/.style = %
                                  draw, semithick, rounded corners, densely dashed,
                                  inner sep=2pt, fit=#1, node contents=
                                  ]
                                  node[FIT=(1-1) (3-1),blue];
                                  node[FIT=(3-2) (3-3),blue];
                                  node[FIT=(1-3) (2-3),red];
                                  endtikzpicture

                                  ]
                                  1 & 0 & 0 \
                                  0 & 1 & 0 \
                                  0 & 0 & 1 \
                                  endbNiceMatrix
                                  ]
                                  enddocument


                                  for correct result you need to compile document at least twice.



                                  enter image description here






                                  share|improve this answer






















                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    today i discover interesting package nicematrix. with it you can write:



                                    documentclass[margin=3mm, preview]standalone
                                    usepackagenicematrix
                                    NiceMatrixOptionstransparent
                                    usetikzlibraryfit

                                    begindocument
                                    [
                                    mathbfI_3 = beginbNiceMatrix[columns-width = 1em]
                                    [code-after= begintikzpicture[FIT/.style = %
                                    draw, semithick, rounded corners, densely dashed,
                                    inner sep=2pt, fit=#1, node contents=
                                    ]
                                    node[FIT=(1-1) (3-1),blue];
                                    node[FIT=(3-2) (3-3),blue];
                                    node[FIT=(1-3) (2-3),red];
                                    endtikzpicture

                                    ]
                                    1 & 0 & 0 \
                                    0 & 1 & 0 \
                                    0 & 0 & 1 \
                                    endbNiceMatrix
                                    ]
                                    enddocument


                                    for correct result you need to compile document at least twice.



                                    enter image description here






                                    share|improve this answer












                                    today i discover interesting package nicematrix. with it you can write:



                                    documentclass[margin=3mm, preview]standalone
                                    usepackagenicematrix
                                    NiceMatrixOptionstransparent
                                    usetikzlibraryfit

                                    begindocument
                                    [
                                    mathbfI_3 = beginbNiceMatrix[columns-width = 1em]
                                    [code-after= begintikzpicture[FIT/.style = %
                                    draw, semithick, rounded corners, densely dashed,
                                    inner sep=2pt, fit=#1, node contents=
                                    ]
                                    node[FIT=(1-1) (3-1),blue];
                                    node[FIT=(3-2) (3-3),blue];
                                    node[FIT=(1-3) (2-3),red];
                                    endtikzpicture

                                    ]
                                    1 & 0 & 0 \
                                    0 & 1 & 0 \
                                    0 & 0 & 1 \
                                    endbNiceMatrix
                                    ]
                                    enddocument


                                    for correct result you need to compile document at least twice.



                                    enter image description here







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 12 at 17:38









                                    Zarko

                                    110k860148




                                    110k860148






















                                         

                                        draft saved


                                        draft discarded


























                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f445701%2fis-there-some-way-put-boxes-around-matrix-elements-without-too-much-effort%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        這個網誌中的熱門文章

                                        How to combine Bézier curves to a surface?

                                        Carbon dioxide

                                        Why am i infinitely getting the same tweet with the Twitter Search API?