[Tex/LaTex] Hatched rectangle with color gradient

gradientpatterntikz-pgf

Is it possible in tikz to draw a hatched rectangle, in which the hatching color(NOT fill color) has a gradient (changes from red(say) from one side to the blue to the other side)?

\draw[pattern=crosshatch, pattern color=lightgray] (0,0) rectangle (10,5);

How do I add a gradient to hatch color?

Best Answer

Just an idea, declare an opposite pattern to the desired result and use it within a postaction over a gradient colored area.

Following code declares a mycheckerboard pattern (inspired in original checkerboard pattern). It fills little squares (2mmx2mm) leaving 1mm space between them. Used in a postaction simulates 1mm width crossing lines.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{patterns}

% Checkerboards

\pgfdeclarepatternformonly
    {mycheckerboard}%name
    {\pgfpointorigin}% lower left point
    {\pgfqpoint{6mm}{6mm}}% upper right point
    {\pgfqpoint{6mm}{6mm}}% tile size
    {% shape description
        \pgfpathrectangle{\pgfqpoint{.5mm}{.5mm}}{\pgfqpoint{2mm}{2mm}}
        \pgfpathrectangle{\pgfqpoint{3.5mm}{.5mm}}{\pgfqpoint{2mm}{2mm}}
        \pgfpathrectangle{\pgfqpoint{.5mm}{3.5mm}}{\pgfqpoint{2mm}{2mm}}
        \pgfpathrectangle{\pgfqpoint{3.5mm}{3.5mm}}{\pgfqpoint{2mm}{2mm}}
        \pgfusepath{fill}
    }


\begin{document}

\begin{tikzpicture}
\fill[left color=red, right color=yellow, postaction={pattern=mycheckerboard, pattern color=white}] (0,0) rectangle (10,5);
\end{tikzpicture}

\begin{tikzpicture}
\fill[postaction={pattern=mycheckerboard, pattern color=white}, top color=blue, bottom color=green] (0,0) circle (3cm);
\end{tikzpicture}

\end{document}

enter image description here