Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I can't find much information about creating TikZ fill patterns or even using the custom built-in patterns. Look to do all the common fills like lines, hatches, dots, stars, etc... I need to be able to control the scale, too. (The patterns I did end up finding were way to large.)

Google doesn't seem to return much info on TikZ fill patterns.

share|improve this question
2  
Refer chapter 41 of pgf manual on patterns. –  Harish Kumar May 4 '12 at 8:44
    
@HarishKumar This gives some(or all?) built in patterns but doesn't describe how to change the scale or create built in ones? –  Uiy May 4 '12 at 9:43
    
you may have to look in to the source code of pattern library. –  Harish Kumar May 4 '12 at 10:11
    
Refer also to chapter 62 (pgf2.00) or chapter 78 (pgf2.10) to define custom patterns. –  guillem May 4 '12 at 10:23
    
possible duplicate of Hatch a rectangle in TikZ –  lockstep May 6 '12 at 8:59

1 Answer 1

Customizing an existing pattern

The fivepointed star pattern is defined in pgflibrarypatterns.code.tex as

\pgfdeclarepatternformonly{fivepointed stars}% name
  {\pgfpointorigin}% bottom left
  {\pgfqpoint{3mm}{3mm}}% top right
  {\pgfqpoint{3mm}{3mm}}% tile size
  {% code
   \pgftransformshift{\pgfqpoint{1mm}{1mm}}
   \pgfpathmoveto{\pgfqpointpolar{18}{1mm}}
   \pgfpathlineto{\pgfqpointpolar{162}{1mm}}
   \pgfpathlineto{\pgfqpointpolar{306}{1mm}}
   \pgfpathlineto{\pgfqpointpolar{90}{1mm}}
   \pgfpathlineto{\pgfqpointpolar{234}{1mm}}
   \pgfpathclose%
   \pgfusepath{fill}
  }

If you want to change the size of stars or how far away they are of each other, you just need to change the 1mm and 3mm values respectively, but you need to define a new pattern for that. If you want full flexibility, you need to define a pattern with variables which you can adjust. Let's say we want the starsize and starspread parameters to customize the pattern. We first define the lengths:

% defining the new dimensions
\newlength{\starsize}
\newlength{\starspread}

then declare the corresponding tikz keys:

% declaring the keys in tikz
\tikzset{starsize/.code={\setlength{\starsize}{#1}},
         starspread/.code={\setlength{\starspread}{#1}}}

then set the default values:

% setting the default values
\tikzset{starsize=1mm,
         starspread=3mm}

and finally declare the pattern itself using our variables:

% declaring the pattern
\pgfdeclarepatternformonly[\starspread,\starsize]% variables
  {custom fivepointed stars}% name
  {\pgfpointorigin}% lower left corner
  {\pgfqpoint{\starspread}{\starspread}}% upper right corner
  {\pgfqpoint{\starspread}{\starspread}}% tilesize
  {% shape description
   \pgftransformshift{\pgfqpoint{\starsize}{\starsize}}
   \pgfpathmoveto{\pgfqpointpolar{18}{\starsize}}
   \pgfpathlineto{\pgfqpointpolar{162}{\starsize}}
   \pgfpathlineto{\pgfqpointpolar{306}{\starsize}}
   \pgfpathlineto{\pgfqpointpolar{90}{\starsize}}
   \pgfpathlineto{\pgfqpointpolar{234}{\starsize}}
   \pgfpathclose%
   \pgfusepath{fill}
  }

You can then just use things like \draw[pattern=custom fivepointed stars,starspread=1.5mm,starsize=0.5mm] (0,0) rectangle (3,4); to produce small stars close to each other in a rectangle. Here's the full code:

tikz star patterns examples

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{patterns}

% defining the new dimensions
\newlength{\starsize}
\newlength{\starspread}
% declaring the keys in tikz
\tikzset{starsize/.code={\setlength{\starsize}{#1}},
         starspread/.code={\setlength{\starspread}{#1}}}
% setting the default values
\tikzset{starsize=1mm,
         starspread=3mm}
% declaring the pattern
\pgfdeclarepatternformonly[\starspread,\starsize]% variables
  {custom fivepointed stars}% name
  {\pgfpointorigin}% lower left corner
  {\pgfqpoint{\starspread}{\starspread}}% upper right corner
  {\pgfqpoint{\starspread}{\starspread}}% tilesize
  {% shape description
   \pgftransformshift{\pgfqpoint{\starsize}{\starsize}}
   \pgfpathmoveto{\pgfqpointpolar{18}{\starsize}}
   \pgfpathlineto{\pgfqpointpolar{162}{\starsize}}
   \pgfpathlineto{\pgfqpointpolar{306}{\starsize}}
   \pgfpathlineto{\pgfqpointpolar{90}{\starsize}}
   \pgfpathlineto{\pgfqpointpolar{234}{\starsize}}
   \pgfpathclose%
   \pgfusepath{fill}
  }

\begin{document}

\begin{center}
%
\begin{tikzpicture}
\draw[pattern=fivepointed stars] (0,0) rectangle (3,4);
\end{tikzpicture}%
%
\begin{tikzpicture}
\draw[pattern=custom fivepointed stars,starspread=5mm] (0,0) rectangle (3,4);
\end{tikzpicture}%
%
\begin{tikzpicture}
\draw[pattern=custom fivepointed stars,starspread=5mm,starsize=2mm] (0,0) rectangle (3,4);
\end{tikzpicture}%
%
\begin{tikzpicture}
\draw[pattern=custom fivepointed stars,starspread=1.5mm,starsize=0.5mm] (0,0) rectangle (3,4);
\end{tikzpicture}
%
\end{center}

\end{document}

In the same vein, you can customize hatch lines. Here's the code for north west lines. I've put options for color and shifting the hatch lines up (not down):

tikz hatch pattern which can be colored

\documentclass{article}

\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{center}
%
\begin{tikzpicture}
\draw[pattern=custom north west lines] (0,0) rectangle (3,4);
\end{tikzpicture}%
%
\begin{tikzpicture}
\draw[pattern=custom north west lines,hatchspread=6pt,hatchthickness=1pt,hatchcolor=red] (0,0) rectangle (3,4);
\end{tikzpicture}%
%
\begin{tikzpicture}
\draw[pattern=custom north west lines,hatchspread=10pt,hatchthickness=5pt] (0,0) rectangle (3,4);
\end{tikzpicture}%
%
\begin{tikzpicture}% hatchshift must be >= 0
\draw[pattern=custom north west lines,hatchspread=10pt,hatchcolor=red] (0,0) rectangle (3,4);
\draw[pattern=custom north west lines,hatchspread=10pt,hatchcolor=blue,hatchshift=2.5pt] (0,0) rectangle (3,4);
\draw[pattern=custom north west lines,hatchspread=10pt,hatchcolor=green!65!black,hatchshift=5pt] (0,0) rectangle (3,4);
\draw[pattern=custom north west lines,hatchspread=10pt,hatchcolor=violet,hatchshift=7.5pt] (0,0) rectangle (3,4);
\end{tikzpicture}%
%
\end{center}

\end{document}

Creating new patterns

Creating new patterns is a bit more complex and needs choosing the right shape, the bounding box overlapping, etc. You can see an example with hexagons in the question Drawing Hexagons. I didn't do the work to control the size within Tikz, but it's straightforward to adapt the method described above.

share|improve this answer
    
Thank you for the comprehensive answer! –  percusse Jun 18 '12 at 10:58
    
Philippe, nice answer. I was wondering, can you change the color of the lines or their style (dashed, dotted, etc)? And another thing, what if you put two of these slightly overlapping each other, how do you make sure both lines/colors are easily visible? If you just overlap them as they are, the second one covers the first one. I tried a yshift but it doesn't work well. :P –  Alenanno Apr 5 '13 at 15:02
1  
@Alenanno: I've added options for the color and the shifting. Dotted or dashed lines won't work correctly because of how the patterns are made. –  Philippe Goutet Apr 7 '13 at 6:47
    
@PhilippeGoutet Brilliant, thanks. Yeah, well I've noticed... are the lines made of many shorter lines in sequence? Is that the reason? –  Alenanno Apr 7 '13 at 8:39
    
@Alenanno: Yes, that's the reason. If you really want dashed hatching, you could clip to your shape some lines made with a for loop, but it's a bit cumbersome. –  Philippe Goutet Apr 7 '13 at 10:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.