59

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
81

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:

enter image description here

You find much more in the documentation (pgfmanual): chapter 62 Pattern Library.

  • @Claudio. Do you know how to apply this to the fill in large typeface? – Nicholas Hamilton Aug 29 '13 at 12:00
  • @ADP: what do you mean for large typeface? – Claudio Fiandrino Aug 30 '13 at 12:26
  • @Claudio say 100pt font used in a chapter page. – Nicholas Hamilton Aug 30 '13 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 '13 at 14:06
  • 1
    @Arne: actually I think this can be a problem of the visualizer. I suggest you to zoom and see if the problem persist. – Claudio Fiandrino Dec 9 '14 at 16:29
22

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}

enter image description here

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.