如何在 Tikz 中的两个节点之间绘制多条线

如何在 Tikz 中的两个节点之间绘制多条线

如果我从这个开始。我该如何在两个长矩形之间绘制与上方三个箭头对齐的三条线?

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta, calc, fit, positioning}

\begin{document}

    \begin{tikzpicture}[
          > = LaTeX,
cell/.style = {rectangle, draw, very thick, outer sep=0pt,
               font=\sffamily\scriptsize},
                ]
\node[cell] (foo) {foo};
\node[cell, right=of foo] (bar) {bar};
\node[cell, right=of bar] (baz) {baz};
\path   let \p1 = ($(foo.west)-(baz.east)$),
            \n1 = {veclen(\x1,\y1)} in
        node[cell, minimum width=\n1,
             below right=1cm and 0cm of foo.south west] (longTime1) {Look how long I am};

\draw[->]   (longTime1.north -| foo) edge (foo)
            (longTime1.north -| bar) edge (bar)
            (longTime1.north -| baz)  to  (baz);

\path   let \p1 = ($(foo.west)-(baz.east)$),
            \n1 = {veclen(\x1,\y1)} in
        node[cell, minimum width=\n1,
             below right=1cm and 0cm of longTime1.south west] (longTime2) {I'm also long};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta, calc, fit, positioning}

\begin{document}

    \begin{tikzpicture}[
          > = LaTeX,
cell/.style = {rectangle, draw, very thick, outer sep=0pt,
               font=\sffamily\scriptsize},
                ]
\node[cell] (foo) {foo};
\node[cell, right=of foo] (bar) {bar};
\node[cell, right=of bar] (baz) {baz};
\path   let \p1 = ($(foo.west)-(baz.east)$),
            \n1 = {veclen(\x1,\y1)} in
        node[cell, minimum width=\n1,
             below right=1cm and 0cm of foo.south west] (longTime1) {Look how long I am};

\draw[->]   (longTime1.north -| foo) edge (foo)
            (longTime1.north -| bar) edge (bar)
            (longTime1.north -| baz)  to  (baz);

\path   let \p1 = ($(foo.west)-(baz.east)$),
            \n1 = {veclen(\x1,\y1)} in
        node[cell, minimum width=\n1,
             below right=1cm and 0cm of longTime1.south west] (longTime2) {I'm also long};
\foreach \X in {foo,bar,baz}             
{\draw (longTime1.south -| \X.center) -- (longTime2.north -| \X.center);}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

与@marmot 的答案类似,但代码更短......

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta, calc, positioning}

\begin{document}

    \begin{tikzpicture}[
node distance = 10mm and 10mm,  %  <---
            > = LaTeX,
  cell/.style = {rectangle, draw, very thick, outer sep=0pt,
                font=\sffamily\scriptsize},
                        ]
\node[cell] (foo) {foo};
\node[cell, right=of foo] (bar) {bar};
\node[cell, right=of bar] (baz) {baz};
\path   let \p1 = ($(foo.west)-(baz.east)$),
            \n1 = {veclen(\x1,\y1)} in
        node[cell, minimum width=\n1,
             below right=1cm and 0cm of foo.south west] 
             (longTime1) {Look how long I am}
        node[cell, minimum width=\n1,                   % nodes longTime1 and longTime2 have the same width
             below=of longTime1]                        % <--- 
             (longTime2) {I'm also long};               
\foreach \i in {foo,bar,baz}                            % loop for all arrows
{   
\draw[->]   (longTime1.north -| \i) -- (\i);
\draw       (longTime1.south -| \i) -- (longTime2.north -| \i);
}
    \end{tikzpicture}
\end{document}

结果与@marmot 的回答相同,因此无需在此重复:-)

答案3

另一种可能性是使用库在背景中绘制箭头backgrounds。然后将节点的背景颜色longTime1设为白色,这样下面的箭头线将不可见。

背景

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta, calc, fit, positioning}
\usetikzlibrary{backgrounds}% 

\begin{document}

    \begin{tikzpicture}[
          > = LaTeX,
cell/.style = {rectangle, draw, very thick, outer sep=0pt,
               font=\sffamily\scriptsize},
                ]
\node[cell] (foo) {foo};
\node[cell, right=of foo] (bar) {bar};
\node[cell, right=of bar] (baz) {baz};

\path   let \p1 = ($(foo.west)-(baz.east)$),
            \n1 = {veclen(\x1,\y1)} in
        node[cell, minimum width=\n1,
             below right=1cm and 0cm of foo.south west,fill=white] (longTime1) {Look how long I am}; % fill the background of the node in white

\path   let \p1 = ($(foo.west)-(baz.east)$),
            \n1 = {veclen(\x1,\y1)} in
        node[cell, minimum width=\n1,
             below right=1cm and 0cm of longTime1.south west] (longTime2) {I'm also long};

\begin{scope}[on background layer]% arrows are drawn on the background
\draw[->]   (longTime2.north -| foo) edge (foo)
            (longTime2.north -| bar) edge (bar)
            (longTime2.north -| baz)  to  (baz); 
\end{scope}      

\end{tikzpicture}
\end{document}

相关内容