7

I have two nodes (drawn as rectangles) in my picture, one filled in green, the other red. I wish to produce a third 'rectangle' which shows the effect of splicing these two rectangles and stitching them back together.

Or, in other words, I wish to produce a rectangle twice the width of my starting rectangles that is subdivided up into smaller segments, each segment filled alternating between red and green.

Given my picture:

\begin{tikzpicture}[node distance=5mm]
  \node [fill=red!30,minimum width=40mm] (b1) {Bank 1 \unit[1024]{MiB}};
  \node [fill=green!30,minimum width=40mm,right=of b1] (b2) {Bank 2 \unit[1024]{MiB}};
\end{tikzpicture}

I have been able to 'create' the desired effect using two \foreach loops:

\foreach \n in {0,2,...,10}
  \draw [fill=red!30] (\n*6.66mm,1) +(-3.33mm,0) rectangle
  ++(3.33mm,5mm);
\foreach \n in {1,3,...,11}
  \draw [fill=green!30] (\n*6.66mm,1) +(-3.33mm,0) rectangle
  ++(3.33mm,5mm);

However, this requires several manual factors (40 / 6 = 6.66, 6.66 / 2 = 3.33) and I have been unable to position it under my existing two nodes due to the use of absolute coordinates.

Are there better ways of doing this which would give me more/easier control of the positioning of the 'group'? I looked into splitting a rectangle but this is seemingly limited to four horizontal splits.

2 Answers 2

4

You could use a chain and position the first node in the chain using the anchors of the unsplit nodes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{chains}



\begin{document}

\newlength\nodewidth
\setlength\nodewidth{40mm}

\tikzset{%
    fill1/.style={fill=red!30,outer sep=0pt},
    fill2/.style={fill=green!30,outer sep=0pt},
    split/.style={%
        minimum width=0.5/3*\nodewidth,
        minimum height=5mm,
        %inner sep=0pt,
        outer sep=0pt,
        on chain},
    split1/.style={split,fill1},
    split2/.style={split,fill2},
}

\begin{tikzpicture}[%
    start chain,
    node distance=0pt]

    \node [fill1,minimum width=\nodewidth] (b1) {Bank 1 1024 MiB};
    \node [fill2,minimum width=\nodewidth,right=of b1] (b2) {Bank 2 1024 MiB};

    \node [split1,anchor=north west] at (b1.south west) {};
    \node [split2] {};
    \foreach \n in {0,1,...,4} {%
        \node [split1] {};
        \node [split2] {};
    };

\end{tikzpicture}

\end{document}
2

You can use the TikZ \scope instruction to make your picture local and then use shifts and scales as options to the \scope to place the whole group where you want it.

1
  • So given a \foreach in a scope (similar to the \foreach Martin suggested) how can that scope then be treated as if it were one object/a node? Aug 13, 2010 at 10:12

You must log in to answer this question.

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