8

I'm using tikz to draw a snake-coil edge/path between nodes, but some edges have whitespace before/after its start or end node. Here is a small example:

    \documentclass{standalone}
    \usepackage{tikz}
    \usetikzlibrary[positioning,decorations.pathmorphing]
    \begin{document}
    \begin{tikzpicture}

      \tikzstyle{enclosed} = [draw, circle, inner sep=0pt, minimum size=.15cm, fill=black]
      \tikzstyle{enclosedM} = [draw, circle, inner sep=0pt, minimum size=.15cm, fill=red]

      \node[enclosed, label={left: $x$}] (x) at (0,2) {};
      \node[enclosed, label={right: $y$}] (y) at (4,2) {};
      \node[enclosed] (w) at (2,0) {};
      \node[enclosed] (v) at (2,4) {};
      \node[enclosedM, label={above: $z$}] (z) at (2,1) {};
      \node[enclosedM, label={below: $t$}] (t) at (2,3) {};

      \draw[decorate,decoration={snake,amplitude=.2mm}]
        (x) -- (v)
        (v) -- (y)
        (x) -- (w)
        (w) -- (y)
        (x) -- (t)
        (v) -- (t)
        (y) -- (t)
        (x) -- (z)
        (w) -- (z)
        (z) -- (y);
    \end{tikzpicture}
    \end{document}

The result is:PDF of what is produced from above

Notice that there is some whitespace between the center bottom node and node z. Also, there is whitespace between the top node and node y.

This still occurs even when "amplitude" is set to 0mm, which I thought would be equivalent to if the edge was not decorated at all. Setting "post length" and "pre length" options with various combinations fixes some edges, but then it creates the same problem with other edges that were OK before.

If I remove the decorate, decorations={...} and use \draw as normal with no settings, it works fine but the edges are not wavy. How can I get rid of the whitespace so it behaves as a normal edge but semi-wavy?

4

You have to handle decoration with care. I do not exactly know why this happens, but obviously, it is because you put multiple segments into one single \draw command.

You should write one separate \draw for every line segment. Or, to make it similar to your original version, you could use the following code:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,decorations.pathmorphing}
\begin{document}

\begin{tikzpicture}

  \tikzset{
   enclosed/.style={draw, circle, inner sep=0pt, minimum size=.15cm, fill=black},
   enclosedM/.style={enclosed, fill=red}
  }

  \node[enclosed, label={left: $x$}] (x) at (0,2) {};
  \node[enclosed, label={right: $y$}] (y) at (4,2) {};
  \node[enclosed] (w) at (2,0) {};
  \node[enclosed] (v) at (2,4) {};
  \node[enclosedM, label={above: $z$}] (z) at (2,1) {};
  \node[enclosedM, label={below: $t$}] (t) at (2,3) {};

  \foreach\x in {
    (x) -- (v),
    (v) -- (y),
    (x) -- (w),
    (w) -- (y),
    (x) -- (t),
    (v) -- (t),
    (y) -- (t),
    (x) -- (z),
    (w) -- (z),
    (z) -- (y)
  }
   \draw[decorate,decoration={snake,amplitude=.2mm}] \x;

\end{tikzpicture}

\end{document}

enter image description here

  • Thank you, @JasperHabicht and @marmot. This explains the issue. An example I found when learning TikZ (clearly not that long ago) used this multi-line syntax, so I just stuck with it because it worked. Good to know that separate \draw lines are more appropriate... and functional! – Agrajag Feb 18 '18 at 1:08
4

Two comments (not a real answer): tikzstyle is deprecated and if you draw the lines one by one there is no issue. (I have not checked if the problem is due to tikzstyle.) UPDATE: compactified the code and a big thanks to @Zarko for catching the square brackets!

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
  \tikzset{Snake/.style={decorate,decoration={snake,amplitude=.2mm}},
    enclosed/.style={draw, circle, inner sep=0pt, minimum size=.15cm,
    fill=black},
    enclosedM/.style={draw, circle, inner sep=0pt, minimum size=.15cm,
    fill=red}}

  \node[enclosed, label={left:$x$}] (x) at (0,2) {};
  \node[enclosed, label={right:$y$}] (y) at (4,2) {};
  \node[enclosed] (w) at (2,0) {};
  \node[enclosed] (v) at (2,4) {};
  \node[enclosedM, label={above:$z$}] (z) at (2,1) {};
  \node[enclosedM, label={below:$t$}] (t) at (2,3) {};
  \foreach \X in {x,y,v} \draw[Snake] (t) -- (\X);
  \foreach \X in {x,y,w} \draw[Snake] (z) -- (\X);
  \foreach \X in {x,y}{\foreach \V in {v,w}  \draw[Snake] (\V) -- (\X);}    
\end{tikzpicture}
\end{document}

enter image description here

  • It makes no difference whether you use \tikzset or \tikzstyle obviously. Off topic: Is it common to use \usetikzlibrary with square brackets? – Jasper Habicht Feb 17 '18 at 23:00
  • @JasperHabicht, no. i see this first time but apparently it works ... – Zarko Feb 17 '18 at 23:03
  • @Zarko Good catch! By permuting the lines in the OP's code I was able to move the gap around, and I see this syntax also for the first time. Anyway, the above code is much shorter and, more importantly, does not cause any issues. And I used \tikzset because otherwise cfr will remind me to do that ;-) And next I'll upvote Jasper's answer hoping that he does not try to catch me (Habicht means hawk;-). – user121799 Feb 17 '18 at 23:08
  • In the OP’s original version, options like pre length have no effect. So, it really seems that decorations can break if used on multi-segment paths. Is this a known issue? – Jasper Habicht Feb 17 '18 at 23:13
  • @JasperHabicht I think so, too. Evidently, the code does something, but not really what it should, and it is very simple to get the desired result with an even shorter code. What TikZ does internally is obscure anyway, and this is the first time I saw the OP's syntax, so I'm more surprised that it works so well. – user121799 Feb 17 '18 at 23:17

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.