我想圈出矩阵表中的元素(如何在表格单元格的顶角创建一个带有边框的小单元格?) 并对矩阵表进行填充图案处理,不包括带圆圈的数字和嵌体。我还想用红色填充需求和供应中的单元格,如图所示。请帮帮我。
请注意,图中所画的红线是近似的,在最终的解决方案中,我希望有完美倾斜的线组(45 度阴影线)。
\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}
答案1
您可以制作带圆圈的节点内容标签,根据矩阵中的位置命名,以便可以从外部访问它们,还可以命名嵌体。这允许您填充该区域。
\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}