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.

I am trying to superpose 2 rectangles; one blue, one red, and have the intersection area with a mix of blue and red. I have problems with the borders though.

  1. I am not getting the thickness of the borders correctly where rectangles intersect

  2. when using rounded corners, I don't see how to nicely get 2 angles rounded (south west and north east) and 2 not.

Any idea?

intersecting rectangles

Here is my MWE:

\documentclass{article}
\usepackage{tikz}
\tikzset{
box1/.style={draw=black, thick, rectangle,rounded corners, minimum height=4cm, minimum width=4cm},
box2/.style={draw=black, thick, rectangle, minimum height=4cm, minimum width=4cm},
}

\begin{document}
\begin{tikzpicture}

\node[box1, fill=red!10] (c2) at (0,0) {};
\node[box1, fill=blue!10] (c1) at (2,2) {};
\begin{scope}
\clip (0,0) rectangle (4,4);
\clip (-2,-2) rectangle (2,2);
\fill[color=blue!50!red!10, rounded corners, draw=black, thick] (2,2) rectangle (0,0);
\end{scope}


\node[box2, fill=red!10] (c2) at (8,0) {};
\node[box2, fill=blue!10] (c1) at (10,2) {};
\begin{scope}
\clip (8,0) rectangle (12,4);
\clip (6,-2) rectangle (10,2);
\fill[color=blue!50!red!10, draw=black, thick] (10,2) rectangle (8,0);
\end{scope}

\end{tikzpicture}
\end{document}
share|improve this question
    
For your second problem (round only some corners) the shape chamfered rectangle with the option chamfered rectangle corners could be an alternative … See also tex.stackexchange.com/q/28115/4918 –  Tobi Jun 22 '12 at 1:03
2  
You've asked six questions and still marked none of the answers as the accepted one. If one of the answers you received has solved your problem, you should mark it as accepted by clicking the green check mark next to the answer text; this rewards the answerer and will be useful for future readers. –  egreg Jun 22 '12 at 11:23
    
ah sorry I didn't know. I have been through them all and have marked all of them. Thanks for pointing that out. –  jejuba Jun 22 '12 at 16:34
add comment

2 Answers

up vote 11 down vote accepted

The problem is that you \clip a part of the borders. Take a look at the following image. Figure a shows a black border, where the path of the border is shown in red. If you clip along the path you’ll get figure b showing that that the outer half of your black border is missing/clipped.

clipping the line width

Possible solution

You could use transparency to overlay the two colors instead of clipping. Just set the colors with 100 percent and then set the opacity to the value you wan’t the colors to have. 10 percent means a value of 0.1. In the intersecting part the colors are mixed automatically and no clipping is needed. I used fill opacity instead of opacity to affect only the filling and not the border (draw opacity).

\documentclass{article}
\usepackage{tikz}
\tikzset{
    box1/.style={%
        draw=black, thick,
        rectangle,
        rounded corners,
        minimum height=4cm,
        minimum width=4cm
    },
}

\begin{document}
\begin{tikzpicture}
\node[box1, fill=red, fill opacity=0.1] (c2) at (0,0) {};
\node[box1, fill=blue, fill opacity=0.1] (c1) at (2,2) {};
\end{tikzpicture}
\end{document}

overlaying colors

share|improve this answer
    
Thanks, it works very well indeed. –  jejuba Jun 22 '12 at 16:41
add comment

Tobi's answer shows the good way but there is a another way without rectangle and without clip.

\documentclass{article}
\usepackage{tikz}
\tikzset{
box1/.style={draw=black, thick, rectangle,rounded corners, minimum height=4cm, minimum width=4cm},
box2/.style={draw=black, thick, rectangle, minimum height=4cm, minimum width=4cm},
}

\begin{document}
\begin{tikzpicture}

\node[box1, fill=red!20]  (c2) at (0,0) {};
\node[box1, fill=blue!20] (c1) at (2,2) {};

\fill[green,thick]  (0+0.5*\pgflinewidth,2cm-0.5*\pgflinewidth)
                   {[rounded corners=3pt+0.5\pgflinewidth] -- ++(2cm-\pgflinewidth,0)} 
                    -- ++(0,-2cm+\pgflinewidth) 
                   {[rounded corners=3pt+0.5\pgflinewidth] -- ++(-2cm+\pgflinewidth,0)} 
                    -- cycle;  
\draw[thick]  (0+0.5*\pgflinewidth,2cm)
                   {[rounded corners] -- ++(2cm-0.5\pgflinewidth,0)} 
                    -- ++(0,-2cm) ;  
\end{tikzpicture}  
\end{document} 

enter image description here

share|improve this answer
    
But if I use \pgflinewidth as part of a coordinate I must use the other part with a dimension, since 2+\pgflinewidth = 2pt+\pgflinwidth. Which is problematic if I set x=0.456cm for example. It’s the same problem with 2+1pt. Is it possible to let TikZ read this as 2+(1pt) and not (2+1)pt which it does? (Is this worth to be a new question?) –  Tobi Jun 22 '12 at 12:41
    
@Tobi In this case, you need to use the calc library for example ($(0,2)+(\pgflinewidth,\pgflinewidth)$) –  Alain Matthes Jun 22 '12 at 13:37
    
I have to say, Tobi's answer was rather sleek and I'll keep to it. But good to learn another way of doing. Thanks, –  jejuba Jun 22 '12 at 16:43
    
Tobi's answer is the better way. I added a new answer only to give another way. –  Alain Matthes Jun 22 '12 at 16:50
add comment

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.