![在矩阵内画线](https://linux22.com/image/308794/%E5%9C%A8%E7%9F%A9%E9%98%B5%E5%86%85%E7%94%BB%E7%BA%BF.png)
我有以下代码来绘制matrix
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
\matrix[
matrix of math nodes,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
nodes={anchor=center, minimum width=2cm, cell},
column 1/.style = {nodes={minimum width=1cm, fill=mlightgray}},
column 2/.style = {nodes={minimum width=3.2cm}},
column 3/.style = {nodes={minimum width=3.2cm}},
row 1/.style = {nodes={text height=1.3ex, text depth=0, fill=mlightgray}},
row 2/.style = {text height=1.3ex, text depth=0},
row 3/.style = {text height=1.3ex, text depth=0},
row 4/.style = {text height=1.3ex, text depth=0},
]
{ & \text{1} & \text{2} \\
\text{1} & & \\
\text{2} & & \\
\text{3} & & \\
};
\end{tikzpicture}
\end{document}
现在,我想在单元格之间画线来实现以下内容(由生成pspicture
,但我想将所有内容转换为 tikz):
是否可以?
PS:我不需要一个通用的解决方案,只要能实现这个数字就足够了。
答案1
你可以给矩阵加上标签,然后使用(m-1-2)
语法来绘制。这意味着矩阵的第一行和第二列(m)
(即1-2
条目的索引)。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
\matrix[
matrix of math nodes,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
nodes={anchor=center, minimum width=2cm, cell},
column 1/.style = {nodes={minimum width=1cm, fill=lightgray}},
column 2/.style = {nodes={minimum width=3.2cm}},
column 3/.style = {nodes={minimum width=3.2cm}},
row 1/.style = {nodes={text height=1.3ex, text depth=0, fill=lightgray}},
row 2/.style = {text height=1.3ex, text depth=0},
row 3/.style = {text height=1.3ex, text depth=0},
row 4/.style = {text height=1.3ex, text depth=0},
](m)
{ & 1 & 2 \\
1 & \bullet & \bullet \\
2 & \bullet & \bullet \\
3 & \bullet & \bullet \\
};
\draw[-latex] (m-4-3.center) -- (m-3-2.center);
\draw[-latex] (m-4-3.center) -- (m-3-3.center);
\draw[-latex] (m-3-2.center) -- (m-2-2.center);
\draw[-latex] (m-3-3.center) -- (m-2-2.center);
\end{tikzpicture}
\end{document}
答案2
全自动 PSTricks 解决方案:
\documentclass{article}
\usepackage{multido}
\usepackage{pstricks}
\usepackage{xfp}
% settings
\psset{
dimen = m,
fillcolor = gray!60
}
% parameters
\def\Columns{2}
\def\ColumnWidth{3}
\def\Rows{3}
\def\RowHeight{0.7}
\begin{document}
\begin{pspicture}(\fpeval{\Columns*\ColumnWidth+1},\fpeval{(\Rows+1)*\RowHeight})
\multido{\r = 0+\RowHeight}{\fpeval{\Rows+1}}{%
\psframe[fillstyle = solid](0,\r)(1,\fpeval{\r+\RowHeight})%
}
\multido{
\rE = 0+\RowHeight,
\rF = \fpeval{0.5*\RowHeight}+\RowHeight,
\iB = \Rows+-1
}{\Rows}{%
\multido{
\rA = 1+\ColumnWidth,
\rB = \fpeval{0.5*\ColumnWidth+1}+\ColumnWidth,
\rC = \fpeval{0.5*\RowHeight}+\RowHeight,
\rD = \fpeval{1.5*\RowHeight}+-\RowHeight,
\iA = 1+1
}{\Columns}{%
\psframe[fillstyle = solid]%
(\rA,\fpeval{\Rows*\RowHeight})%
(\fpeval{\rA+\ColumnWidth},\fpeval{(\Rows+1)*\RowHeight})
\rput(\rB,\fpeval{(\Rows+0.5)*\RowHeight}){$\iA$}
\psframe(\rA,\rE)(\fpeval{\rA+\ColumnWidth},\fpeval{\rE+\RowHeight})
\rput(\rB,\rF){\psdot}}
\rput(0.5,\rF){$\iB$}}
\psline{->}(\fpeval{1+(\Columns-0.5)*\ColumnWidth},\fpeval{0.5*\RowHeight})%
(\fpeval{1+0.5*\ColumnWidth},\fpeval{(\Rows-1.5)*\RowHeight})
\psline{->}(\fpeval{1+0.5*\ColumnWidth},\fpeval{(\Rows-1.5)*\RowHeight})%
(\fpeval{1+0.5*\ColumnWidth},\fpeval{(\Rows-0.5)*\RowHeight})
\psline{->}(\fpeval{1+(\Columns-0.5)*\ColumnWidth},\fpeval{0.5*\RowHeight})%
(\fpeval{1+(\Columns-0.5)*\ColumnWidth},\fpeval{1.5*\RowHeight})
\psline{->}(\fpeval{1+(\Columns-0.5)*\ColumnWidth},\fpeval{1.5*\RowHeight})%
(\fpeval{1+0.5*\ColumnWidth},\fpeval{(\Rows-0.5)*\RowHeight})
\end{pspicture}
\end{document}
您所要做的就是改变参数的值,绘图就会相应地调整。
更新
如果您需要宏,可以按如下方式操作:
\documentclass{article}
\usepackage{multido}
\usepackage{pstricks}
\usepackage{xfp}
% settings
\psset{
dimen = m,
fillcolor = gray!60
}
% \Table[<cell width>,<cell height>]{<number of columns}{<number of rows>}
\def\Table[#1,#2]#3#4{%
\begin{pspicture}(\fpeval{#3*#1+1},\fpeval{(#4+1)*#2})
\multido{\r = 0+#2}{\fpeval{#4+1}}{%
\psframe[fillstyle = solid](0,\r)(1,\fpeval{\r+#2})%
}
\multido{
\rE = 0+#2,
\rF = \fpeval{0.5*#2}+#2,
\iB = #4+-1
}{#4}{%
\multido{
\rA = 1+#1,
\rB = \fpeval{0.5*#1+1}+#1,
\rC = \fpeval{0.5*#2}+#2,
\rD = \fpeval{1.5*#2}+-#2,
\iA = 1+1
}{#3}{%
\psframe[fillstyle = solid]%
(\rA,\fpeval{#4*#2})%
(\fpeval{\rA+#1},\fpeval{(#4+1)*#2})
\rput(\rB,\fpeval{(#4+0.5)*#2}){$\iA$}
\psframe(\rA,\rE)(\fpeval{\rA+#1},\fpeval{\rE+#2})
\rput(\rB,\rF){\psdot}}
\rput(0.5,\rF){$\iB$}}
\psline{->}(\fpeval{1+(#3-0.5)*#1},\fpeval{0.5*#2})%
(\fpeval{1+0.5*#1},\fpeval{(#4-1.5)*#2})
\psline{->}(\fpeval{1+0.5*#1},\fpeval{(#4-1.5)*#2})%
(\fpeval{1+0.5*#1},\fpeval{(#4-0.5)*#2})
\psline{->}(\fpeval{1+(#3-0.5)*#1},\fpeval{0.5*#2})%
(\fpeval{1+(#3-0.5)*#1},\fpeval{1.5*#2})
\psline{->}(\fpeval{1+(#3-0.5)*#1},\fpeval{1.5*#2})%
(\fpeval{1+0.5*#1},\fpeval{(#4-0.5)*#2})
\end{pspicture}}
\begin{document}
\Table[3,0.7]{2}{3}
\end{document}
\end{document}