5

I'd like to create a dashed line in TikZ with an arrow head at the end of each dash. The following code accomplishes this using a \foreach loop but looking for a way to do this with a style key instead. Something similar to

\draw [densely dashed, ->] (0,0) -- ++(100pt,0);

but places arrowheads on all dashes not just the end.

MWE:

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \draw [densely dashed] (0,0) -- ++(100pt,0);
    \foreach \i in {0,5,10,...,100}{%
        \draw [->] (\i*1pt,-0.25) -- ++(3pt,0);
    }
\end{tikzpicture}
\end{document}
7

I guess for this one of the simplest possibilities will be to employ decorations.markings.

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}
    \draw [dash pattern=on 3pt off 2pt,postaction={decorate,
    decoration={markings,
    mark=between positions 3pt and 1 step 5pt with {\arrow{>};}}}] (0,0) -- ++(100pt,0);
\end{tikzpicture}
\end{document}

enter image description here

Depending on the real application, one could make this a style, or, what might be better for curved paths, just declare a new (meta) decoration. Luckily, the pgfmanual has on p. 1007 a meta decoration that one only has to slightly modify to arrive at

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations,arrows.meta}
\pgfdeclaremetadecoration{many arrows}{initial}{ 
\state{initial}[width=0pt, next state=arrow] {
    \pgfmathdivide{100}{\pgfmetadecoratedpathlength}
    \let\factor\pgfmathresult
    %\pgfsetlinewidth{1pt}
    \pgfset{/pgf/decoration/segment length=4pt}
  }
  \state{arrow}[
switch if less than=\pgfmetadecorationsegmentlength to final, width=\pgfmetadecorationsegmentlength/3,
next state=end arrow]
  {
    \decoration{curveto}
    \beforedecoration
    {
      \pgfpathmoveto{\pgfpointmetadecoratedpathfirst}
} }
\state{end arrow}[width=\pgfmetadecorationsegmentlength/3, next state=move] {
    \decoration{curveto}
    \beforedecoration{\pgfpathmoveto{\pgfpointmetadecoratedpathfirst}}
    \afterdecoration
    {
      \pgfsetarrowsend{Latex[length=1pt,width=1pt]}
      \pgfusepath{stroke}
    }
}
\state{move}[width=\pgfmetadecorationsegmentlength/2, next state=arrow]{} \state{final}{}
}
\begin{document}
\begin{tikzpicture}
\draw[ultra thin,decorate,decoration={many arrows,meta-segment length=3pt}] (0,0) .. controls (0,2) and (3,2) .. (3,0)
        .. controls (3,-2)  and (0,-2)  .. (0,-4)
        .. controls (0,-6)  and (3,-6)  .. (3,-8)
        .. controls (3,-10) and (0,-10) .. (0,-8);
\end{tikzpicture}       
\end{document}      

Here I zoom into the outcome to show that the arrows are tiny. (Recall that the diameter is 3cm.)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.