# Nested rectangles with different widths

The following code

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\path [style={draw}] (0,0) rectangle (4,1);
\path [style={draw}] (0,1) rectangle (4,2);
\path [style={draw,thick,fill=orange}] (0,1) rectangle (1,2);
\end{tikzpicture}
\end{document}


Produces an image like this:

On closer observation one can see that the rectangles don't intersect properly on the border (drawn a bit exaggerated for better visibility):

Is there a way to make the rectangles fit perfectly? I know that adding/subtracting 0.01 to some coordinates works but that isn't really a clean fix.

-
Don't use thick? Or use thick for all of the rectangles? If you need variable thicknesses, you could specify them so that you can compensate appropriately. –  cfr Aug 22 '14 at 21:17

A PSTricks solution:

\documentclass{article}

\usepackage{pstricks}

\begin{document}

\psset{dimen = m} % no overlapping of the rectangles
\begin{pspicture}(8,4)
\psframe(0,0)(8,2)
\psframe(0,2)(8,4)
\psframe[
fillstyle = solid,
fillcolor = orange!70
](0,2)(2,4)
\end{pspicture}

\end{document}


or

\documentclass{article}

\usepackage{pstricks}

\begin{document}

\psset{dimen = m} % no overlapping of the rectangles
\begin{pspicture}(8,4)
\psframe(0,0)(8,2)
\psframe(0,2)(8,4)
\psframe[
dimen = o, % aligns the outer part of the top right (the 'thick') rectangle with the two bigger ones
linewidth = 2pt,
fillstyle = solid,
fillcolor = orange!70
](0,2)(2,4)
\end{pspicture}

\end{document}


-
+1 : Only for those who don't know. dimen=m, dimen=i, dimen=o are for middle, inner, and outer, respectively. Only the first character is taken into account so dimen=m has the same effect as dimen=monkey, for example. –  kiss my armpit Aug 23 '14 at 13:53
@cyanide-basedfood I didn't knew that. Thanks for that note. –  Svend Tveskæg Aug 23 '14 at 13:55
(+1) That's neat and I appreciate @cyanide-basedfood's explanation. (I don't think I've ever understood a pstricks answer before.) –  cfr Aug 23 '14 at 15:35
Thanks, this is exactly what I was looking for. –  user1912 Aug 23 '14 at 18:55

The non-alignment is caused by the use of thick for one rectangle but not the others. If you need variable line widths, you can compensate for the difference either by checking the widths TikZ is using for regular and thick lines or by specifying the widths you want. For example:

\documentclass[a4paper,10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\path [draw, line width=.4pt] (0,0) rectangle (4,1);
\path [draw, line width=.4pt] (0,1) rectangle (4,2);
\path [draw, line width=1pt, fill=orange] ($(0,1) + (.3pt,.3pt)$) rectangle ($(1,2) - (.3pt,.3pt)$);
\end{tikzpicture}
\end{document}


The value of .3pt is used because it is half of the difference between the line thicknesses. calc is used so that this can be entered directly rather than figuring out how to adjust the coordinate itself.

I'm not sure what you mean about this type of solution not being a 'clean' fix. You are otherwise telling TikZ to draw something which will not be aligned due to the way you've defined things. If you want things aligned, you obviously need to specify them differently.

-
Best to draw one rectangle and two lines. Is a better and more minimalistic solution. –  Yiannis Lazarides Aug 23 '14 at 13:57
@YiannisLazarides But that won't achieve the desired difference in line widths. I'm assuming here that that is desired. If not, there's no problem: just deleting thick or adding it to all nodes will solve the problem. Only if the difference in widths is required is there an issue. But, in that case, you cannot draw the desired result with one rectangle and two lines. You could do one rectangle and five lines or two rectangles and one line. But it isn't clear to me that that would be a better solution since you'd still need to compensate for the width difference. –  cfr Aug 23 '14 at 15:32