TransWikia.com

Hatch a rectangle in TikZ

TeX - LaTeX Asked by sandu on February 24, 2021

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}

2 Answers

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

Correct answer by Claudio Fiandrino on February 24, 2021

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}

deftkzhachrect[#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

deftkzhachrectfp[#1](#2,#3)(#4,#5){%
setboolean{tkz@rect@inv}{false}
FPadd{deb}{#2}{#3}
FPtruncdeb{deb}{2}
FPadd{fin}{#4}{#5}
FPtruncfin{fin}{2} 
FPadd{sone}{#2}{#5}
FPtruncsone{sone}{2}
FPadd{stwo}{#4}{#3}
FPtruncstwo{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}%
FPtrunchach{hach}{2}%

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

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

FPadd{stwo}{stwo}{#1}
FPadd{hach}{stwo}{#1}
FPtrunchach{hach}{2}

 foreach s in {stwo,hach,...,fin}
 {FPadd{oo}{s}{-#5} FPtruncoo{oo}{2}
  FPadd{aa}{s}{-#4} FPtruncaa{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

Answered by Alain Matthes on February 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP