创建一个填充整个宽度的矩形 Tikz 节点

创建一个填充整个宽度的矩形 Tikz 节点

我正在尝试创建一个矩形 Tikz 节点,使其填充整个图形的宽度。我该怎么做?

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

\begin{document}

\begin{tikzpicture}[
    font=\sf \scriptsize,
    >=LaTeX,
    cell/.style={
        rectangle, 
        sharp corners=5mm, 
        draw,
        very thick,
        },
    ]

    \node[cell] (foo) {foo};
    \node[cell, right=of foo] (bar) {bar};
    \node[cell, right=of bar] (baz) {baz};

    \node[cell](input) at (0, -1){I want this all the way};

    \draw[->] (input) -- (foo);
    \draw[->] (input) -- (bar);
    \draw[->] (input) -- (baz);
\end{tikzpicture}
\end{document}

我希望下方矩形的最外边缘与foo和对齐baz。另外,如果箭头完全垂直就更好了。

答案1

您可以测量从节点左侧foo到节点右侧的距离baz,并绘制具有此最小宽度的矩形:

\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] (input) {I want this all the way};
\draw[->]   (input) edge (foo)
            (input) edge (bar)
            (input)  to  (baz);
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:如果您希望input节点与其他节点之间有垂直箭头,则需要按如下方式绘制箭头:

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

在此处输入图片描述

答案2

您已在加载fit库,下面是您可以使用它的方法。(非常感谢@Ignasi 的改进!)

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

\begin{document}

\begin{tikzpicture}[
    font=\sf \scriptsize,
    >=LaTeX,
    cell/.style={
        rectangle, 
        sharp corners=5mm, 
        draw,
        very thick,
        },
    ]
    \begin{scope}[local bounding box=misc]
    \node[cell] (foo) {foo};
    \node[cell, right=of foo] (bar) {bar};
    \node[cell, right=of bar] (baz) {baz};
    \end{scope}
    \node[cell,fit=(foo) (baz),inner sep=-.5\pgflinewidth, below=of bar,
    label=center:I want this all the way](input){}; 

    \draw[->] (input) -- (foo);
    \draw[->] (input) -- (bar);
    \draw[->] (input) -- (baz);
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者

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

\begin{document}

\begin{tikzpicture}[
    font=\sf \scriptsize,
    >=LaTeX,
    cell/.style={
        rectangle, 
        sharp corners=5mm, 
        draw,
        very thick,
        },
    ]
    \begin{scope}[local bounding box=misc]
    \node[cell] (foo) {foo};
    \node[cell, right=of foo] (bar) {bar};
    \node[cell, right=of bar] (baz) {baz};
    \end{scope}
    \node[below=5mm of misc] (aux)  {I want this all the way};
    \node[cell,fit=(aux.north-|misc.west)
    (aux.south-|misc.east),inner sep=0pt](input){};

    \draw[->] (input) -- (foo);
    \draw[->] (input) -- (bar);
    \draw[->] (input) -- (baz);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容