7

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 '14 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 '14 at 19:50
4

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

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\def\a{2}%width of rectangle
\def\b{\a/2}%height of rectangle
\def\lw{0.1}
\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);}

\end{tikzpicture}

\end{document}

enter image description here

5

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)

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}

\makeatletter
\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
    {\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
    {
        \pgfsetcolor{\tikz@pattern@color}
        \pgfsetlinewidth{\hatchthickness}
        \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
        \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
        \pgfusepath{stroke}
    }
\makeatother

\begin{document}
\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);
\end{tikzpicture}
\end{document}

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 at 18:36
2

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.

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{patterns}

% defining the new dimensions and parameters
\newlength{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
% declaring the keys in tikz
\tikzset{hatchspread/.code={\setlength{\hatchspread}{#1}},
         hatchthickness/.code={\setlength{\hatchthickness}{#1}},
         hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
         hatchcolor/.code={\renewcommand{\hatchcolor}{#1}}}
% setting the default values
\tikzset{hatchspread=3pt,
         hatchthickness=0.4pt,
         hatchshift=0pt,% must be >= 0
         hatchcolor=black}
% 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
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{\dimexpr\hatchspread+\hatchshift}}
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+0.15pt+\hatchshift}{-0.15pt}}
    \ifdim \hatchshift > 0pt
      \pgfpathmoveto{\pgfqpoint{0pt}{\hatchshift}}
      \pgfpathlineto{\pgfqpoint{\dimexpr0.15pt+\hatchshift}{-0.15pt}}
    \fi
    \pgfsetstrokecolor{\hatchcolor}
%    \pgfsetdash{{1pt}{1pt}}{0pt}% dashing cannot work correctly in all situation this way
    \pgfusepath{stroke}
   }

\begin{document}
\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);
\end{tikzpicture}%
\end{document}

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.