\usepackage{tikz,pgfplots}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows,shadows,patterns}
\tikzset{
mymat/.style={
matrix of math nodes,
text height=2.5ex,
text depth=0.75ex,
text width=3.25ex,
align=center,
column sep=-\pgflinewidth
},
mymats/.style={
mymat,
nodes={draw,fill=#1}
}
}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[>=latex]
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,0)
(mat1)
{
& \\
N1 \\
};
\matrix[mymat,right=of mat1,row 2/.style={nodes={draw,fill=gray!30}}]
(mat2)
{
& \\
t_2 & t_4 \\
};
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,-1)
(mat3)
{
& \\
N2 \\
};
\matrix[mymat,right=of mat3,row 2/.style={nodes={draw,fill=gray!30}}]
(mat4)
{
& & \\
t_1 & t_3 & t_4 \\
};
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,-2)
(mat5)
{
& \\
N3 \\
};
\matrix[mymat,right=of mat5,row 2/.style={nodes={draw,fill=gray!30}}]
(mat6)
{
& &\\
t_1 & t_2 & t_3 \\
};
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,-3)
(mat7)
{
& \\
N4 \\
};
\matrix[mymat,right=of mat7,row 2/.style={nodes={draw,fill=gray!30}}]
(mat8)
{
& \\
t_3 & t_4 \\
};
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,-4)
(mat9)
{
& \\
N5 \\
};
\matrix[mymat,right=of mat9,row 2/.style={nodes={draw,fill=gray!30}}]
(mat10)
{
& \\
t_1 & t_3 \\
};
\matrix[mymat, anchor=west,row 2/.style={nodes={draw,fill=gray!30}}]
at (1.65,-5.5)
(mat11)
{
& & & \\
t_1 & t_2 & t_3 & t_4\\
};
\node[ draw, red, dashed, line width=0.7pt, fit=(mat2)(mat4)(mat6)(mat8)
(mat10)](node1){};
\begin{scope}[shorten <= -2pt]
\draw[*->]
(mat1-2-1.east) -- (mat2-2-1.west);
\draw[*->]
(mat3-2-1.east) -- (mat4-2-1.west);
\draw[*->]
(mat5-2-1.east) -- (mat6-2-1.west);
\draw[*->]
(mat7-2-1.east) -- (mat8-2-1.west);
\draw[*->]
(mat9-2-1.east) -- (mat10-2-1.west);
\end{scope}
\draw[green, dashed](node1.south west) -- (mat11.north west);
\draw[green, dashed](node1.south east) -- (mat11.north east);
\end{tikzpicture}
\end{figure}
\end{document}
无法用绿色虚线连接 node1 和 mat11 的角。
部分代码取自这里
提供解决方案这里虽然很优雅,但对这个人物来说却不起作用。
答案1
如果我理解正确的话,我认为你的问题来自于这样一个事实:a\matrix
只是 的一种特殊类型\node
,并且像所有节点一样,它有一个(默认情况下非零)inner sep
。你可以看到,如果你将 添加到draw
的选项中mat11
,这样你就有了
\matrix[mymat, draw, anchor=west,row 2/.style={nodes={draw,fill=gray!30}}]
at (1.65,-5.5)
(mat11)
{
& & & \\
t_1 & t_2 & t_3 & t_4\\
};
结果是:
因此,您绘制的线条指向外边界的角,而不是节点的角。
我想到两种解决方法:
画线到
mat11-2-1.north west
和mat11-2-4.north east
而不是mat11.north west
/mat11.north east
。这样线就指向矩阵内部的节点,而不是矩阵本身。\draw[green, dashed](node1.south west) -- (mat11-2-1.north west); \draw[green, dashed](node1.south east) -- (mat11-2-4.north east);
inner sep
将设置mat11
为 0,但随后必须将 设置为inner sep
,即默认值:nodes
0.333em
\matrix[mymat, inner sep=0, anchor=west,row 2/.style={nodes={draw,fill=gray!30,inner sep=0.333em}}] at (1.65,-5.5) (mat11) { & & & \\ t_1 & t_2 & t_3 & t_4\\ };
使用两种方法完成的代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,positioning,arrows}
\tikzset{
mymat/.style={
matrix of math nodes,
text height=2.5ex,
text depth=0.75ex,
text width=3.25ex,
align=center,
column sep=-\pgflinewidth,
},
mymats/.style={
mymat,
nodes={draw,fill=#1}
}
}
\begin{document}
\begin{tikzpicture}[>=latex]
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,0)
(mat1)
{
& \\
N1 \\
};
\matrix[mymat,right=of mat1,row 2/.style={nodes={draw,fill=gray!30}}]
(mat2)
{
& \\
t_2 & t_4 \\
};
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,-1)
(mat3)
{
& \\
N2 \\
};
\matrix[mymat,right=of mat3,row 2/.style={nodes={draw,fill=gray!30}}]
(mat4)
{
& & \\
t_1 & t_3 & t_4 \\
};
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,-2)
(mat5)
{
& \\
N3 \\
};
\matrix[mymat,right=of mat5,row 2/.style={nodes={draw,fill=gray!30}}]
(mat6)
{
& &\\
t_1 & t_2 & t_3 \\
};
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,-3)
(mat7)
{
& \\
N4 \\
};
\matrix[mymat,right=of mat7,row 2/.style={nodes={draw,fill=gray!30}}]
(mat8)
{
& \\
t_3 & t_4 \\
};
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,-4)
(mat9)
{
& \\
N5 \\
};
\matrix[mymat,right=of mat9,row 2/.style={nodes={draw,fill=gray!30}}]
(mat10)
{
& \\
t_1 & t_3 \\
};
\matrix[mymat, inner sep=0, anchor=west,row 2/.style={nodes={draw,fill=gray!30,inner sep=0.333em}}]
at (1.65,-5.5)
(mat11)
{
& & & \\
t_1 & t_2 & t_3 & t_4\\
};
\node[ draw, red, dashed, line width=0.7pt, fit=(mat2)(mat4)(mat6)(mat8)
(mat10)](node1){};
\begin{scope}[shorten <= -2pt]
\draw[*->]
(mat1-2-1.east) -- (mat2-2-1.west);
\draw[*->]
(mat3-2-1.east) -- (mat4-2-1.west);
\draw[*->]
(mat5-2-1.east) -- (mat6-2-1.west);
\draw[*->]
(mat7-2-1.east) -- (mat8-2-1.west);
\draw[*->]
(mat9-2-1.east) -- (mat10-2-1.west);
\end{scope}
\draw[green, dashed](node1.south west) -- (mat11.north west);
\draw[green, dashed](node1.south east) -- (mat11.north east);
\end{tikzpicture}
\bigskip
\begin{tikzpicture}[>=latex]
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,0)
(mat1)
{
& \\
N1 \\
};
\matrix[mymat,right=of mat1,row 2/.style={nodes={draw,fill=gray!30}}]
(mat2)
{
& \\
t_2 & t_4 \\
};
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,-1)
(mat3)
{
& \\
N2 \\
};
\matrix[mymat,right=of mat3,row 2/.style={nodes={draw,fill=gray!30}}]
(mat4)
{
& & \\
t_1 & t_3 & t_4 \\
};
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,-2)
(mat5)
{
& \\
N3 \\
};
\matrix[mymat,right=of mat5,row 2/.style={nodes={draw,fill=gray!30}}]
(mat6)
{
& &\\
t_1 & t_2 & t_3 \\
};
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,-3)
(mat7)
{
& \\
N4 \\
};
\matrix[mymat,right=of mat7,row 2/.style={nodes={draw,fill=gray!30}}]
(mat8)
{
& \\
t_3 & t_4 \\
};
\matrix[mymat,anchor=west,row 2/.style={nodes=draw}]
at (0,-4)
(mat9)
{
& \\
N5 \\
};
\matrix[mymat,right=of mat9,row 2/.style={nodes={draw,fill=gray!30}}]
(mat10)
{
& \\
t_1 & t_3 \\
};
\matrix[mymat, anchor=west,row 2/.style={nodes={draw,fill=gray!30}}]
at (1.65,-5.5)
(mat11)
{
& & & \\
t_1 & t_2 & t_3 & t_4\\
};
\node[ draw, red, dashed, line width=0.7pt, fit=(mat2)(mat4)(mat6)(mat8)
(mat10)](node1){};
\begin{scope}[shorten <= -2pt]
\draw[*->]
(mat1-2-1.east) -- (mat2-2-1.west);
\draw[*->]
(mat3-2-1.east) -- (mat4-2-1.west);
\draw[*->]
(mat5-2-1.east) -- (mat6-2-1.west);
\draw[*->]
(mat7-2-1.east) -- (mat8-2-1.west);
\draw[*->]
(mat9-2-1.east) -- (mat10-2-1.west);
\end{scope}
\draw[green, dashed](node1.south west) -- (mat11-2-1.north west);
\draw[green, dashed](node1.south east) -- (mat11-2-4.north east);
\end{tikzpicture}
\end{document}
附录
正如评论中提到的,我认为没有必要\matrix
对N
左侧的节点使用 。我也不明白矩阵中第一行空白的用途,似乎你添加了很多不必要的代码。这是制作图表的另一种方法。我利用库chains
来创建N
左侧的节点(\matrix
实际上也可以使用 )。所有东西都是相对于其他东西放置的,因此没有使用明确的坐标。为了绘制箭头,我使用了一个循环。
我使用了arrows.meta
库而不是arrows
,因为 TikZ 认为后者已被弃用,而前者则被青睐(请参阅版本 3.0.1a 手册第 512 页)。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,positioning,chains,arrows.meta}
\tikzset{
nodestyle/.style={
text height=2.5ex,
text depth=0.75ex,
text width=3.25ex,
align=center,
draw
},
mymat/.style={
matrix of math nodes,
nodes={
nodestyle,
fill=gray!30,
},
column sep=-\pgflinewidth,
}
}
\begin{document}
\begin{tikzpicture}[node distance=1.2cm]
\begin{scope}[
start chain=N going below,
every node/.style={
nodestyle,
on chain
},
node distance=2mm
]
\node {$N1$};
\node {$N2$};
\node {$N3$};
\node {$N4$};
\node {$N5$};
\end{scope}
\matrix [mymat,right=of N-1, name=m1] {t_2 & t_4 \\ };
\matrix [mymat,right=of N-2, name=m2] {t_1 & t_3 & t_4 \\ };
\matrix [mymat,right=of N-3, name=m3] {t_1 & t_2 & t_3 \\ };
\matrix [mymat,right=of N-4, name=m4] {t_3 & t_4 \\ };
\matrix [mymat,right=of N-5, name=m5] {t_1 & t_3 \\ };
\node [draw, red, dashed, fit=(m1)(m2)(m3)(m4)(m5), name=M] {};
\foreach \i in {1,...,5}
\draw [Circle-Latex,shorten <=-2pt] (N-\i) -- (m\i-1-1);
\matrix [mymat,below=5mm of M, name=m11] {t_1 & t_2 & t_3 & t_4 \\ };
\draw[green, dashed](M.south west) -- (m11-1-1.north west);
\draw[green, dashed](M.south east) -- (m11-1-4.north east);
\end{tikzpicture}
\end{document}