I would like to create a gif (as explained in this answer) of a point moving along a crooked "multi-node path". I would like this path to be specified as below: a sequence of nodes connected by edges. In the iteration I would like to specify nodes along this "multi-node path" as in node[pos=\n, above] {x}.
I have a solution but it's not easily scalable: if I want more nodes along the path I have to change it by hand. Also there is a problem with the window shifting up and down as the point moves along the path.
I could do this by having as many for loops as there are edges, and placing the node[pos=\n, above] {x} on the relevant edge every time, but I'd like to have a more generic solution where I don't have to know in advance how long the "multi-node path" will be, something like the following (which doesn't work):
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\foreach \n in {0,0.05,...,1} {
\begin{tikzpicture}
\draw[name path=mypath]
(0,0) -- ++ (1,2)
-- ++ (1,.5)
-- ++ (1,-1)
-- ++ (1,0)
-- ++ (1,-4);
\draw (mypath) node [draw=black, fill=red, circle] {}
node [pos=\n, above] {x};
\end{tikzpicture}
}
\end{document}
My current solution is ugly :( :
\documentclass[tikz, border=.5cm]{standalone}
\usepackage{tikz}
\begin{document}
\foreach \n in {0,0.1,...,.9} {
\begin{tikzpicture}
\draw
(0,0) -- node [circle, fill=red, draw=black, pos=\n, inner sep = 0, minimum size = 4pt] {} node [pos=\n, above] {x} ++ (1,2)
-- ++ (1,.5)
-- ++ (1,-1)
-- ++ (1,0)
-- ++ (1,-4);
\end{tikzpicture}
}
\foreach \n in {0,0.1,...,.9} {
\begin{tikzpicture}
\draw
(0,0) -- ++ (1,2)
-- node [circle, fill=red, draw=black, pos=\n, inner sep = 0, minimum size = 4pt] {} node [pos=\n, above] {x} ++ (1,.5)
-- ++ (1,-1)
-- ++ (1,0)
-- ++ (1,-4);
\end{tikzpicture}
}
\foreach \n in {0,0.1,...,.9} {
\begin{tikzpicture}
\draw
(0,0) -- ++ (1,2)
-- ++ (1,.5)
-- node [circle, fill=red, draw=black, pos=\n, inner sep = 0, minimum size = 4pt] {} node [pos=\n, above] {x} ++ (1,-1)
-- ++ (1,0)
-- ++ (1,-4);
\end{tikzpicture}
}
\foreach \n in {0,0.1,...,.9} {
\begin{tikzpicture}
\draw
(0,0) -- ++ (1,2)
-- ++ (1,.5)
-- ++ (1,-1)
-- node [circle, fill=red, draw=black, pos=\n, inner sep = 0, minimum size = 4pt] {} node [pos=\n, above] {x} ++ (1,0)
-- ++ (1,-4);
\end{tikzpicture}
}
\foreach \n in {0,0.1,...,.9} {
\begin{tikzpicture}
\draw
(0,0) -- ++ (1,2)
-- ++ (1,.5)
-- ++ (1,-1)
-- ++ (1,0)
-- node [circle, fill=red, draw=black, pos=\n, inner sep = 0, minimum size = 4pt] {} node [pos=\n, above] {x} ++ (1,-4);
\end{tikzpicture}
}
\begin{tikzpicture}
\draw
(0,0) -- ++ (1,2)
-- ++ (1,.5)
-- ++ (1,-1)
-- ++ (1,0)
-- node [circle, fill=red, draw=black, pos=1, inner sep = 0, minimum size = 4pt] {} node [pos=1, above] {x} ++ (1,-4);
\end{tikzpicture}
\end{document}


