1

A chain of nodes is drawn using tikz chains. I would like to draw a rectangle node covering the chain nodes (or, multiple rectangle nodes covering subsets of them). I am using tikz fit.

Unfortunately, by the way I use it, the rectangle nodes are considered part of the chains. As a result, additional join edges are drawn to the rectangle positions are kind of messed up. The following are the MWE:

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{chains, fit}

\begin{document}
\begin{tikzpicture}[start chain, node distance=5mm, every node/.style={draw, on chain, join}, every join/.style=->]
  \node (a)  {a};
  \node (b)  {b};
  \node  (c)  {c};
  \node  (d)  {d};
  \begin{scope} [start branch=numbers, every join/.style=<-]
    \node (1) [on chain=going above] {1};
    \node (2) [on chain=going above] {2};
  \end{scope}
  \node  (e)  {e};
  \node (fit2) [draw, inner sep=2pt, fit=(a) (b) ] {};
  \node (fit1) [draw, inner sep=2pt, fit=(d) (1) (e)] {};
\end{tikzpicture}
\end{document}

The result, without and with the covering rectangles, are

enter image description here

enter image description here

1 Answer 1

1

Don’t include the last two nodes in the scope where you say every node/.style=on chain or overwrite the every node style again.

Code A

\documentclass[border=5pt, tikz]{standalone}
\usetikzlibrary{chains, fit}

\begin{document}
\begin{tikzpicture}[node distance=5mm]
 \begin{scope}[start chain, every node/.style={draw, on chain, join}, every join/.style=->]
  \node (a)  {a};
  \node (b)  {b};
  \node  (c)  {c};
  \node  (d)  {d};
  \begin{scope} [start branch=numbers, every join/.style=<-]
    \node (1) [on chain=going above] {1};
    \node (2) [on chain=going above] {2};
  \end{scope}
  \node  (e)  {e};
 \end{scope}
  \node (fit2) [draw, inner sep=2pt, fit=(a) (b) ] {};
  \node (fit1) [draw, inner sep=2pt, fit=(d) (1) (e)] {};
\end{tikzpicture}
\end{document}

Code B

\documentclass[border=5pt, tikz]{standalone}
\usetikzlibrary{chains, fit}

\begin{document}
\begin{tikzpicture}[node distance=5mm, start chain, every node/.style={draw, on chain, join}, every join/.style=->]
  \node (a)  {a};
  \node (b)  {b};
  \node  (c)  {c};
  \node  (d)  {d};
  \begin{scope} [start branch=numbers, every join/.style=<-]
    \node (1) [on chain=going above] {1};
    \node (2) [on chain=going above] {2};
  \end{scope}
  \node  (e)  {e};
  \tikzset{every node/.style={draw, inner sep=+2pt}}
  \node (fit2) [fit=(a) (b) ] {};
  \node (fit1) [fit=(d) (1) (e)] {};
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .