7

A circle lies within a square. The square has a fill pattern, say , A, and the circle has fill pattern, say, B. I wish the two patterns not to overlap. In case this is unclear, here is an MWE:

\documentclass[10pt,class=memoir]{standalone}
\usepackage[cmyk,dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
  \usetikzlibrary{calc,shapes,decorations,decorations.text, mindmap,shadings,patterns,matrix,arrows,intersections,automata,backgrounds}
\begin{document}
\begin{tikzpicture}
\draw[pattern=dots, pattern color=green] (0, 0) rectangle (4, 4);
% \draw[fill=white] (2, 2) circle[radius=1]; 
\draw[pattern=bricks, pattern color=brown] (2, 2) circle[radius=1]; 
\end{tikzpicture}
\end{document}

If I uncomment the line with \fill[white] I get the result I want. But this seems an inelegant hack. Is there a better way of achieving the same result?

Thanks.

  • It was hard choice to select one answer! Thanks for all the replies below: each taught me something I did not know. – chandra Aug 11 '16 at 20:45
3

you can also clip the rectangle without the circle

\documentclass{article}
\usepackage[cmyk,dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}

\begin{tikzpicture} 
\begin{scope}
   \clip[insert path={(0, 0) rectangle (4, 4)}]  (2, 2) circle[radius=1];  
   \draw[pattern=dots, pattern color=red] (0, 0) rectangle (4, 4);
\end{scope}
\draw[pattern=bricks, pattern color=brown] (2, 2) circle[radius=1]; 
\end{tikzpicture}
\end{document}

enter image description here

7

If you don't want the white fill then you can punch the rectangle and repat the path (and use even odd rule if complex paths are used). If you have a really complicated path then use layers and send them to different layers.

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\fill[yellow!20] (-1,-1) rectangle (5,5);
\draw[pattern=dots, pattern color=green] (0, 0) rectangle (4, 4) (2, 2) circle[radius=1];
\path[pattern=bricks,pattern color=brown] (2,2) circle (1);
\end{tikzpicture}
\end{document}

enter image description here

4

It's possible to apply inner patern as a postaction over a previously white filled circle.

\documentclass[10pt,class=memoir]{standalone}
\usepackage[cmyk,dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
  \usetikzlibrary{calc,shapes,decorations,decorations.text, mindmap,shadings,patterns,matrix,arrows,intersections,automata,backgrounds}
\begin{document}
\begin{tikzpicture}
\draw[pattern=dots, pattern color=green] (0, 0) rectangle (4, 4);
\draw[fill=white, postaction={pattern color=brown, pattern=bricks}] (2, 2) circle[radius=1]; 
\end{tikzpicture}
\end{document}

enter image description here

4

If care is taken over the path directions (which is a nuisance in this case) then the different filling rules can be exploited (at least for PDF output) and it can be done in one path with various post actions:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\tikz\draw [pattern=bricks, pattern color=brown, nonzero rule,
  postaction={fill=white, even odd rule, 
    postaction={pattern=dots, pattern color=green}}] 
      (0, 0) rectangle (4, 4) (3, 2) arc (360:0:1);
\end{document}

enter image description here

3

You can specify a preaction that fills the circle. Pretty much the same hack as you suggested but one line of code less.

Use:

\documentclass[10pt,class=memoir]{standalone}
\usepackage[cmyk,dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
  \usetikzlibrary{calc,shapes,decorations,decorations.text, mindmap,shadings,patterns,matrix,arrows,intersections,automata,backgrounds}
\begin{document}
\begin{tikzpicture}
\draw[pattern=dots, pattern color=green] (0, 0) rectangle (4, 4);
\draw[preaction={fill=white},pattern=bricks, pattern color=brown] (2, 2) circle[radius=1]; 
\end{tikzpicture}
\end{document}

Tikz then fills the circle before the brick pattern is drawn. Without preaction, tikz obviously draws the patterns last.

Result:

enter image description here

Same as Ignasi's answer but the other way around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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