3

A while ago, I have stumbled upon a very nice solution for parallel paths drawn with TikZ, which can be found here: Draw additional parallel paths in TikZ.

However, when trying to use this for my purposes - namely in combination with the shorten >= and shorten <= options - I discovered that this solution doesn't quite behave as expected. Here is an MWE:

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

\def\pgfdecoratedcontourdistance{0pt}

\pgfkeys{/pgf/decoration/contour distance/.code={%
    \pgfmathparse{#1}%
    \let\pgfdecoratedcontourdistance=\pgfmathresult}%
}

\pgfdeclaredecoration{contour lineto}{start}
{
    \state{start}[next state=draw, width=0pt]{
        \pgfpathmoveto{\pgfpoint{0pt}{\pgfdecoratedcontourdistance}}%
    }
    \state{draw}[next state=draw, width=\pgfdecoratedinputsegmentlength]{       
        \pgfmathparse{-\pgfdecoratedcontourdistance*cot(-\pgfdecoratedangletonextinputsegment/2+90)}%
        \let\shorten=\pgfmathresult%
        \pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentlength+\shorten}{\pgfdecoratedcontourdistance}}%  
    }
    \state{final}{
        \pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentlength}{\pgfdecoratedcontourdistance}}%
    }   
}

\begin{document}
  \tikzset{
    box/.style={draw=black, fill=blue!20, inner sep=1em},
    arrow/.style={->,draw=black, shorten >=1mm,shorten <=1mm},
    quintuple/.style={
      postaction={decoration={contour lineto, contour distance=-4pt},draw=black, decorate},
      postaction={decoration={contour lineto, contour distance=-2pt},draw=black, decorate},
      postaction={decoration={contour lineto, contour distance=+2pt},draw=black, decorate},
      postaction={decoration={contour lineto, contour distance=+4pt},draw=black, decorate},
    },
  }
  \begin{tikzpicture}
    \node[box] (box1) {Box 1};
    \node[box,above right=of box1] (box2) {Box 2};
    \draw[arrow,quintuple] (box1) |- (box2);
  \end{tikzpicture}
\end{document}

The picture this produces looks like this:

Screwed up arrows

The arrow ends are all screwed up, and I can't figure out why this happens. Can anybody provide a solution that has nice arrow ends (or at least explain what's happening here)?

3
  • One note: \tikzset{} goes in the preamble. (before \begin{document}).
    – Alenanno
    Commented May 28, 2015 at 20:21
  • 2
    @Alenanno No need. That's \usetikzlibrary{} that is important. \tikzset is just key setter and can set anywhere.
    – percusse
    Commented May 28, 2015 at 20:37
  • @percusse Oh, I see, it's not mandatory. I still prefer settings in the preamble. :P
    – Alenanno
    Commented May 28, 2015 at 20:39

1 Answer 1

2

It is the final state that is messing things up, but in your case the final state is not needed because you don't need or want the added paths to end at the same point as the base path.

Simply remove the declaration of the final state and everything works as we'd like:

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

\def\pgfdecoratedcontourdistance{0pt}

\pgfkeys{/pgf/decoration/contour distance/.code={%
    \pgfmathparse{#1}%
    \let\pgfdecoratedcontourdistance=\pgfmathresult}%
}

\pgfdeclaredecoration{contour lineto}{start}
{
    \state{start}[next state=draw, width=0pt]{
        \pgfpathmoveto{\pgfpoint{0pt}{\pgfdecoratedcontourdistance}}%
    }
    \state{draw}[next state=draw, width=\pgfdecoratedinputsegmentlength]{       
        \pgfmathparse{-\pgfdecoratedcontourdistance*cot(-\pgfdecoratedangletonextinputsegment/2+90)}%
        \let\shorten=\pgfmathresult%
        \pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentlength+\shorten}{\pgfdecoratedcontourdistance}}%  
    }
}

\begin{document}
  \tikzset{
    box/.style={draw=black, fill=blue!20, inner sep=1em},
    arrow/.style={->,draw=black, shorten >=1mm,shorten <=1mm},
    quintuple/.style={
      postaction={decoration={contour lineto, contour distance=-4pt},draw=black, decorate},
      postaction={decoration={contour lineto, contour distance=-2pt},draw=black, decorate},
      postaction={decoration={contour lineto, contour distance=+2pt},draw=black, decorate},
      postaction={decoration={contour lineto, contour distance=+4pt},draw=black, decorate},
    },
  }
  \begin{tikzpicture}
    \node[box] (box1) {Box 1};
    \node[box,above right=of box1] (box2) {Box 2};
    \draw[arrow,quintuple] (box1) |- (box2);
    \draw[arrow,quintuple] (box1) -| (box2);
  \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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