更新无shorten =-3pt

更新无shorten =-3pt

matrix加载库并matrix of nodes激活密钥后,就可以根据name-row-column语法访问每个节点。

在某些情况下,箭头内置于相反的方向请求的内容。为了说明这一点,我有颜色为红色箭头所指的相反的方向所请求的内容。

我涂上了颜色细胞背景从中箭头以蓝色开始

更新无shorten <=-3pt,shorten >=-3pt

默认情况下,当单元格并排放置时,箭头会反转。

截屏

\documentclass[border=10pt,tikz]{standalone}

\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}
\matrix (magic) [matrix of nodes]
{
8 & 1 & 6 \\
3 & 5 & 7 \\
4 & 9 & 2 \\
};
\draw[thick,blue,->] (magic-1-1) |- (magic-2-3);
\draw[thick,blue,->] (magic-1-1) -- (magic-2-3);
\draw[thick,red,->] (magic-1-1) -- (magic-1-2);
\end{tikzpicture}

\end{document}

截屏

\documentclass[border=10pt,tikz]{standalone}

\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}
\matrix (magic) [matrix of nodes,
row 1 column 1/.style={nodes={fill=blue!20}},
row 2 column 3/.style={nodes={fill=blue!20}},
row 4 column 1/.style={nodes={fill=blue!20}},
row 1 column 5/.style={nodes={fill=blue!20}},
row 2 column 6/.style={nodes={fill=blue!20}},
row 3 column 5/.style={nodes={fill=blue!20}},
row 4 column 6/.style={nodes={fill=blue!20}},
row 7 column 4/.style={nodes={fill=blue!20}},
row 4 column 4/.style={nodes={fill=blue!20}}]
{
8 & 8 & 8 & 8 & 8 & 8 & 8\\
3 & 3 & 5 & 7 & 7 & 6 & 5\\
4 & 4 & 9 & 2 & 6 & 5 & 4 \\
4 & 4 & 9 & 2 & 5 & 4 & 3 \\
8 & 8 & 8 & 8 & 8 & 8 & 8\\
3 & 3 & 5 & 7 & 7 & 6 & 5\\
4 & 4 & 9 & 2 & 6 & 5 & 4 \\
4 & 4 & 9 & 2 & 5 & 4 & 3 \\
};
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-1-1) |- (magic-2-2);
\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-1-1) -- (magic-2-2);
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-1-1) -| (magic-2-2);


\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-4-4) |- (magic-3-3);
\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-4-4) -- (magic-3-3);
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-4-4) -| (magic-3-3);

\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-4-1) -- (magic-3-2);
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-4-1) |- (magic-3-2);
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-4-1) -| (magic-3-2);

\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-2-3) -- (magic-1-4);
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-2-3) |- (magic-1-4);
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-2-3) -| (magic-1-4);

\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-1-5) -- (magic-2-5);
\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-2-6) -- (magic-1-6);

\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-3-5) -- (magic-3-6);
\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-4-6) -- (magic-4-5);

\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-6-1);
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-6-2);
\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-6-3);
\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-6-4);
\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-6-5);
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-6-6);
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-6-7);

\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-8-1);
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-8-2);
\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-8-3);
\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-8-4);
\draw[thick,red,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-8-5);
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-8-6);
\draw[thick,blue,->,shorten <=-3pt,shorten >=-3pt] (magic-7-4) -- (magic-8-7);
\end{tikzpicture}

\end{document}

答案1

当节点名称用作起点或目标坐标时,TikZ 的线条绘制操作非常智能,因为 TikZ 将节点边界上的点作为真正的起点或目标点。

尽管它很智能,但是当节点彼此太靠近(或太大)时,它就不会产生最佳线路。

何时(a) -- (b)使用 TikZ 询问 PGF

  • 对于边界上的点,a其位于b中心的方向(即b.center),并且
  • 因为边界上的点b位于a的中心方向。

看一下这个例子:

\begin{tikzpicture}[->, nodes={draw,gray}]
\node (a) {a};
\node at (.1,1) (b) {b};
\draw (a) |- (b);

