4

I would like to have a rectangle that has some hatch. Furthermore, the hatch should have some shading. But, I am only able to get a transition of the colors in the background. Does anyone know how to only have a color gradient in the hatch with only a white background?

Thanks a lot.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture} 
\fill[left color=blue!40, right color=white, postaction={pattern=north west lines, pattern color=blue!40}] (0,0) rectangle (3,3);
\end{tikzpicture}
\end{document}
  • 1
    Maybe help: tex.stackexchange.com/a/393131/31034 – ferahfeza Jun 9 at 16:23
  • Thank you. I have found this as well. But was not able to adapt it to a north west pattern and defining the dimensions in the tikz environment... – Sir Sy Jun 9 at 16:53
3

Only for fun!

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\def\a{3}
\def\lw{0.2}
\draw[blue, thick] (0,0) rectangle (\a,\a);
\foreach \x [count=\i] in{0,0.1,0.2,...,\a}{
\draw [blue,line width=\lw mm,opacity=\i/45](\x,0)--(0,\x) (\a,\a-\x)--(\a-\x,\a);}
\end{tikzpicture}

\end{document}

enter image description here

4

This is conceptually the same as Ignasi's nice answer in that the pattern is in the background color, so it determines what should be left unshaded. And in order to customize the pattern I use this answer. And of course you need to dial a shading angle to make the shading follow the north east lines.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{patterns}
% defining the new dimensions and parameters
\newlength{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
% declaring the keys in tikz
\tikzset{hatchspread/.code={\setlength{\hatchspread}{#1}},
         hatchthickness/.code={\setlength{\hatchthickness}{#1}},
         hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
         hatchcolor/.code={\renewcommand{\hatchcolor}{#1}}}
% setting the default values
\tikzset{hatchspread=3pt,
         hatchthickness=0.4pt,
         hatchshift=0pt,% must be >= 0
         hatchcolor=black}
% declaring the pattern
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]% variables
   {custom north west lines}% name
   {\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}% lower left corner
   {\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}{\dimexpr\hatchspread+2\hatchthickness}}% upper right corner
   {\pgfqpoint{\dimexpr\hatchspread}{\dimexpr\hatchspread}}% tile size
   {% shape description
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{-1.5\hatchthickness}{\dimexpr1.5\hatchthickness+\hatchspread+\hatchshift}}
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+1pt+\hatchshift}{-1pt}}
    \ifdim \hatchshift > 0pt
      \pgfpathmoveto{\pgfqpoint{0pt}{\hatchshift}}
      \pgfpathlineto{\pgfqpoint{\dimexpr1pt+\hatchshift}{-1pt}}
    \fi
    \pgfsetstrokecolor{\hatchcolor}
    \pgfusepath{stroke}
   }

\begin{document}
\begin{tikzpicture} 
\fill[left color=blue!40, right color=white,shading angle=45,line width=2pt,
postaction={pattern=custom north west lines, 
hatchthickness=1pt,hatchcolor=white}] (0,0) rectangle (3,3);
\end{tikzpicture}
\end{document}

enter image description here

  • I think the lines should extend beyond the bounds of the shape. – azetina Jun 9 at 18:01
  • @azetina There are always viewer issues with these patterns. In order to address your comment I (i) changed the code slightly and (ii) took the screen shot from acroread. – user121799 Jun 9 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.