# Node at every point of a polyline

Right now I have a code like this:

...
mnode/.style={circle,draw=black,fill=black,inner sep=0pt,minimum size=2pt},
...

\draw (0,0) node[mnode]{}
-- (1,1) node[mnode]{}
-- (2,2) node[mnode]{};


Albeit the real line is much longer. Can I do something to avoid specifying node[mnode]{} after every point?

-

One way to place a node (or any arbitrary TikZ code) at every point is to use the show path construction decoration from the decorations.pathreplacing library. Here's a style node at every point that takes a node option as an argument and places a nodes with the provided option at every point on a path:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}[
mnode/.style={circle,draw=black,fill=black,inner sep=0pt,minimum size=2pt},
node at every point/.style={
decoration={
show path construction,
moveto code={\node at (\tikzinputsegmentfirst) [#1] {};},
lineto code={\node at (\tikzinputsegmentlast) [#1] {};},
curveto code={\node at (\tikzinputsegmentlast) [#1] {};}
},
postaction=decorate
}
]
\draw [node at every point=mnode] (0,0) -- (1,1) -- (2,2) -- (3,2) -- (4,5);
\end{tikzpicture}
\end{document}


Or you could use a \foreach loop for this:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[mnode/.style={circle,draw=black,fill=black,inner sep=0pt,minimum size=2pt}]
\draw (0,0) node[mnode]{}
\foreach \x/\y in {1/1,2/2,3/2,4/5}{
-- (\x,\y) node[mnode]{}
};
\end{tikzpicture}
\end{document}

-
This is not ideal, because I want to reuse my polyline in another part of the code, where no nodes are required. Ideally, I want to save it with \newcommand as a succession of points without nodes and then substitute this command where necessary. I thought there might be some trick to enforce automatic node creation... –  MindV0rtex Feb 17 '12 at 16:12
Well, you could save your polyline in the form 1/1,2/2,3/2,4/5 and if you want to draw it without the nodes just use \draw (0,0) \foreach \x/\y in {1/1,2/2,3/2,4/5}{ -- (\x,\y)}; –  Jake Feb 17 '12 at 16:16
No magic then! But this is good enough. –  MindV0rtex Feb 17 '12 at 16:30
@MindV0rtex: I was wrong, there is a magic way. I've edited my answer. –  Jake Feb 17 '12 at 16:39
Interesting solution as usual ! –  Alain Matthes Feb 17 '12 at 17:06

You can also to do this :

With this code :

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[mnode/.style={circle,draw=black,fill=black,inner sep=0pt,minimum size=2pt},
to path={ -- (\tikztotarget) node{} \tikztonodes},every node/.style={mnode}]

\draw[fill=orange!20] (0,0) node{}  to (1,1)  to (2,2) to (3,1) node [fill=red,minimum size=4pt]{} to (5,4) to (8,0) --cycle;
\end{tikzpicture}

\end{document}

-
Ah yes, very elegant! –  Jake Feb 17 '12 at 17:01
Another bit of knowledge for me to remember. Upvoted. –  MindV0rtex Feb 20 '12 at 13:57