[Tex/LaTex] how to draw two connected rectangles

tikz-pgf

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}

enter image description here

Best Answer

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}

enter image description here

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}

enter image description here

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}

enter image description here