\tikzset{xshift=1cm}
\node[blue] (a) {a};
\node[green] at (.1,.4) (b) {b};
\draw[thick, shorten >=-3pt, shorten <=-3pt] (a) -- (b);
\draw[red] (a) -- (b);
\end{tikzpicture}

在此处输入图片描述

右侧的红色箭头从 的边界开始a 并到 的边界b(这就是您要求的(a) -- (b))。

shorten路径的设置在该过程的后期完成,在这里无法帮助您。)

-|和路径运算符也发生了类似的事情|-。当 TikZ 发现(a) |- (b)它询问 PGF

  • 对于边界上的点,a该点要么向上(当在当前坐标系中b高于时),要么向下(当在低于时),并且aba
  • 因为 边界上的点b要么在 左边(当a在 左边时b),要么在 右边(当a在 右边时b)。

由于距离它a的左侧非常近,因此请选择边界上最左边的点,即使它比的中心b 更靠左。a


但边界究竟在哪里?使用 PGF/TikZ 的默认设置,边界位于形状路径的外部。

但是我们可以根据形状的路径进行更改,这就是/pgf/outer xsep和的值的作用/pgf/outer ysep。事实上,默认情况下,设置为 ,.5\pgflinewidth 这样边框就恰好位于线的边缘。

因此,shorten我们不用设置路径,而是使用如下方法使边框小于形状:

outer sep = -3pt

我选择-.1666em这个例子是因为我认为它看起来更好而且它的长度是默认长度inner sep(文本和形状路径之间的空间)的一半。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}[->, nodes={draw,gray}]
\node (a) {a};
\node at (.1,1) (b) {b};
\draw (a) |- (b);

\tikzset{xshift=1cm}
\node[blue] (a) {a};
\node[green] at (.1,.4) (b) {b};
\draw[thick, shorten >=-3pt, shorten <=-3pt] (a) -- (b);
\draw[red] (a) -- (b);
\end{tikzpicture}

\begin{tikzpicture}[
  rc/.style 2 args={
    @/.style args={r##1c##2}{row ##1 column ##2/.append style={#2}},
    @/.list={#1}}
]
\matrix (magic) [
  matrix of nodes,
  nodes={outer sep=-.1666em},
  rc={r1c1, r2c3, r4c1, r1c5, r2c6, r3c5, r4c6, r7c4, r4c4}
     {nodes={fill=blue!20}},
] {
    8 & 8 & 8 & 8 & 8 & 8 & 8 \\
    3 & 3 & 5 & 7 & 7 & 6 & 5 \\
    4 & 4 & 9 & 2 & 6 & 5 & 4 \\
    4 & 4 & 9 & 2 & 5 & 4 & 3 \\
    8 & 8 & 8 & 8 & 8 & 8 & 8 \\
    3 & 3 & 5 & 7 & 7 & 6 & 5 \\
    4 & 4 & 9 & 2 & 6 & 5 & 4 \\
    4 & 4 & 9 & 2 & 5 & 4 & 3 \\
};

\begin{scope}[thick, ->]
    \draw[blue] (magic-1-1) |- (magic-2-2);
    \draw[red] (magic-1-1) -- (magic-2-2);
    \draw[blue] (magic-1-1) -| (magic-2-2);
    
    \draw[blue] (magic-4-4) |- (magic-3-3);
    \draw[red] (magic-4-4) -- (magic-3-3);
    \draw[blue] (magic-4-4) -| (magic-3-3);
    
    \draw[red] (magic-4-1) -- (magic-3-2);
    \draw[blue] (magic-4-1) |- (magic-3-2);
    \draw[blue] (magic-4-1) -| (magic-3-2);
    
    \draw[red] (magic-2-3) -- (magic-1-4);
    \draw[blue] (magic-2-3) |- (magic-1-4);
    \draw[blue] (magic-2-3) -| (magic-1-4);
    
    \draw[red] (magic-1-5) -- (magic-2-5);
    \draw[red] (magic-2-6) -- (magic-1-6);
    
    \draw[red] (magic-3-5) -- (magic-3-6);
    \draw[red] (magic-4-6) -- (magic-4-5);
    
    \foreach \row in {6, 8} {
      \path[blue] (magic-7-4) edge (magic-\row-1)
                              edge (magic-\row-2)
                              edge (magic-\row-6)
                              edge (magic-\row-7);
      \path[red]  (magic-7-4) edge (magic-\row-3)
                              edge (magic-\row-4)
                              edge (magic-\row-5);
    }
\end{scope}
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

答案2

节点互相接触。

使用inner sep=0pt,我创建较小的节点,并使用row sepcolumn sep将它们分隔开。

使用shorten > =0ptshorten < = 0pt,我在节点之间绘制“精确”箭头,它们符合预期(节点彼此不接触)。

\documentclass[border=10pt,tikz]{standalone}

\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}
\matrix (magic) [matrix of nodes,inner sep = 0pt, row sep = 2mm, column sep  =2mm,
row 1 column 1/.style={nodes={fill=blue!20}},
row 2 column 3/.style={nodes={fill=blue!20}},
row 4 column 1/.style={nodes={fill=blue!20}},
row 1 column 5/.style={nodes={fill=blue!20}},
row 2 column 6/.style={nodes={fill=blue!20}},
row 3 column 5/.style={nodes={fill=blue!20}},
row 4 column 6/.style={nodes={fill=blue!20}},
row 7 column 4/.style={nodes={fill=blue!20}},
row 4 column 4/.style={nodes={fill=blue!20}}]
{
8 & 8 & 8 & 8 & 8 & 8 & 8\\
3 & 3 & 5 & 7 & 7 & 6 & 5\\
4 & 4 & 9 & 2 & 6 & 5 & 4 \\
4 & 4 & 9 & 2 & 5 & 4 & 3 \\
8 & 8 & 8 & 8 & 8 & 8 & 8\\
3 & 3 & 5 & 7 & 7 & 6 & 5\\
4 & 4 & 9 & 2 & 6 & 5 & 4 \\
4 & 4 & 9 & 2 & 5 & 4 & 3 \\
};
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-1-1) |- (magic-2-2);
\draw[thick,red, ->,shorten <=0pt,shorten >=0pt] (magic-1-1) -- (magic-2-2);
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-1-1) -| (magic-2-2);


\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-4-4) |- (magic-3-3);
\draw[thick,red, ->,shorten <=0pt,shorten >=0pt] (magic-4-4) -- (magic-3-3);
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-4-4) -| (magic-3-3);

\draw[thick,red, ->,shorten <=0pt,shorten >=0pt] (magic-4-1) -- (magic-3-2);
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-4-1) |- (magic-3-2);
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-4-1) -| (magic-3-2);

\draw[thick,red, ->,shorten <=0pt,shorten >=0pt] (magic-2-3) -- (magic-1-4);
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-2-3) |- (magic-1-4);
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-2-3) -| (magic-1-4);

\draw[thick,red,->,shorten <=0pt,shorten >=0pt] (magic-1-5) -- (magic-2-5);
\draw[thick,red,->,shorten <=0pt,shorten >=0pt] (magic-2-6) -- (magic-1-6);

\draw[thick,red,->,shorten <=0pt,shorten >=0pt] (magic-3-5) -- (magic-3-6);
\draw[thick,red,->,shorten <=0pt,shorten >=0pt] (magic-4-6) -- (magic-4-5);

\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-6-1);
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-6-2);
\draw[thick,red,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-6-3);
\draw[thick,red,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-6-4);
\draw[thick,red,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-6-5);
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-6-6);
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-6-7);

\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-8-1);
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-8-2);
\draw[thick,red,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-8-3);
\draw[thick,red,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-8-4);
\draw[thick,red,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-8-5);
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-8-6);
\draw[thick,blue,->,shorten <=0pt,shorten >=0pt] (magic-7-4) -- (magic-8-7);
\end{tikzpicture}

\end{document}

上述代码的输出

相关内容