6

In an answer on this site is proposed a method to draw arrows on paths. For example, using the style [arrow inside={pos=0.2}] will put an arrow at position 0.2 on the path :

\begin{tikzpicture}
\tikzset{
  set arrow inside/.code={\pgfqkeys{/tikz/arrow inside}{#1}},
  set arrow inside={pos/.initial=.5, end/.initial=>},
  arrow inside/.style={
    set arrow inside={#1},
    postaction={
      decorate,
      decoration={
        markings,
        mark=at position \pgfkeysvalueof{/tikz/arrow inside/pos} with \arrow{\pgfkeysvalueof{/tikz/arrow inside/end}}
      }
    }
  },
}

\draw[arrow inside={pos=0.2}] (0,0) -- (5cm,5cm);
\end{tikzpicture}

Now, I would like to do something like

\draw[arrow inside={pos=0.2}, arrow inside={pos=0.8}] (0,0) -- (5cm,5cm);

and end up with two arrows, one at position 0.2 and the other at position 0.8. This does not work, because the second call to set arrow inside modifies the value of the key /tikz/arrow inside/pos and hence the style (only the second arrow is drawn). I tried to play with expansion rules, but I fail to wrap my mind around these things, so I hope perhaps somebody else will manage to do so (or perhaps the problem is somewhere else, and then I hope somebody will point out where !).

For the record, the original answer also provides another way to circumvent this problem by modifying the syntax a little, and that solution may be useful in practice. (However, I for one am interested in a solution to the evaluation order problem.) Thanks in advance !

2
  • Last setting always win. So, IMHO is not that easy to keep the same syntax as you're going to exploit the same keys several times. Sep 12, 2014 at 8:23
  • Yes, it is tricky. What I believe I need to do is to first parse the pgfkeys arguments to set the relevant macros (or pgf keys values), and then to produce an expanded style and to use it. Then, the contents of the macros/keys can change without affecting the style (because they were expanded). However, I don't know how to do that, hence my question. Sep 12, 2014 at 22:45

2 Answers 2

3

Do you mean some thing like this?

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset{
  arrowmark/.style 2 args={postaction={decorate},decoration={markings,mark=at position #1 with \arrow{#2}}}
}

\begin{document}
\begin{tikzpicture}
\draw[arrowmark={.2}{<},arrowmark={.8}{>}] (0,0) -- (5cm,5cm);
\end{tikzpicture}
\end{document}

enter image description here

1
  • Ah, no. I want to keep the syntax arrow inside={pos=0.2} and to be able to use several of them. I know there are several other options ! However, I am interested in this specific problem :) Thanks anyway ! Sep 12, 2014 at 8:08
1

Few years later ... you can use a nested macros for that.

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset{
  arrow inside/.style={
    decoration={markings},
    end/.style={
      pos/.style={
        decoration={
          mark=at position ####1 with {\expandafter\arrow##1}
        }
      }
    },
    end=>,
    #1,
    postaction={decorate}
  }
}

\begin{document}
  \begin{tikzpicture}
    \draw[arrow inside={pos=0.2},arrow inside={end={[red,scale=3]{latex}},pos=0.7}] (0,0) -- (5cm,5cm);
  \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 .