Hatch a rectangle in TikZ
You can use the library patterns
. For example:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\draw[pattern=north west lines, pattern color=blue] (0,0) rectangle (2,4);
\end{tikzpicture}
\end{document}
which gives you as result:
You find much more in the documentation (pgfmanual): chapter 63 Pattern Library.
Not exactly an answer but two examples of code to get a crosshatched rectangle without pattern
. This code was necessary with the first version of pgf/tikz.
It is possible to get a better code with clip
. The second one use fp.
\documentclass[11pt]{scrartcl}
\usepackage{tikz,ifthen,fp,calc}
\makeatletter
\newlength{\tkz@size}
\newlength{\tkz@rect@A}
\newlength{\tkz@rect@B}
\newlength{\tkz@rect@C}
\newlength{\tkz@rect@D}
\newlength{\tkz@hachsep}
\newboolean{tkz@rect@inv}\setboolean{tkz@rect@inv}{false}
\def\tkzhachrect[#1](#2,#3)(#4,#5){%
% A(#2,#3) D(#4,#5)
\draw (#2,#3) rectangle (#4,#5) ;
\setboolean{tkz@rect@inv}{false}
\setlength{\tkz@hachsep}{#1 cm}% sep des hach.
\setlength{\tkz@rect@A}{#2 cm + #3 cm}
\setlength{\tkz@rect@B}{#2 cm + #5 cm}
\setlength{\tkz@rect@C}{#4 cm + #3 cm}
\ifthenelse{\lengthtest{\tkz@rect@B > \tkz@rect@C}}%
{\setlength{\tkz@rect@C}{#2 cm + #5 cm}
\setlength{\tkz@rect@B}{#4 cm + #3 cm}
\setboolean{tkz@rect@inv}{true}%
}{}%
\setlength{\tkz@rect@D}{#4 cm + #5 cm}
\setlength{\tkz@size}{\tkz@rect@A}
\whiledo{\lengthtest{\tkz@size < \tkz@rect@D}}%
{\ifthenelse{\lengthtest{\tkz@size < \tkz@rect@B}}
{\draw[hstyle] (#2 cm,\tkz@size-#2 cm) -- (\tkz@size-#3 cm,#3 cm);}%else
{\ifthenelse{\lengthtest{\tkz@size < \tkz@rect@C}}
{\ifthenelse{\boolean{tkz@rect@inv}}
{\draw[hstyle] (#2 cm,\tkz@size-#2 cm) -- (#4 cm,\tkz@size-#4 cm);}
{\draw[hstyle] (\tkz@size - #5 cm,#5 cm) -- (\tkz@size-#3 cm,#3 cm);}%
}%
{\draw[hstyle] (\tkz@size - #5 cm,#5 cm) -- (#4 cm,\tkz@size-#4 cm);}}% fi
\addtolength{\tkz@size}{\tkz@hachsep}
}% end whiledo
}% end def
\def\tkzhachrectfp[#1](#2,#3)(#4,#5){%
\setboolean{tkz@rect@inv}{false}
\FPadd{\deb}{#2}{#3}
\FPtrunc\deb{\deb}{2}
\FPadd{\fin}{#4}{#5}
\FPtrunc\fin{\fin}{2}
\FPadd{\sone}{#2}{#5}
\FPtrunc\sone{\sone}{2}
\FPadd{\stwo}{#4}{#3}
\FPtrunc\stwo{\stwo}{2}
\FPifgt{\sone}{\stwo}
\FPset{\temp}{\sone}
\FPset{\sone}{\stwo}
\FPset{\stwo}{\temp}
\setboolean{tkz@rect@inv}{true}%
\else
\fi
\FPadd{\hach}{\deb}{#1}%
\FPtrunc\hach{\hach}{2}%
\draw (#2,#3) rectangle (#4,#5);
\foreach \s in {\deb ,\hach,...,\sone}
{\FPadd{\oo}{\s}{-#2}
\FPtrunc\oo{\oo}{2}%
\FPadd{\aa}{\s}{-#3}
\FPtrunc\aa{\aa}{2}%
\draw[hstyle] (#2,\oo) -- (\aa,#3);}%
\FPifeq{\sone}{\stwo}%
\else
\FPadd{\sone}{\sone}{#1}
\FPadd{\hach}{\sone}{#1}
\FPtrunc\hach{\hach}{2}
%
\foreach \s in {\sone ,\hach,...,\stwo}
{\ifthenelse{\boolean{tkz@rect@inv}}
{\FPadd{\oo}{\s}{-#2}
\FPtrunc\oo{\oo}{2}
\FPadd{\aa}{\s}{-#4}
\FPtrunc\aa{\aa}{2}
\draw[hstyle] (#2,\oo) -- (#4,\aa);}
{\FPadd{\oo}{\s}{-#5}
\FPtrunc\oo{\oo}{2}
\FPadd{\aa}{\s}{-#3}
\FPtrunc\aa{\aa}{2}
\draw[hstyle] (\oo,#5) -- (\aa,#3);}%
}% fin de la boucle
\fi%
\FPadd{\stwo}{\stwo}{#1}
\FPadd{\hach}{\stwo}{#1}
\FPtrunc\hach{\hach}{2}
\foreach \s in {\stwo,\hach,...,\fin}
{\FPadd{\oo}{\s}{-#5} \FPtrunc\oo{\oo}{2}
\FPadd{\aa}{\s}{-#4} \FPtrunc\aa{\aa}{2}
\draw[hstyle] (\oo,#5) -- (#4,\aa);}
}% end def
\begin{document}
\tikzset{hstyle/.style={blue,dashed}}
\begin{tikzpicture}
\tkzhachrect[0.1](2,1)(6,3)
\end{tikzpicture}
\tikzset{hstyle/.style={red,dotted}}
\begin{tikzpicture}
\tkzhachrectfp[0.2](2,1)(6,3)
\end{tikzpicture}
\tikzset{hstyle/.style={solid,thin}}
\begin{tikzpicture}
\tkzhachrectfp[0.2](2,1)(6,3)
\end{tikzpicture}
\tikzset{hstyle/.style={solid,thick}}
\begin{tikzpicture}
\tkzhachrectfp[0.2](2,1)(6,3)
\end{tikzpicture}
\end{document}