7

I would like to place a specified number of zigzags along a path.
That means, in the following MWE are exactly 4 positive and 4 negativ amplitudes around the black line.

How can I avoid the strange line at the end of a zigzag decoration?

enter image description here

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing, decorations.markings}
\begin{document}

\begin{tikzpicture}
\pgfmathsetmacro\r{1.5}
\pgfmathsetmacro\u{2*pi*\r}% Length
\pgfmathsetmacro\s{\u/4}% 4 zigzags over length

\draw[blue, rounded corners]
decorate[decoration={zigzag,segment length=\s*1cm,amplitude=4.5mm,
%post=lineto,post length=0pt % no effect
}
] { (0,0) -- (\u,0)  
};

\draw (0,0) -- (\u,0); 
\end{tikzpicture}
\end{document}
2
  • 2
    This is because of the rounded corners option, which rounds off the end of the curve. Try decoration=snake without rounded corners
    – AndréC
    Commented Jun 19 at 18:07
  • Looks better, but there is an important point: "I would like to place a specified number of zigzags along a path."; I added: "That means, in the following MWE are exactly 4 positive and 4 negativ amplitudes around the black line. "
    – cis
    Commented Jun 19 at 19:24

1 Answer 1

7
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{decorations.pathmorphing}

\makeatletter
\pgfdeclaredecoration{rigrag}{up from center}{%
  \state{up from center}[width=+.5\pgfdecorationsegmentlength, next state=big down]
  {
    \pgfpathlineto{\pgfqpoint{.25\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
  }%
  \state{big down}[switch if less than=+.5\pgfdecorationsegmentlength to center finish,
                   width=+.5\pgfdecorationsegmentlength,
                   next state=big up]
  {
    \pgfpathlineto{\pgfqpoint{.25\pgfdecorationsegmentlength}{-\pgfdecorationsegmentamplitude}}
  }%
  \state{big up}[switch if less than=+.5\pgfdecorationsegmentlength to center finish,
                 width=+.5\pgfdecorationsegmentlength,
                 next state=big down]
  {
    \pgfpathlineto{\pgfqpoint{.25\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
  }%
  \state{center finish}[width=0pt, next state=final]{
    \pgfpathlineto{\pgfpointorigin}
  }%
  \state{final}
  {
  %  \pgfpathlineto{\pgfpointdecoratedpathlast} %do not draw final lineto
  }%
}%
\makeatother

\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro\r{1.5}
\pgfmathsetmacro\u{2*pi*\r}% Length
\pgfmathsetmacro\s{\u/4}% 4 zigzags over length
\draw[red, decoration={rigrag, segment length=\s cm, amplitude=4.5mm}, decorate, rounded corners] (0,0) -- (\u,0);
\draw (0,0) -- (\u,0);
\end{tikzpicture}
\end{document}

A line and four zigzags

As @cfr correctly writes, the code is copied and modified from

texmf-dist/tex/generic/pgf/libraries/decorations/pgflibrarydecorations.pathmorphing.code.tex
2
  • 3
    Would you mind adding the file where you got the code to modify? (I'm assuming you modified zigzag to produce rigrag.) There are so many files in pgf/tikz, I think it would help people who don't know where to look. (I assume pgflibrarydecorations.pathmorphing.code.tex, although it's also an example in the docs, so maybe the relevant file for that or, even, pgfmoduledecorations.code.tex.)
    – cfr
    Commented Jun 20 at 4:29
  • Thanks a lot. I would like to get to the bottom of 'segment length' in more detail, so I created a follow-up question here: tex.stackexchange.com/q/720997/46023
    – cis
    Commented Jun 20 at 14:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .