12

I'm trying to create a custom pattern made of zigzag lines. Thanks to the TikZ manual and to Decorating (with random steps) a filling pattern I'm almost there:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing, patterns,shapes}
\begin{document}
\pgfdeclarepatternformonly{zigzag}
{\pgfpointorigin}{\pgfpoint{1cm}{1cm}}
{\pgfpoint{0.3cm}{0.3cm}}
{
\tikz\draw[decoration = {zigzag,segment length = 2mm, amplitude = 2mm}, decorate] (0,0) -- ++(3,0);
}
\begin{tikzpicture}
\node[draw=black,pattern=zigzag,regular polygon,regular polygon sides=5,minimum size=10mm] at (0,0.5){};
\end{tikzpicture}
\end{document}

The above code produces this:
enter image description here
But what I'd like is something like this:
enter image description here
I'd like the zigzag line to be more "relaxed". I've messed up with the \pgfdeclarepatternformonly parameters but I'm not able to obtain the zigzag line I like.

Also, I don't understand what the purpose of ++ is in (0,0) -- ++(3,0).

More Details

the zigzag line I want is produced by this code
\draw[decoration = {zigzag,segment length = 3mm, amplitude = 1mm},decorate] (0,0)--(1,0);
enter image description here
but it doesn't work as expected. When inserted in the previous code it prodces this: enter image description here


Solved (but I still don't understand why)

So, I solved my problem by trials and errors. The code that produces the pattern I want is this:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing, patterns,shapes}
\begin{document}
\pgfdeclarepatternformonly{zigzag}
{\pgfpointorigin}{\pgfpoint{2cm}{2cm}}{\pgfpoint{0.35cm}{0.15cm}}
{
\tikz\draw[decoration = {zigzag,segment length = 0.35cm, amplitude = 0.9mm},decorate,very thin] (0,1.5) -- (2.9,1.5);
}
\begin{tikzpicture}
\node[draw=black,pattern=zigzag,regular polygon,regular polygon sides=5,minimum size=10mm] at (0,0){};
\end{tikzpicture}
\end{document}

enter image description here

Unfortunately I still have a vague undestanding of what I did, so I'll accept as correct answer that which will explain why this code works.

  • Would Custom and built-in TikZ fill patterns help? – percusse Mar 24 '13 at 13:24
  • @percusse I believe that I already have all the code I need, its just that I don't understand how to set the \pgfdeclarepatternformonly parameters to obtain the zigzag line I want. If you change the pgfpoint, segment lenght and amplitude values you'll get different zigzag lines. Problem is I don't understand how they interact with each other so I can't get what I want. – Nico Boni Mar 24 '13 at 13:29
10

I have tried to explain in an example why this is happening. The main problem is how to tile the pictures. The tiles don't behave like bounding boxes so they can overlap even though the picture drawn inside the tiles are larger. So to be able to engage the teeth you need to reduce the tile size to some box smaller than the tile picture.

Here the atom of the pattern to be tiled can either be a decorated path or a manually drawn zigzag line, say only two threads. Then these tiles should be stitched together not with respect to the tile picture but with respect to the tile size. Hence a little care is required to put things together.

I've made an example with the decoration option and put some comments in it. You don't need to nest another TikZ picture inside a repeating pattern so I've converted them to lower level commands for simplicity. Also I've marked the 2mm legend to judge the lengths of the decoration. You can also convert the hard-coded numbers to pattern options if desired.

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing, patterns,shapes,arrows}

\pgfdeclarepatternformonly{zigzagpat}
{\pgfpointorigin}
{\pgfpoint{8mm}{2mm}} % Upper right corner so the box is 8x2 mm
{\pgfpoint{8mm}{1mm}} % The tile size is smaller than the picture itself 
                      % So the teeth are getting closer
{
\pgfdecoration{{zigzag}{\pgfdecoratedpathlength}{%Setting up the decoration for the full path
                        \pgfdecorationsegmentlength=4mm      % The zigzag thread width
                        \pgfdecorationsegmentamplitude=1mm   % The zigzag thread height
                   }
              }
\pgfpathmoveto{\pgfpoint{0cm}{1mm}}        % So first we move 1 mm up
\pgfpathlineto{\pgfpoint{8mm}{1mm}}        % And draw a straight horizontal line to the end of our box
\endpgfdecoration                          % Close the decoration environment
\pgfusepathqstroke                         % Now draw the decorated path
}


\begin{document}

\begin{tikzpicture}
\node[draw=black,pattern=zigzagpat,regular polygon,regular polygon sides=5,minimum size=10mm] at (0,0){};
\draw[|-|,red] (0,-1mm) -- ++(0,2mm);
\end{tikzpicture}

\end{document}

enter image description here

  • thank you very much for the great explanation, now it's clear :) – Nico Boni Mar 24 '13 at 16:09
  • @dcmst My pleasure – percusse Mar 24 '13 at 16:36

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.