[Tex/LaTex] using pattern inside tikz shapes with dropped shadows

shadowstikz-pgf

Please, consider the mwe below.

Question: why does the fill of the second node become transparent?

Subquestion: why are lines in the pattern which are thicker than 1pt 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

Best Answer

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

Related Question