[Tex/LaTex] control the ‘density’ of a pattern in TikZ

tikz-pgf

…i.e. I want to control the spacing between lines in a hatch pattern. Is that possible at all with the predefined patterns from the pattern library, like north east lines? Or should I just create my own pattern? I tried the step modifier, but it has no effect as in the minimal non-working example below. 😉

\begin{tikzpicture}
    \draw [step=0.5cm, pattern=north east lines] (0,0) rectangle (1.4,1.4);
\end{tikzpicture}

EDIT: I changed the code a bit, because I would like to control the spacing in any type of pattern and originally in the example was grid.

Best Answer

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}