[Tex/LaTex] Shortcut for arrows on curves in TikZ

arrowstikz-pgf

If I want to draw a curve with an arrow somewhere along its length (say 40% along), I can write this:

\draw [decoration={
             markings,
             mark=at position 0.4 with \arrow{>}}
       ,postaction=decorate]
       (A) to (B);

If I want to draw lots of curves with the same decoration, I can put most of this in a scope and reduce how much I have to type for each path.

However, if I want to draw lots of curves with different parameters – say, each one needs to have the arrow at a different position, and the choice of < or > could vary between curves. Then it seems I have a lot of typing to do for each curve.

What I'd like to do is define something like this:

\newcommand\arrowdata[2]{decoration={
             markings,
             mark=at position #1 with \arrow{#2}}
       ,postaction=decorate}

Then I can draw my arrows with ease:

\draw [\arrowdata{0.3}{>}] (A) to (B);
\draw [\arrowdata{0.95}{<}] (C) to (D) to (E);

and so on. But, this doesn't work, and I don't understand the error messages that result. Clearly I'm being too naive in my understanding of how macros apply in TikZ. Can someone help me out?

Best Answer

Macros in style definitions don't work if they aren't expanded beforehand. You should define your own style instead.

\tikzset{arrow data/.style 2 args={%
      decoration={%
         markings,
         mark=at position #1 with \arrow{#2}},
         postaction=decorate}
      }%
}

The use it like:

\draw [arrow data={0.3}{>}] (A) to (B);