46

...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.

13

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}
  • Sorry, that's not what I really want, I'd like to set the spacing for any pattern... – Count Zero Sep 28 '11 at 17:38
  • Ok have updated the solution to provide a way to control the spacing of the grid. This is similar to Gonzalo's solution, but has an extra feature of an adjustable parameter to control the size. – Peter Grill Sep 28 '11 at 18:05
  • 7
    Peter, is this still true now? I mean, maybe there have been updates of the library. Are we still unable to set the distance beween lines? – Alenanno Apr 6 '13 at 15:52
17

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)

13

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}
}
-4

Or you can use \hatchsep=3pt or more, so the lines separate more and have less of them.

  • 2
    Could you elaborate a little more on your suggested solution? Does this work with tikz as in the OP? – Werner Sep 20 '13 at 15:47
  • This is not an answer at all. It might be a comment to one of the other answers, but I cannot find \hatchsep on this page. – Scz Sep 8 '14 at 15:25

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.