3

I'm trying to create a rectangular Tikz node which fill the entire width of the graphic. How do I do that?

\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}

I'd like for the outer most edges of the lower rectangle to align with foo and baz. Also it would be nice of the arrows were completely vertical.

  • 3
    What is graphic to you? Please, show us some compilable code. – Sigur Dec 19 '18 at 20:53
5

you can measure distance from left side of foo node to right side of the baz node and draw rectangle wit this minimal width:

\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}

enter image description here

edit: if you like to have vertical arrows between input node and other nodes, that you need to draw arrows as follows:

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

enter image description here

  • Is it possible to make the arrows vertical? – Philippe Dec 19 '18 at 21:56
  • yes it is. i will show this asap. – Zarko Dec 19 '18 at 22:15
2

You are already loading the fit library, here is how you may use it. (Big thanks to @Ignasi for the improvements!)

\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}

enter image description here

Or

\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}

enter image description here

  • 1
    It took me also some time to understand that fit nodes can be placed in any position and not only surrounding the fitting coordinates. Therefore you could use \node[cell,fit=(foo) (baz),inner sep=-.5\pgflinewidth, below=of bar](input){}; or whatever way of positioning the node. You don't need the aux coordinate or local bounding box. And a second thing about fitting nodes, it's better to use label=center:xxxxxxx to place the text instead of {}. – Ignasi Dec 20 '18 at 10:35
  • @Ignasi Thank you so much! I didn't know this. – user121799 Dec 20 '18 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.