I'm having problem with a TikZ picture – I've got rectangles that I've split, but for some reason the split rectangles are a bit larger than the other rectangles, causing the the alignment to get messed up:
Any ideas on how to solve this? I've included the code below.
\documentclass[a4paper, 11pt]{article}
\usepackage[rgb,hyperref]{xcolor}
\usepackage{tikz}
% Define colors
\definecolor{shade1}{rgb}{0.9, 0.9, 0.9}
\definecolor{shade2}{rgb}{0.75, 0.75, 0.75}
\definecolor{shade3}{rgb}{0.5, 0.5, 0.5}
\definecolor{shade4}{rgb}{0.35, 0.35, 0.35}
% Load TikZ libraries
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{fit}
\usetikzlibrary{backgrounds}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
% Text settings
\newcommand{\figureTextSize}{\tiny}
% Figure element lengths
\newlength{\gpgpuElemSep}
\setlength{\gpgpuElemSep}{1mm}
\newlength{\gpgpuElemSize}
\setlength{\gpgpuElemSize}{8mm}
% TikZ styles
\newcommand{\arrowStyle}{stealth}
\newcommand{\bendAngle}{45}
\newcommand{\lineThickness}{semithick}
\tikzstyle{box} = [%
draw,
rectangle,
\lineThickness,
]
\begin{document}
\begingroup
\figureTextSize
\begin{tikzpicture}[%
every node/.style={%
node distance=0.375\gpgpuElemSep,
},
component/.style={%
box,
minimum size=0.42\gpgpuElemSize,
inner sep=0pt,
},
alu/.style={%
component,
fill=shade1,
},
controlcache/.style={%
component,
rectangle split,
rectangle split parts=2,
rectangle split part fill={shade2, shade3},
},
memory/.style={%
box,
fill=shade3,
minimum height=0.5\gpgpuElemSize,
inner sep=0pt,
},
]
% Rows of ALUs, control logics and caches
\foreach \i in {1, ..., 7} {%
\ifnum \i=1
\node [controlcache] (cc\i) {};
\else
\pgfmathtruncatemacro\prevCC{\i-1}
\node [controlcache, below=of cc\prevCC] (cc\i) {};
\fi
\foreach \j in {1, ..., 7} {%
\ifnum \j=1
\node [alu, right=of cc\i] (alu\j) {};
\else
\pgfmathtruncatemacro\prevAlu{\j-1}
\node [alu, right=of alu\prevAlu] (alu\j) {};
\fi
}
}
% Memory
\path let \p1 = (cc7.south west),
\p2 = (alu7.north east)
in
node [%
memory,
minimum width=\x2-\x1-\pgflinewidth,
below right,
] at ([%
yshift={-2\gpgpuElemSep},
] cc7.south west) (memory) {Memory};
\end{tikzpicture}
\endgroup
\end{document}
Best Answer
You can reset the minimum size options of the
rectangle split
parts as explained in How to change the height of just one nodepart in TikZ? usingand then, to get the parts to have the correct height, set
(i.e. half the desired height, minus the thickness of the line, to account for the added size).
Addendum (by Andrew Stacey): The fact that the line width correct is
\pgflinewidth
surprised me; I expected it to be either.5*\pgflinewidth
or not needed at all. So I did a little investigation and discovered a crucial difference between therectangle split empty part height
and theminimum height
keys. Theminimum height
key, when given to a rectangular node and assuming that there's no funny business with theinner sep
, is the distance between the top and bottom lines of the rectangle. This is independent of the line thickness. Therectangle split empty part height
is the height of the internal box which takes into account the line width. So if we set each to be 1cm, then for a rectangular node, PGF will draw a rectangle with height 1cm, then it will look at the options as to whether to draw it or fill it, and (in particular) what line width to use. For arectangle split
, on the other hand, PGF will draw a rectangle such that when it is drawn, the inside of the drawn rectangle has height 1cm.Here's a picture to show that. The background lines are 1cm apart, the red and cyan lines are guide lines. The first two yellow boxes are drawn using the
rectangle split
shape (the second only has one part). The second two are drawn as rectangular nodes. In each case, the corresponding key has been set to either1cm
or2cm
as appropriate. The second and third boxes are placed so that theirsouth
anchor is at the relevant coordinate, which has been pushed down by half the line width. All of the arrows are 1cm in length.Here's the code (not particularly elegant!):