Take the 2-minute tour ×
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.

please, consider the mwe below.

Question: why the fill of second node become transparent?

Subquestion: why lines in pattern, then are thicker than 1pt are not smooth?

    \documentclass[tikz,border=5mm]{standalone}
\makeatletter
    \usetikzlibrary{chains,patterns,shadows}
\tikzset{% customization of pattern
         % based on <m.wibrow@gm...> - 2013-03-24 07:20: 
        hatch distance/.store in=\hatchdistance,
        hatch distance=5pt,
        hatch thickness/.store in=\hatchthickness,
        hatch thickness=5pt
        }
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{north east hatch}% name
    {\pgfqpoint{0pt}{0pt}}% below left
    {\pgfqpoint{\hatchdistance}{\hatchdistance}}% above right
    {\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
    {
        \pgfsetcolor{\tikz@pattern@color}
        \pgfsetlinewidth{\hatchthickness}
        \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
        \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
        \pgfusepath{stroke}
    }
\makeatother
\begin{document}
    \begin{tikzpicture}[
    start chain=going below,
    node distance=2mm,
   Node/.style = {minimum width=#1,
                  shape=rectangle, draw, fill=white,
                  on chain},
Pattern/.style = {pattern=north east hatch,
                  pattern color=teal!30,
                  hatch distance=7pt, hatch thickness=2pt},
                        ]\small\sffamily
%----------------
\node[Node=24mm,Pattern]    (a) {without shadow};
\node[Node=24mm,Pattern,
      drop shadow]          (b) {with shadow};
\node[Node=24mm,
      drop shadow]          (c) {without pattern};
%---
    \end{tikzpicture}
\end{document}

enter image description here

share|improve this question
    
    
By default all nodes are transparent. If you want to make them opaque, fill them with a white color. –  Ignasi Jan 19 at 11:40
    
my Node has fill=white,i.e. it is not transparent. that should be seen from third rectangle. my observation is, that use of pattern make a shape transparent again. i need to see how the suggestion of Claudio work. –  Zarko Jan 19 at 13:00
    
@Claudio Fiandrino, unfortunately, the solution given link doesn't help. It doesn't change look out of my second rectangle. It stay the same. –  Zarko Jan 19 at 15:46

1 Answer 1

up vote 9 down vote accepted

New answer

I've found the correct answer which, of course, is perfectly explained in pgfmanual. I must give some credit to Jake's answer in How to combine fill and pattern in a pgfplot bar plot? because it switched on the light.

It's called preaction or postaction and you can read about them in section (v3.0.0) 15.10 Doing Multiple Actions on a Path.

/tikz/preaction=<options> 

[...] When this option is used on a \path, the effect is the following: When the path has been completely constructed and is about to be used, a scope is created. Inside this scope, the path is used but not with the original path options, but with <options> instead. Then, the path is used in the usual manner. In other words, the path is used twice: Once with <options> in force and then again with the normal path options in force.

/tikz/postaction=<options> 

The postactions work in the same way as the preactions, only they are applied after the main action has been taken. Like preactions, multiple postaction options may be given to a \path command, in which case the path is reused several times, each time with a different set of options in force. If both pre- and postactions are specified, then the preactions are taken first, then the main action, and then the post actions.

and from section 66.1 Overview (Shadows Library) (bold is mine)

A shadow is usually a black or gray area that is drawn behind a path or a node, thereby adding visual depth to a picture. The shadows library defines options that make it easy to add shadows to paths. Internally, these options are based on using the preaction option to use a path twice: Once for drawing the shadow (slightly shifted) and once for actually using the path.

Therefore, if you want to fill a pattern you can use preaction={fill=white}. The node will be drawn twice, the first one it will be filled while the transparent pattern will be added on the second use of it.

\node[Node=24mm, Pattern, preaction={fill=white}] (a) {without shadow};

When nodes have shadow options, you must be careful with options order. Shadows use opacity=.5 and if your filling preaction is used before the shadow,

\node[Node=24mm, preaction={fill=yellow}, drop shadow, Pattern] (b) {with shadow};

opacity is also applied to filling color. Then you must declare shadow option always before filling preaction.

\node[Node=24mm, drop shadow, preaction={fill=yellow}, Pattern] (b) {with shadow};

Instead of preactions you can also use postactions, but in this case you will need to draw again the node, because the pattern is drawn over it.

Next code shows examples with preaction and potsaction options.

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{chains,patterns,shadows,fit,backgrounds}

\makeatletter
\tikzset{% customization of pattern
         % based on <m.wibrow@gm...> - 2013-03-24 07:20: 
        hatch distance/.store in=\hatchdistance,
        hatch distance=5pt,
        hatch thickness/.store in=\hatchthickness,
        hatch thickness=5pt
        }
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{north east hatch}% name
    {\pgfqpoint{-1pt}{-1pt}}% below left
    {\pgfqpoint{\hatchdistance}{\hatchdistance}}% above right
    {\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
    {
        \pgfsetcolor{\tikz@pattern@color}
        \pgfsetlinewidth{\hatchthickness}
        \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
        \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
        \pgfusepath{stroke}
    }
\makeatother

\begin{document}
 \begin{tikzpicture}[
    start chain=going below,
    node distance=2mm,
    Node/.style = {minimum width=#1,
                   shape=rectangle, 
                   draw, fill=white,
                   on chain},
    Pattern/.style = {pattern=north east hatch,
                    pattern color=teal!30,
                    hatch distance=7pt, 
                    hatch thickness=2pt},
    font=\small\sffamily]
%----------------
    \node[Node=24mm, Pattern, 
            preaction={fill=white}] (a) {without shadow};
    \begin{scope}[on background layer]
        \node[fit=(a),fill=red] {};
    \end{scope}

    \node[Node=24mm, drop shadow,
            preaction={fill=yellow}, Pattern] (b) {with shadow};

    \node[Node=24mm, preaction={fill=yellow},
            drop shadow, Pattern] (b) {with shadow};

    \node[Node=24mm, postaction={Pattern},
            drop shadow] (b) {with shadow};

    \node[Node=24mm, postaction={draw=red, Pattern},
            drop shadow] (b) {with shadow};

    \node[Node=24mm, drop shadow] (c) {without pattern};
%---
 \end{tikzpicture}   
\end{document}

enter image description here

First version

Next code is just an idea. Looks like is not possible to fill a hatch pattern with any color but you can try to draw double lines. It's not perfect and I don't know how to do it better because I don't understand enough how it works. This solution was obtained after playing with \hatchdistance and \hatchthickness.

  \documentclass[tikz,border=5mm]{standalone}
\makeatletter
    \usetikzlibrary{chains,patterns,shadows,fit,backgrounds}
\tikzset{% customization of pattern
         % based on <m.wibrow@gm...> - 2013-03-24 07:20: 
        hatch distance/.store in=\hatchdistance,
        hatch distance=5pt,
        hatch thickness/.store in=\hatchthickness,
        hatch thickness=5pt
        }
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{north east hatch}% name
    {\pgfqpoint{-1pt}{-1pt}}% below left
    {\pgfqpoint{\hatchdistance}{\hatchdistance}}% above right
    {\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
    {
        \pgfsetcolor{\tikz@pattern@color}
        \pgfsetlinewidth{\hatchthickness}
        \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
        \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
        \pgfsetinnerlinewidth{1pt}
        \pgfsetinnerstrokecolor{white}
        \pgfusepath{stroke}
    }
\makeatother
\begin{document}
    \begin{tikzpicture}[
    start chain=going below,
    node distance=2mm,
   Node/.style = {minimum width=#1,
                  shape=rectangle, draw, fill=white,
                  on chain},
Pattern/.style = {pattern=north east hatch,
                  pattern color=teal!30,
                  hatch distance=4pt, hatch thickness=2.5pt},
                        ]\small\sffamily
%----------------
\node[Node=24mm,Pattern]    (a) {without shadow};
\begin{scope}[on background layer]
\node[fit=(a),fill=red] {};
\end{scope}

\node[Node=24mm,Pattern,
      drop shadow]          (b) {with shadow};
\node[Node=24mm,
      drop shadow]          (c) {without pattern};
%---
    \end{tikzpicture}
\end{document}

The result is big enough to see some thin blue lines behind white ones. I don't know where they come from.

enter image description here

share|improve this answer
    
thank you very much. it works and result is good enough for my temporary needs. me either not understand haw this pattern work. I also observe this tiny blue lines in my other examples. At first I expect that to pattern can be defined an "background" color or it can be put somehow in foreground of node with fill. Maybe this can be feature request. –  Zarko Jan 21 at 14:52
    
@Zarko: Please look at updated answer. It shows a better solution. –  Ignasi Jan 23 at 9:20
1  
Thank you very much. This is great solution! I my confess, when I need to "browse" in tikz manual for something, i feel a little bit lost in forest of information in it, so many time i do not see particular tree ... and thank you for explanation. It is very helpful form my future plans for more drawing more complex. –  Zarko Jan 23 at 12:46

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.