3

I would like to circle the elements in the matrix table (How to create a small cell with borders in the top corner of a cell in the table?) and hatch pattern the matrix table excluding the circled numbers and inlays. I would also like to fill the cells in the demand and supply with red color as indicated in the figure. Please help me.

Please note that the red lines drawn in the figure are approximate, in the final solution, I would like to have perfectly slanted group of lines (45 degree hatch lines).

enter image description here

\documentclass[tikz,border=3mm]{standalone}
\usepackage{amsmath}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[mmat/.style={matrix of math nodes,nodes in empty cells,
    row sep=-\pgflinewidth,column sep=-\pgflinewidth,
    nodes={minimum width=5.5em,minimum height=3.5em,
        draw,anchor=center,
        text depth=0.25ex,text height=0.8em}},
    inlay/.style={label={[draw,thin,anchor=north east,minimum width=0.7cm,
    minimum height=0em,inner sep=1.4pt]north east:#1}}]
    \matrix[mmat] (mat){
    &  P & Q & R & S & \textit{Supply} \\
    A & |[inlay=10]| 300 & |[inlay=20]| 200 & |[inlay=30]| 0 & |[inlay=40]| 0 & 500 \\
    B & |[inlay=50]| 0 & |[inlay=60]| & |[inlay=70]| & |[inlay=80]| & 500 \\
    C & |[inlay=90]| 0 & |[inlay=100]| & |[inlay=110]| & |[inlay=120]| & 500 \\
    Demand & |[inlay=130]| 300 & |[inlay=140]| 200 & |[inlay=150]| 1000 & |[inlay=160]| 500 & 2000 \\
    };         
    \draw (mat-1-1.north west) -- (mat-1-1.south east);
    \draw[stealth-] ([yshift=2ex,xshift=-0.5ex]mat-1-1.east) 
    -- ++ (-2ex,0) node[left,font=\itshape]{To};
    \draw[stealth-] ([xshift=3ex,yshift=0.5ex]mat-1-1.south west) 
    -- ++ (0,2ex) node[above,font=\itshape]{From};
    \end{tikzpicture}
    \end{document}
rakatex
  • 489
  • 2
  • 13

1 Answers1

3

You can make the circled node contents labels, which you name according to the position in the matrix, such that you can access them from outside, and also name the inlays. This allows you hatch the region.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{amsmath}
\usetikzlibrary{calc,matrix,patterns.meta}
\begin{document}
\begin{tikzpicture}[mmat/.style={matrix of math nodes,nodes in empty cells,
    row sep=-\pgflinewidth,column sep=-\pgflinewidth,
    nodes={minimum width=5.5em,minimum height=3.5em,
        draw,anchor=center,
        text depth=0.25ex,text height=0.8em}},
    inlay/.style={label={[draw,thin,anchor=north east,minimum width=0.7cm,
    minimum height=0em,inner sep=1.4pt,fill=white,
    name=inlay-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn]north east:#1}},
    c/.style={label={[minimum width=0em,minimum height=0em,
        anchor=center,circle,inner sep=1pt,draw=red,
    name=c-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn]center:#1}}]
    \matrix[mmat] (mat){
    &  P & Q & R & S & \textit{Supply} \\
    A & |[c=300,inlay=10]|  & |[c=200,inlay=20]| & |[c=0,inlay=30]| & |[c=0,inlay=40]|  &   |[fill=red]|500 \\
    B & |[c=0,inlay=50]| & |[inlay=60]| & |[inlay=70]| & |[inlay=80]| & 500 \\
    C & |[c=0,inlay=90]|  & |[inlay=100]| & |[inlay=110]| & |[inlay=120]| & 500 \\
    Demand & |[inlay=130,fill=red]| 300 & |[inlay=140]| 200 & |[inlay=150]| 1000 & |[inlay=160]| 500 & 2000 \\
    };         
    \draw (mat-1-1.north west) -- (mat-1-1.south east);
    \draw[stealth-] ([yshift=2ex,xshift=-0.5ex]mat-1-1.east) 
    -- ++ (-2ex,0) node[left,font=\itshape]{To};
    \draw[stealth-] ([xshift=3ex,yshift=0.5ex]mat-1-1.south west) 
    -- ++ (0,2ex) node[above,font=\itshape]{From};
    \foreach \X/\Y in {2/2,3/2,4/2,5/2,2/3,2/4}
    {\begin{scope}
    \path[pattern={Lines[angle=45,line width=0.7pt]},pattern color=red] 
    let \p1=($(c-\Y-\X.east)-(c-\Y-\X.center)$) in
     (mat-\Y-\X.south west) |- (inlay-\Y-\X.north west)
    |- (inlay-\Y-\X.south east) |- cycle
    (c-\Y-\X.center) circle[radius=\x1];
    \end{scope}}
\end{tikzpicture}
\end{document}

enter image description here