1

MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}

\tikzstyle{block} = [rectangle, draw,
    text width=5em, text centered, rounded corners, minimum height=2em]
\tikzstyle{entity} = [rectangle, draw, text centered]

\centering
\begin{tikzpicture}
  \begin{scope}
    \matrix [row sep= 2em, column sep= 2em] {
      & \node [block] (a) {A}; & \\
      \node [block] (b) {B}; & & \node [block] (c) {C};\\
      & \node [block] (d) {D}; & \\
      \node [block] (e) {E}; & & \\
    };
  \end{scope}

  \node [entity, fit= (a)] {};
  \node [entity, fit= (b) (c)] {};
  \node [entity, fit= (d)] {};
  \node [entity, fit= (e)] {};
\end{tikzpicture}

\end{document}

Suppose that each of the nodes is a different step. Each of them are part of a bigger stage, which is signaled by surrounding them with the entitys. How can I make sure that each entity is:

  • the exact same width
  • centered with each other
  • have no separation between them?

Since I will later colorize them, the entitys will be [on background layer].

amyspark
  • 628
  • text width, text height, anchor=center. The last is probably best done in the matrix with node distance or similar. – cfr May 13 '16 at 02:17

1 Answers1

2

A possible solution consists in using some anchors on matrix node to define convenient rectangles on a background layer.

First of all I've changed deprecated tikzstyle by tikzset. You can read about it in Should \tikzset or \tikzstyle be used to define TikZ styles?

The matrix has been changed to a matrix of nodes (you need matrix library) and adjusted matrix inner sep=1em. This way all layers will present a similar space around inner nodes.

\matrix (M) [matrix of nodes, inner sep=1em, row sep= 2em, column sep= 2em, nodes={block}]

I've preserved original nodes names with syntax |(A)| A, if you don't need it just write A as node contents.

|(B)| B & &|(C)| C\\

As inner nodes and matrix anchor are not directly related. Two auxiliar anchors are defined:

\path (M.north) -- coordinate[pos=.25] (aux1) coordinate[pos=.75] (aux2) (M.south);

And finally these anchors are used to draw and fill four bands on a background layer.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, backgrounds}

\begin{document}

\centering
\begin{tikzpicture}[%
    block/.style={draw, text width=5em, text centered, rounded corners, minimum height=2em},
    entity/.style={draw, text centerd}
]

  \begin{scope}
    \matrix (A) [matrix of nodes, inner sep=1em, row sep= 2em, column sep= 2em, nodes={block}] {
      & A & \\
      B & & C\\
      & D & \\
      E & & \\
    };
  \end{scope}

    \path (A.north) -- coordinate[pos=.25] (aux1) coordinate[pos=.75] (aux2) (A.south);

\begin{scope}[on background layer]
  \draw[fill=green!30] (A.north west) rectangle (aux1-|A.east);
  \draw[fill=red!30] (A.west|-aux1) rectangle (A.east);
  \draw[fill=blue!30] (A.west) rectangle (aux2-|A.east);
  \draw[fill=orange!30] (A.west|-aux2) rectangle (A.south east);
  \end{scope}
\end{tikzpicture}

\end{document}

enter image description here

Ignasi
  • 136,588