Can I control the 'density' of a pattern in TikZ?

The answer of Peter works only for \GridSize smaller than 4pt, if you want to modify the pattern in order to accept all \GrideSize you have to use the following code:

\makeatletter
\pgfdeclarepatternformonly[\GridSize]{MyGrid}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\GridSize}{\GridSize}}{\pgfqpoint{\GridSize}{\GridSize}}%
{
     \pgfsetcolor{\tikz@pattern@color}
     \pgfsetlinewidth{0.3pt}
     \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
     \pgfpathlineto{\pgfqpoint{0pt}{\GridSize + 0.1pt}}
     \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
     \pgfpathlineto{\pgfqpoint{\GridSize + 0.1pt}{0pt}}
     \pgfusepath{stroke}
}
\makeatother

\newdimen\GridSize
\tikzset{
    GridSize/.code={\GridSize=#1},
    GridSize=3pt
}

Then if you want to modify al type of patterns to accept this density option you have to modify all pattern's codes inserting them in your preamble or directly in the file pgflibrarypatterns.code.tex. Here there are other two example of such modified patterns:

\makeatletter
\pgfdeclarepatternformonly[\LineSpace]{my north east lines}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\LineSpace}{\LineSpace}}{\pgfqpoint{\LineSpace}{\LineSpace}}%
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{0.4pt}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\LineSpace + 0.1pt}{\LineSpace + 0.1pt}}
    \pgfusepath{stroke}
}

\pgfdeclarepatternformonly[\LineSpace]{my north west lines}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\LineSpace}{\LineSpace}}{\pgfqpoint{\LineSpace}{\LineSpace}}%
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{0.4pt}
    \pgfpathmoveto{\pgfqpoint{0pt}{\LineSpace}}
    \pgfpathlineto{\pgfqpoint{\LineSpace + 0.1pt}{-0.1pt}}
    \pgfusepath{stroke}
}
\makeatother

\newdimen\LineSpace
\tikzset{
    line space/.code={\LineSpace=#1},
    line space=3pt
}

In any case there is a problem of color specification (see Modified pattern does not see the pattern color option)


As Gonzalo says the patterns are not customizable. So, here is one way you can customize the code for the grid pattern to accept a variable that allows you to control the density by adjusting the GridSize=1pt. The default is GridSize=3pt

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

\makeatletter
\pgfdeclarepatternformonly[\GridSize]{MyGrid}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{4pt}{4pt}}{\pgfqpoint{\GridSize}{\GridSize}}%
{
  \pgfsetcolor{\tikz@pattern@color}
  \pgfsetlinewidth{0.3pt}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0pt}{3.1pt}}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{3.1pt}{0pt}}
  \pgfusepath{stroke}
}
\makeatother

\newdimen\GridSize
\tikzset{
    GridSize/.code={\GridSize=#1},
    GridSize=3pt
}

\begin{document}
\begin{tikzpicture}[grid/.style={pattern=MyGrid}]
   \draw [GridSize=1pt, pattern=MyGrid] (0,0) rectangle (1.4,1.4);
\end{tikzpicture}
\end{document}

A quick look at the file pgflibrarypatterns.code.tex reveals that the predefined patterns do not accept modifications through keys. You will have to create your own pattern, or modify the existing ones to make them key-aware.

The following code shows the definition of a my grid (more dense grid) pattern, side by side with the predefined grid:

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

\pgfdeclarepatternformonly{my grid}{\pgfqpoint{-2pt}{-2pt}}{\pgfqpoint{4pt}{4pt}}{\pgfqpoint{2pt}{2pt}}%
{
  \pgfsetlinewidth{0.4pt}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0pt}{3.1pt}}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{3.1pt}{0pt}}
  \pgfusepath{stroke}
}
\begin{document}

\begin{tikzpicture}
    \draw [step=0.5cm, pattern=my grid] (0,0) rectangle (1.4,1.4);
    \draw [step=0.5cm, pattern=grid] (2,0) rectangle (3.4,1.4);
\end{tikzpicture}

\end{document}

enter image description here

Here's the oritional definition of the grid pattern:

\pgfdeclarepatternformonly{grid}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{4pt}{4pt}}{\pgfqpoint{3pt}{3pt}}%
{
  \pgfsetlinewidth{0.4pt}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0pt}{3.1pt}}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{3.1pt}{0pt}}
  \pgfusepath{stroke}
}

Tags:

Tikz Pgf