6

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?

2 Answers 2

10

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

1

You can put a shading box under your rectangle like the following.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}

\begin{tikzpicture}
    \shade [left color=red, right color=blue, ] (0,0) rectangle (10,5);
    \draw [pattern=crosshatch, pattern color=lightgray, ] (0,0) rectangle (10,5);
\end{tikzpicture}

\end{document}

enter image description here

1
  • I wanted the hatch color to have a gradient while the rectangle fill color still being white. In this answer, the pattern(hatch) color is still a monotonous gray. Sep 25, 2017 at 8:24

You must log in to answer this question.

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