To draw a rectangle and fill it in gray in TikZ:

\draw [fill=gray,very thick] (0,0) rectangle (2,1);

Now, I want to fill it in both gray and yellow. The best way would be if I can make diagonal stripes of gray and yellow alternately. Is there a way to do that?

  • 1
    In tikz these stripes are called patterns. The manual has a lot of examples for that. When the documented functions are not enough, have a look at tex.stackexchange.com/questions/54358/…
    – papabravo
    Apr 16, 2014 at 19:43
  • @papabravo Thanks, is there a quick way to implement what I wanted without having to define a custom pattern?
    – boaten
    Apr 16, 2014 at 19:50

3 Answers 3


A bit tricky solution without defining custom pattern for rectangle a=2xb.



\def\a{2}%width of rectangle
\def\b{\a/2}%height of rectangle
\draw[] (0,0) rectangle (\a,\b);
\foreach \x in{0,0.2,0.4,...,\a}{
\draw [gray,line width=\lw mm](\x,0)--(0,\x/2);
\draw [gray,line width=\lw mm](\a,\x/2)--(\x,\b);}

\foreach \x in{0.1,0.3,...,\a}{
\draw [yellow,line width=\lw mm](\x,0)--(0,\x/2);
\draw [yellow,line width=\lw mm](\a,\x/2)--(\x,\b);}



enter image description here


You can fill areas with some pattern, but you cannot combine fill with color and pattern on same path. If you want to use both, use preaction to fill with color and pattern to draw your pattern after filling.

Already predefined patterns use thin lines to draw, therefore if you want to use same width stripes, you need to define your own pattern.

Next you have two examples, both use preaction to fill with gray and pattern to draw orange stripes. First rectangle uses predefined north west lines pattern and second one a new north east hatch. (Taken from using pattern inside tikz shapes with dropped shadows)


\tikzset{% customization of pattern
         % based on <m.wibrow@gm...> - 2013-03-24 07:20: 
        hatch distance/.store in=\hatchdistance,
        hatch distance=5pt,
        hatch thickness/.store in=\hatchthickness,
        hatch thickness=5pt
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{north east hatch}% name
    {\pgfqpoint{-1pt}{-1pt}}% below left
    {\pgfqpoint{\hatchdistance}{\hatchdistance}}% above right

\begin{tikzpicture}[Pattern/.style={pattern=north east hatch, pattern color=orange, hatch distance=7pt, hatch thickness=2pt}]

\draw[preaction={fill=gray}, pattern=north west lines, pattern color=orange, draw=red] (0,0) rectangle (2,2.5);

\draw[preaction={fill=gray}, Pattern, draw=red] (2.5,0) rectangle (4.5,2.5);

enter image description here

  • I wanted to make north WEST hatch by making change to \pgfpathmoveto{\pgfqpoint{0pt}{\hatchdistance}} and \pgfpathlineto{\pgfqpoint{\hatchdistance}{0pt}}, but does not look well: line has white teeths. Do you know what went wrong?
    – Pygmalion
    Jul 1, 2019 at 18:36

Using the answer I linked to above, below is a solution. The package itself does not give you an easier way I'm afraid. Unless of course you draw the pattern by hand.



% defining the new dimensions and parameters
% declaring the keys in tikz
         hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
% setting the default values
         hatchshift=0pt,% must be >= 0
% 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
    \ifdim \hatchshift > 0pt
%    \pgfsetdash{{1pt}{1pt}}{0pt}% dashing cannot work correctly in all situation this way

\begin{tikzpicture}% hatchshift must be >= 0
\draw[pattern=custom north west lines,hatchthickness=5.5pt,hatchspread=15pt,hatchcolor=gray] (0,0) rectangle (2,2);
\draw[pattern=custom north west lines,hatchthickness=5.5pt,hatchspread=15pt,hatchcolor=orange,hatchshift=7.5pt] (0,0) rectangle (2,2);

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.