自动连接 tikz-matrix 节点

自动连接 tikz-matrix 节点

我有这个流程图:

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes,arrows,matrix,decorations.markings}

\begin{document}
\tikzset{
  base/.style={draw,minimum width=2cm,minimum height=1cm},
  start/.style={base,rectangle},
  test/.style={base,diamond,aspect=2},
  end/.style={base,rectangle},
  link/.style={->},
  label/.style={link,%
    postaction={decorate,transform shape,%
      decoration={markings,mark=at position 0 with {\node #1;}}}},
  true/.style={label={[rotate=90,left,anchor=north east]{T}}},
  false/.style={label={[above,anchor=south west]{F}}},
  acl/.style={matrix of nodes,column sep=1cm,row sep=1cm}
}
\begin{tikzpicture}

\matrix (href) [acl] {
  \node [start] {href}; \\
  \node [test] {Authenticated}; & & & & & \node [test] {Invert}; & \node [end] {Unauthorized}; \\
  \node [test] {Member}; & & & \node [test] {Invert}; & \node [end] {Forbidden}; & \node [test] {Grant}; & \node [end] {Unauthorized}; \\
  \node [test] {Invert}; & \node [test] {Grant}; & \node [end] {Forbidden}; & \node [test] {Grant}; & \node [end] {Forbidden}; & \node [end] {OK}; \\
  \node [end] {Forbidden}; & \node [end] {OK}; & & \node [end] {OK}; \\
};

\draw [link] (href-1-1) to (href-2-1);

\draw [true] (href-2-1) to (href-3-1);
\draw [true] (href-3-1) to (href-4-1);
\draw [true] (href-4-1) to (href-5-1);
\draw [true] (href-4-2) to (href-5-2);
\draw [true] (href-4-4) to (href-5-4);
\draw [true] (href-3-4) to (href-4-4);
\draw [true] (href-3-6) to (href-4-6);
\draw [true] (href-2-6) to (href-3-6);

\draw [false] (href-2-1) to (href-2-6);
\draw [false] (href-2-6) to (href-2-7);
\draw [false] (href-3-1) to (href-3-4);
\draw [false] (href-3-4) to (href-3-5);
\draw [false] (href-3-6) to (href-3-7);
\draw [false] (href-4-1) to (href-4-2);
\draw [false] (href-4-2) to (href-4-3);
\draw [false] (href-4-4) to (href-4-5);

\end{tikzpicture}
\end{document}

流程图

因为我即将编写许多这样的内容,所以我想自动化绘制[true][false]路径的过程,因为每个[test]节点都将与下面的第一个节点和其右边的第一个节点相连。

答案1

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes,arrows,matrix,decorations.markings}
\makeatletter
\newif\ifqrr@tikz@rightpath@started
\def\doMyMatrixPaths{\qrr@tikz@saved@rightpaths\qrr@tikz@saved@downpaths\pgfkeysalso{/tikz/initial right and down paths}}
\tikzset{
    every picture/.append style={execute at end picture=\doMyMatrixPaths},
    every matrix/.append style={initial right and down paths},
    initial right and down paths/.code=
        \global\let\qrr@tikz@saved@rightpaths\pgfutil@empty
        \global\let\qrr@tikz@saved@downpaths\pgfutil@empty,
    start right/.code={%
        \toks@\expandafter{\qrr@tikz@saved@rightpaths\path[/tikz/every right path/.try,#1]}%
        \xdef\qrr@tikz@saved@rightpaths{\the\toks@
            (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn) to }%
        \global\qrr@tikz@rightpath@startedtrue
    },
    start right/.default={},
    end right/.code={%
        \ifqrr@tikz@rightpath@started
            \global\qrr@tikz@rightpath@startedfalse
            \toks@\expandafter{\qrr@tikz@saved@rightpaths}%
            \xdef\qrr@tikz@saved@rightpaths{\the\toks@
                (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn); }%
        \fi
    },
    start down/.code={% this goes exactly one row down
        \toks@\expandafter{\qrr@tikz@saved@downpaths\path[/tikz/every down path/.try,#1]}%
        \xdef\qrr@tikz@saved@downpaths{\the\toks@
            (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn) to (\tikzmatrixname-\number\numexpr\pgfmatrixcurrentrow+1\relax-\the\pgfmatrixcurrentcolumn);}%
        }
}
\makeatother
\tikzset{
  base/.style={draw,minimum width=2cm,minimum height=1cm},
  start/.style={base,rectangle},
  test/.style={base,diamond,aspect=2},
  end/.style={base,rectangle},
  link/.style={->},
  Label/.style={link,%
    postaction={decorate,transform shape,%
      decoration={markings,mark=at position 0 with {\node #1;}}}},
  true/.style={draw,Label={[rotate=90,left,anchor=north east]{T}}},
  false/.style={draw,Label={[above,anchor=south west]{F}}},
  acl/.style={matrix of nodes,column sep=1cm,row sep=1cm},
  %
  every down path/.append style={true},
  every right path/.append style={false},
  test/.append style={end right,start right,start down},% end right must appear before start right!
  end/.append style={end right}
}

\begin{document}
\begin{tikzpicture}
\matrix (href) [acl] {
  |[start]| href \\
  \node [test] {Authenticated}; & & & & & \node [test] {Invert}; & \node [end] {Unauthorized}; \\
  \node [test] {Member}; & & & \node [test] {Invert}; & \node [end] {Forbidden}; & \node [test] {Grant}; & \node [end] {Unauthorized}; \\
  \node [test] {Invert}; & \node [test] {Grant}; & \node [end] {Forbidden}; & \node [test] {Grant}; & \node [end] {Forbidden}; & \node [end] {OK}; \\
  \node [end] {Forbidden}; & \node [end] {OK}; & & \node [end] {OK}; \\
};
\draw [link] (href-1-1) to (href-2-1);

\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容