11

Sometimes, I want to have arrow tips on a path. The full syntax for that would be:

postaction=decorate,decoration={markings,mark={between positions <1> and <2> step <3> with {\arrow{latex}}}}

where by <1> <2> <3> I denote the parameters of this decoration, which must be two numbers between 0 and 1 and a length. For now, I have:

\tikzset{repmidarrow/.style={postaction=decorate,decoration={markings,mark={between positions 0.05 and 1 step #1 with {\arrow{latex}}}}}}

Unfortunately, this does not allow me to specify anything but <3>, which is the reason for the specified <1> and <2>, which were a specific case in a picture I just drew. In the future, should I want different positions, I would have to either design another key or place the full syntax in the picture code, which makes the code far messier. Unfortunately, TikZ keys can, AFAIK, only have one parameter. I was thinking of using auxiliary keys to give the extra parameters, and would like to get to something that, with:

\draw[repmidarrow={from=<1>,to=<2>,step=<3>}] <path>;

gives:

\draw[postaction=decorate,decoration={markings,mark={between positions <1> and <2> step <3> with {\arrow{latex}}}} <path>;

Is there a way to achieve this? PS I hope my braces above are well-matched.

The picture I referred to above is:

\begin{tikzpicture}
\draw[repmidarrow=25pt] (-2,0) -- (2,0) -- (0,2) -- cycle;
\clip (-2,0) -- (2,0) -- (0,2) -- cycle;
\draw[repmidarrow=25pt] (-.02,0) -- (-1.02,.98);
\draw[repmidarrow=25pt] (.98,.98) -- (.02,0);
\draw[repmidarrow=25pt] (-.98,1.02) -- (1.02,1.02);
\draw[repmidarrow=25pt,red] (0,0) -- (1,1) -- (-1,1) -- cycle;
\end{tikzpicture}

compiled with the above \tikzset and the TikZ package of course. Result:

enter image description here

MickG
  • 5,426

1 Answers1

24

As you are almost there. For having more than one arguments you can either use the explicit style n args handler for supplying all arguments back to back inside braces, or you can cook up your own style with style args (same goes for the code handler):

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset{
  repmidarrownargs/.style n args={3}{
    postaction=decorate,
    decoration={
      markings,
      mark={between positions #1 and #2 step #3 with \arrow{latex}}
    }
  },
  repmidarrowargs/.style args={#1and#2with step#3}{% Better to keep the spaces minimal
    postaction=decorate,
    decoration={
      markings,
      mark={between positions #1 and #2 step #3 with \arrow{latex}}
    }
  }
}
\begin{document}
\begin{tikzpicture}
\draw[style=help lines] (-1,-1) grid[step=1cm] (2,2);
\draw[repmidarrownargs={0.5}{1}{25pt}] (-.02,0) -- (-1.02,.98); % N args
\draw[repmidarrowargs=0.1 and 0.9 with step 5pt] (.98,.98) -- (.02,0);% args
\end{tikzpicture}
\end{document}

enter image description here

percusse
  • 157,807
  • 3
    Thanks for this. You say «Better to keep the spaces minimal». What do you mean? What if I do style args={from #1 to #2 step #3} instead of the same without spaces? – MickG Mar 06 '15 at 16:01
  • 1
    @MickG Depending on how you enter the arguments the styles can be sensitive to whitespaces and TikZ might not match the pattern robustly. Just a precaution not a deal breaker though. – percusse Mar 06 '15 at 16:02
  • Sorry to ask in a such an old question, but are the arguments mandatory in the first \tikzset definition? I'm getting an error in my custom implementation that expects two arguments. The error is gone if I pass empty arguments, e.g., foo={}{} – José Joaquim Mar 30 '22 at 01:27