TransWikia.com

Tikz nodes with pattern ignoring z-order/fill

TeX - LaTeX Asked by JE_Muc on July 14, 2021

I’m trying to draw overlapping circles with patterns aka hatches. Overlapping circles are meant to hide underlying circles. This is the way it usually works without patterns, but as soon as I add patterns, the value for fill seems to be completely ignored.

Is there any way to use patterns with fill? Thanks in advance!

documentclass{article}
usepackage{tikz}
usetikzlibrary{%
    shapes.misc,    % additional shapes, such as rounded rectangles
    positioning,    % advanced positioning
    patterns,       % hatches etc
}

begin{document}
begin{tikzpicture}[%
    roundnode/.style={circle, draw=black, fill=yellow!20, thick, inner sep=0em, opacity=1, fill opacity=1, draw opacity=1, font=small},
    ]
    
    node[roundnode, minimum size=15mm] (nodeA) at (1, 0) {A};
    node[roundnode, minimum size=15mm, fill=red!20, pattern=north west lines, pattern color=gray] (nodeB) at (2, 0) {B};
    
    node[roundnode, minimum size=15mm] (nodeC) at (2, 1) {C};
    node[roundnode, minimum size=15mm, pattern=north east lines, pattern color=gray] (nodeD) at (2, -1) {D};
    
end{tikzpicture}
end{document}

One Answer

You can use postaction to achieve the desired effect. Please note also that there is patterns.meta which is an arguably more convenient way to add pattern (but you will still need a postaction or declare a filled pattern).

documentclass{article}
usepackage{tikz}
usetikzlibrary{%
    shapes.misc,    % additional shapes, such as rounded rectangles
    positioning,    % advanced positioning
    patterns,       % hatches etc
}

begin{document}
begin{tikzpicture}[%
    roundnode/.style={circle, draw=black, fill=yellow!20, thick, inner sep=0em, opacity=1, fill opacity=1, draw opacity=1, font=small},
    ]
    
    node[roundnode, minimum size=15mm] (nodeA) at (1, 0) {A};
    node[roundnode, minimum size=15mm, fill=red!20, 
        postaction={pattern=north west lines, pattern color=gray}] (nodeB) at (2, 0) {B};
    
    node[roundnode, minimum size=15mm] (nodeC) at (2, 1) {C};
    node[roundnode, minimum size=15mm, 
        postaction={pattern=north east lines, pattern color=gray}] (nodeD) at (2, -1) {D};
    
end{tikzpicture}
end{document}

enter image description here

Correct answer by user230294 on July 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP