TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I create a TikZ/pgf-style that will put a coordinate, which should be named automatically, at each point of a path?

So the following code

\draw[put coordinates](0,0)--(3,2)--(4,7)--(-2,1);

Should be essentially the same as:

\coordinate(mypathcoordinate1) at (0,0);
\coordinate(mypathcoordinate2) at (3,2);
\coordinate(mypathcoordinate3) at (4,7);
\coordinate(mypathcoordinate4) at (-2,1);
share|improve this question
Should this work for arbitrary paths (with curves and such), and do the paths have to remain closed? – Jake Jun 28 '13 at 12:40
Generally, the paths should be arbitrary. The coordinates should only lie at the explicitly given points, regardless of the way they are connected (line,move,curve,to,…). Acutely, I only work with -- or to and the paths are closed. – Toscho Jun 28 '13 at 13:56
up vote 7 down vote accepted

You can use a show path construction decoration that you apply using a postaction to place the coordinates. That way, the underlying path is not changed at all.

Here's a style put coordinates that takes an optional argument to name the coordinates (default is coordinate):


    put coordinates/.style={
        initialize counter/.code={
        initialize counter,
            show path construction,
            moveto code={
                \coordinate (#1\thecoordinateindex) at (\tikzinputsegmentfirst);
            lineto code={
                \coordinate (#1\thecoordinateindex) at (\tikzinputsegmentlast);
            curveto code={
                \coordinate (#1\thecoordinateindex) at (\tikzinputsegmentlast);
            closepath code={
                \coordinate (#1\thecoordinateindex) at (\tikzinputsegmentlast);
    put coordinates/.default=coordinate

\draw [put coordinates] (0,0) -- (3,2) to [out=90, in=0] (4,7)-- (-2,1);

\foreach \i in {1,...,\thecoordinateindex}{
    \fill (coordinate\i) circle [radius=2pt] node [above left] {\i};

\draw [red, put coordinates=secondpath] (0,1) -- (5,4) -- (4,1);
\foreach \i in {1,...,\thecoordinateindex}{
    \fill (secondpath\i) circle [radius=2pt] node [above left] {\i};

share|improve this answer
+1 Clever! . . . – JLDiaz Jun 28 '13 at 14:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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