Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 11 down vote accepted

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

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.