3

This is a follow up question of two other questions. The first is Node at every vertex and the second one is this. My goal is to combine the two. In particular, I want to write something like

 \draw[polyline] (0,0) -- (1,1) -- (2,0) -- (3,1);

And the result would be a polyline (polygon) such that each vertex will be marked with a circle and there will be an arrow at the middle of each segment. This is what I have so far:

\begin{tikzpicture}[
  mnode/.style={circle,draw=black,fill=black,inner sep=0pt,minimum size=2pt},
  node at every point/.style={%
    decoration={%
      show path construction,
      lineto code={%
        \coordinate (pos) at ($0.5*(\tikzinputsegmentlast)+0.5*(\tikzinputsegmentfirst)$);
        \coordinate (direction) at ($(\tikzinputsegmentlast)-(\tikzinputsegmentfirst)$);
        \gettikzxy{(direction)}{\dirx}{\diry}
        \pgfmathsetmacro\rotationdir{atan(\diry/\dirx)}
        \node at (pos) [rotate=\rotationdir] {>};
        \node at (\tikzinputsegmentlast) [#1] {};
      },
      moveto code={\node at (\tikzinputsegmentfirst) [#1] {};},
      curveto code={\node at (\tikzinputsegmentlast) [#1] {};}
    },
    postaction=decorate
  }
  ]
  \draw[node at every point=mnode] (0,0) -- (1,2) -- (2.3,0) -- (2.5,1);
\end{tikzpicture}

And this is the result:

image

This code exhibits two problems. First, the arrow looks bad, and secondly it cannot handle vertical segments. In the latter case, \dirx is zero and the code fails. I tried to hack it using \pgfmathifthenelse{x}{y}{z} of \ifthenelse but nothing worked for me...

How can I address the two problems?

Dror
  • 22,613

1 Answers1

7

How about decorating the decoration? In this case decorate the lineto code with the markings decoration. I haven't done it with nodes, but you should get the general idea.

\documentclass[border=0.125cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,decorations.markings}

\begin{document}
\begin{tikzpicture}[>=stealth,
  node at every point/.style={%
    decoration={%
      show path construction,
      lineto code={%
        \path [decoration={markings,
          mark=at position 0 with {\fill circle [radius=1pt];},
          mark=at position .5 with {\arrow{>};},
          mark=at position 1 with {\fill circle [radius=1pt];},
        }, decorate] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
      },
    },
    postaction=decorate
  }
  ]
  \draw [node at every point](0,0) -- (1,2) -- (2.3,0) -- (2.5,1);
\end{tikzpicture}

\end{document}

enter image description here

Dror
  • 22,613
Mark Wibrow
  • 70,437