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.

How do I make a TikZ decoration with a 1cm zigzag in the middle?

Here's what I have so far, but it didn't work:

\pgfdeclaremetadecoration{timearrow}{initial}{
    \state{initial}[width=0pt, next state=arrow] {
        \pgfsetlinewidth{1pt}
        \pgfset{/pgf/decoration/segment length=8pt}
        \pgfmathtruncatemacro{\blah}{\pgfmetadecoratedpathlength/2 - 1cm}
    }
    \state{arrow}[switch if less than=1cm to final,
                  width=\blah ,
                  next state=zigzag] {
        \decoration{curveto}
        \beforedecoration {\pgfpathmoveto{\pgfpointmetadecoratedpathfirst}}
    }
    \state{zigzag}[width=1cm,
                   next state=final] {
        \decoration{zigzag}
    }
    \state{final}[] {
        \decoration{curveto}
        \beforedecoration{\pgfpathmoveto{\pgfpointmetadecoratedpathfirst}}
    }
}
share|improve this question
    
Have a look at Draw the discontinuity symbol with tikz. –  Martin Scharrer Oct 30 '11 at 22:34
add comment

1 Answer

up vote 2 down vote accepted

Your style worked for me, except for the fact that the zigzag segment wasn't centred correctly because you shifted it by 1cm, not by 0.5cm.

I would suggest to keep as many of the lengths in your decoration as variables, instead of hard coding your values. If you set \pgfset{/pgf/decoration/segment length=8pt}, you rob yourself of the freedom to change the zigzag wavelength by setting segment length=<value> as an option to your decoration. The same goes for \pgfsetlinewidth{1pt}: If you leave out this line, you can just specify the line width as you would with any other TikZ path. If you need to draw the decoration frequently with the same values, you can just define a new style that sets these for you.

Also, you don't need to say \beforedecoration{\pgfpathmoveto{\pgfpointmetadecoratedpathfirst}} at the beginning of each segment if your segments are joined.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{decorations,decorations.pathmorphing}

\pgfdeclaremetadecoration{middlezigzag}{straight}{
    \state{straight}[switch if less than=\pgfmetadecorationsegmentlength to final,
                  width=\pgfmetadecoratedpathlength/2 - \pgfmetadecorationsegmentlength/2 ,
                  next state=zigzag] {
        \decoration{curveto}
    }
    \state{zigzag}[width=\pgfmetadecorationsegmentlength,
                   next state=final] {
        \decoration{zigzag}
    }
    \state{final}{
        \decoration{curveto}
        \beforedecoration{\pgfpathmoveto{\pgfpointmetadecoratedpathfirst}}
    }
}

\tikzset{
    middle zigzag/.style={
        decorate,
        decoration={
            middlezigzag,
            meta-segment length=#1,
            segment length=0.5cm
        }
    },
    middle zigzag/.default=1cm
}


\begin{document}
\begin{tikzpicture}
\draw [gray!50,yshift=-0.5cm] (0,0) grid (5,1);
\draw [middle zigzag, ultra thick] (0,0) -- (5,0);
\end{tikzpicture}
\end{document}
share|improve this answer
    
Wow! Thanks! I was just going to adapt the solution at Martin's link, but I'm glad that I was too busy today to do this as I really appreciate all of the tips you included with your solution. –  Neil G Oct 31 '11 at 6:48
add comment

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.