# how to draw two connected rectangles

How can I draw two rectangles (or two shapes in general) so that they touch each other. Below is the minimum working example.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle,draw,minimum width=2in, minimum height=1.00in, fill=cyan] (a) {}
node[rectangle,draw,fill=magenta] (b) [left=of a] {};
\end{tikzpicture}
\end{document}


-

You can use left= 0pt of a:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle,draw,minimum width=2in, minimum height=1.00in, fill=cyan] (a) {}
node[rectangle,draw,fill=magenta] (b) [left= 0pt of a] {};
\end{tikzpicture}
\end{document}


Or left= -\pgflinewidth of a if you want the edges to overlap:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle,draw,minimum width=2in, minimum height=1.00in, fill=cyan] (a) {}
node[rectangle,draw,fill=magenta] (b) [left= -\pgflinewidth of a] {};
\end{tikzpicture}
\end{document}


As Qrrbrbirlbel mentioned in a comment, another way to have the edges to overlap is to set outer sep (or outer xsep, outer ysep) to zero which puts the anchors in the middle of the edge's line.

If this should apply to all nodes, you could better use the node distance key for tikzpicture (or a scope); something like:

\begin{tikzpicture}[node distance=0pt]
...
\end{tikzpicture}


Another option would be to use the anchors:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle,draw,minimum width=2in, minimum height=1.00in, fill=cyan] (a) {}
node[rectangle,draw,fill=magenta,anchor=east] (b) at (a.west) {};
\node[rectangle,draw,fill=green,anchor=south] at (a.north) {};
\end{tikzpicture}
\end{document}


-
Thanks for letting me know of the various options. Will try those. @Qrrbrbirlbel Thanks again. –  Sandeep Apr 17 '13 at 4:55
Some problems can appear with different line width for the different shapes, especially with anchors or with left= -\pgflinewidth –  Alain Matthes Apr 18 '13 at 8:39