10

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

0

1 Answer 1

15

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

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

You must log in to answer this question.

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