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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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

share|improve this answer

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

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

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.