8

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?

Werner
  • 603,163

2 Answers2

10

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}
Jake
  • 232,450
  • 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
  • Ah, this is what I was talking about! There is always a magic trick out there! – MindV0rtex Feb 20 '12 at 13:56
3

You can also to do this :

enter image description here

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} 
Alain Matthes
  • 95,075