# Arrowheads every two points in Tikz

I have a file data.dat with the following lines:

-1.200000     0.000000
-1.200000    -0.728000
-0.728000    -0.728000
-0.728000     0.614172
0.614172     0.614172
0.614172     1.231670


which stores a set of points with x and y coordinates. I subsequently want to plot this list of points with arrowheads every two points (or every point, that would be fine as well) as follows (but with arrowheads instead of mark=*):

\begin{document}
\begin{tikzpicture}
\draw[mark=*,mark indices={2,4,...,8}] plot file {data.dat};
\end{tikzpicture}
\end{document}


It looks like there is not mark which looks like arrowhead: would a better solution be the use of a foreach loop that reads in the data file?

-
What would be the direction of the arrow heads? Following the path? With the decorations library you can try something like \draw[decoration={markings,mark=between positions 0 and 1 step 0.728cm with {\arrow{stealth}}},postaction={decorate}] plot file {data.dat}; –  percusse Jan 20 '13 at 23:06
yes, the direction would be following the path. This is probably why mark= is not an option. I thought about the decoration approach but the steps are not of equal size. –  pluton Jan 20 '13 at 23:14
Well they are equally distant in terms path length but not vertically. –  percusse Jan 20 '13 at 23:27
A solution would be to prepare a script that writes the proper tikz code for each segment as follows: \draw[->](point A) -- (point B); –  pluton Jan 21 '13 at 0:20

Here is a suggestion that uses Lua to write multiple \draws, and therefore has to be compiled with lualatex. I borrowed the file reading code from http://stackoverflow.com/questions/16038236/store-in-2d-array-the-contents-of-file-lua

There is a problem with the corners between the arrows because the line is drawn as separate paths, so they're not joined properly. For this specific case, a hack would be to add [shorten >=-0.5\\pgflinewidth] to the draw in the else clause.

\documentclass[border=2mm]{standalone}
\usepackage{tikz,luacode,filecontents}
\begin{filecontents*}{data.dat}
-1.200000     0.000000
-1.200000    -0.728000
-0.728000    -0.728000
-0.728000     0.614172
0.614172     0.614172
0.614172     1.231670
\end{filecontents*}
\begin{document}

\begin{tikzpicture}
\begin{luacode}
local mt = {}          -- create the matrix
for line in io.lines'data.dat' do
local new_row = {}
for n in line:gmatch'%S+' do
table.insert(new_row, tonumber(n))
end
if #new_row > 0 then
table.insert(mt, new_row)
end
endrows = \#mt
for i=1,endrows-1 do
if i % 2 == 1 then
tex.sprint("\\draw [-latex] (", mt[i][1], ",",  mt[i][2] , ") -- (", mt[i+1][1], ",",  mt[i+1][2], ");")
else
tex.sprint("\\draw (", mt[i][1], ",",  mt[i][2] , ") -- (", mt[i+1][1], ",",  mt[i+1][2], ");")
end
end
end
\end{luacode}
\end{tikzpicture}
\begin{tikzpicture}
\draw[mark=*,mark indices={2,4,...,8}] plot file {data.dat};
\end{tikzpicture}
\end{document}

-