# Overwriting fill patterns in TikZ

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}
\begin{document}
\begin{tikzpicture}
\draw[pattern=dots, pattern color=green] (0, 0) rectangle (4, 4);
\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.

-

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}


-

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}
\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}


-

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}


-

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}
\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:

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

-

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}


-