![如何对该表进行图案填充(不包括圆圈区域)?](https://linux22.com/image/414643/%E5%A6%82%E4%BD%95%E5%AF%B9%E8%AF%A5%E8%A1%A8%E8%BF%9B%E8%A1%8C%E5%9B%BE%E6%A1%88%E5%A1%AB%E5%85%85%EF%BC%88%E4%B8%8D%E5%8C%85%E6%8B%AC%E5%9C%86%E5%9C%88%E5%8C%BA%E5%9F%9F%EF%BC%89%EF%BC%9F.png)
我想圈出矩阵表中的元素(如何在表格单元格的顶角创建一个带有边框的小单元格?) 并对矩阵表进行填充图案处理,不包括带圆圈的数字和嵌体。我还想用红色填充需求和供应中的单元格,如图所示。请帮帮我。
请注意,图中所画的红线是近似的,在最终的解决方案中,我希望有完美倾斜的线组(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}