6

I am trying to highlight elements of a matrix by drawing blocks around them. However some portion of blocks is not visible as you can see in the following image

enter image description here

I created this by using the code posted here: Highlight elements in the matrix

How do I adjust the coordinates of these boxes so that they look like concentric rectangles.

MWE:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows,matrix,positioning}
\begin{document}
    \begin{tikzpicture}
        \matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (m)
        {
            8 &8 &1 &6 \\               
            3 &8 &5 &7 \\               
            4 &8 &9 &5 \\           
        };  
        \draw[color=red] (m-1-1.north west) -- (m-1-3.north east) -- (m-2-3.south east) -- (m-2-1.south west) -- (m-1-1.north west);
        \draw[color=red,double,implies-](m-1-2.north) -- +(0,0.3);
        \draw[color=blue] (m-1-1.north west) -- (m-1-2.north east) -- (m-2-2.south east) -- (m-2-1.south west) -- (m-1-1.north west);
    \end{tikzpicture}
\end{document}
NAASI
  • 2,809

1 Answers1

7

I would use the fit library instead, making a node that encompasses two of the opposing corners of the rectangle. Then you can simply adjust the inner sep of the node to adjust the size.

output of code

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows,matrix,positioning,fit}
\begin{document}
    \begin{tikzpicture}
        \matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (m)
        {
            8 &8 &1 &6 \\               
            3 &8 &5 &7 \\               
            4 &8 &9 &5 \\           
        };  
        \node[draw=red,inner sep=1pt,fit=(m-1-1.north west)(m-2-3.south east)] {};
        \draw[color=red,double,implies-] ([yshift=1pt]m-1-2.north) -- +(0,0.3);
        \node[draw=blue,inner sep=0pt,fit=(m-1-1.north west)(m-2-2.south east)] {};
    \end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688