7

This is my code:

\documentclass[tikz,12pt]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[line width=1.5pt]
\coordinate (A) at (2,2);
\coordinate (B) at (2,1.2);
\coordinate (C) at (3,2);
\coordinate (D) at (3,1.2);
\draw[fill=white] (A) circle (0.5);
\draw[fill=white,pattern=north east lines, pattern color=black] (B) circle (0.5);
\draw[fill=white] (C) circle (0.5);
\draw[fill=white] (D) circle (0.5);
\end{tikzpicture}
\end{document}

enter image description here

When I use pattern=north east lines I can see first circle through second with pattern.

If I do not use pattern=north east lines I get what I want to see: second circle overlay first one.

But I need Second circle to be with pattern.

How this problem could be solved without using \pgfdeclarelayer{background} \pgfsetlayers{background,main}?

1

2 Answers 2

12

You can use postaction and draw the patterns after filling.

\documentclass[tikz,12pt]{standalone}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}[line width=1.5pt]
\coordinate (A) at (2,2);
\coordinate (B) at (2,1.2);
\coordinate (C) at (3,2);
\coordinate (D) at (3,1.2);
\draw[fill=white] (A) circle (0.5);
\draw[fill=white,
         postaction = {pattern=north east lines,pattern color=black}] (B) circle (0.5);
\draw[fill=white] (C) circle (0.5);
\draw[fill=white] (D) circle (0.5);
\end{tikzpicture}
\end{document}

enter image description here

4

In Graphic Parameters: Fill Pattern section from TiKZ documentation you can read

Instead of filling a path with a single solid color, it is also possible to fill it with a tiling pattern.

Although it's not clearly stated, it looks like any pattern option replaces any previous fill option. Of course, any fill option will replace any pattern one. Just put fill=white after pattern in your code and see what happens.

Therefore, if you want a filled (non transparent) pattern, you need to draw it after (postaction) filling while node is drawn as it's shown in Harish's answer.

Another option is filling before (preaction) drawing the node and its filling pattern.

\documentclass[tikz,12pt]{standalone}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}[line width=1.5pt]
\coordinate (A) at (2,2);
\coordinate (B) at (2,1.2);
\coordinate (C) at (3,2);
\coordinate (D) at (3,1.2);
\draw[fill=white] (2,2) circle (0.5);
\draw[preaction={fill=white},
         pattern=north east lines,pattern color=black] (B) circle (0.5);
\draw[fill=white] (C) circle (0.5);
\draw[fill=white] (D) circle (0.5);
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .