# Hatch a rectangle in TikZ

I know how to make a rectangle and fill it.

But what shoud be done to hatch this rectangle with thin 45 degree inclined line with 2mm gap.

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
%\draw (0,0) rectangle (2,4);
\fill[blue] (0,0) rectangle (2,4);

\end{tikzpicture}

\end{document}


Thank you Altermundus,

I want to hatch area under the curve. But I am unable to use your code as i did with

[pattern=north west lines, pattern color=blue]

Please give little explanation for your code so that i can use it.

\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}

\begin{tikzpicture}
\draw [thick,pattern=north west lines, pattern color=red] (1,0)--(1,1) to [bend left] (4,4) -- (4,0) --cycle;
\end{tikzpicture}

\end{document}

-
Actually, I don't have a clue what you want. Could you probably add a picture (handdrawn or whatever) of what you want to achieve? –  Tom Bombadil May 5 '12 at 9:48

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 41 Pattern Library.

-
Thanks Claudio Fiandrino –  sandu May 5 '12 at 9:59
@Claudio. Do you know how to apply this to the fill in large typeface? –  Nicholas Hamilton Aug 29 at 12:00
@ADP: what do you mean for large typeface? –  Claudio Fiandrino Aug 30 at 12:26
@Claudio say 100pt font used in a chapter page. –  Nicholas Hamilton Aug 30 at 13:58
@ADP: ultimately you want to apply a pattern to a text, is it? In that case I don't know (notice, the font size does not influence at all the applicability), but I guess it won't be so much easy. –  Claudio Fiandrino Aug 30 at 14:06
show 1 more comment

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
}% end whiledo
}% end def

\def\tkzhachrectfp[#1](#2,#3)(#4,#5){%
\setboolean{tkz@rect@inv}{false}
\FPtrunc\deb{\deb}{2}
\FPtrunc\fin{\fin}{2}
\FPtrunc\sone{\sone}{2}
\FPtrunc\stwo{\stwo}{2}
\FPifgt{\sone}{\stwo}
\FPset{\temp}{\sone}
\FPset{\sone}{\stwo}
\FPset{\stwo}{\temp}
\setboolean{tkz@rect@inv}{true}%
\else
\fi
\FPtrunc\hach{\hach}{2}%

\draw (#2,#3) rectangle (#4,#5);

\foreach \s in {\deb ,\hach,...,\sone}
\FPtrunc\oo{\oo}{2}%
\FPtrunc\aa{\aa}{2}%
\draw[hstyle] (#2,\oo) -- (\aa,#3);}%
\FPifeq{\sone}{\stwo}%
\else
\FPtrunc\hach{\hach}{2}
%
\foreach \s in {\sone ,\hach,...,\stwo}
{\ifthenelse{\boolean{tkz@rect@inv}}
\FPtrunc\oo{\oo}{2}
\FPtrunc\aa{\aa}{2}
\draw[hstyle] (#2,\oo) -- (#4,\aa);}
\FPtrunc\oo{\oo}{2}
\FPtrunc\aa{\aa}{2}
\draw[hstyle] (\oo,#5) -- (\aa,#3);}%
}% fin de la boucle
\fi%

\FPtrunc\hach{\hach}{2}

\foreach \s in {\stwo,\hach,...,\fin}
\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}


-