1

I am currently drawing polylines with desired style, but I was not able to change labels for them. Instead of showing "1" everywhere I want to put a,b,c or whatever there for each line while keeping it simple and automated.

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,decorations.markings}

\begin{tikzpicture}[>=stealth,
node at every point/.style={%
    decoration={%
        show path construction,
        lineto code={%
            \path [decoration={markings,
                mark=at position 0 with {\fill circle [radius=1pt];},
                mark=at position .5 with {\arrow{>};},
                mark=at position .5 with {\node[shape=circle,draw,swap,auto,inner sep=2pt] at (0,0.4) {1};},
                mark=at position 1 with {\fill circle [radius=1pt];},
            }, decorate] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
        },
    },
    postaction=decorate
}
]
\draw [node at every point](0,0) -- (1,2) -- (2.3,0) -- (2.5,1);
\end{tikzpicture}

Polyline with labels

I also tried with \foreach, but wasnt able to come to any solution. Ideal solution would be adding third parameter for the nodes for example (0,0) -- (1,2,first line label) -- (2.3,0,second line label) etc.

  • 1
    Please make your code compilable. And I think your question has already an answer here. – marmot Dec 9 '18 at 14:28
  • @marmot I have added tikz options to make it compilable. The answer you've recommended works great for numbering 1,2,3...n, but unfortunately no custom labels. Thank you very much anyway! I'll definitely use it sometime. – Jan Vorisek Dec 9 '18 at 14:36
1

OK, here we go then. It is very similar to this answer except that I use \alph to convert the number into a,b... And I'd like to note that auto and swap have no effect in your code. They would have if you would not use markings to draw the circled letter.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,decorations.markings}
\newcounter{coordinateindex}
\begin{document}
\begin{tikzpicture}[>=stealth,initialize counter/.code={
            \setcounter{coordinateindex}{0}
        },
node at every point/.style={%
    decoration={show path construction,
        lineto code={%
            \path [decoration={markings,
                mark=at position 0 with {\fill circle [radius=1pt];},
                mark=at position .5 with {\arrow{>};},
                mark=at position .5 with {\stepcounter{coordinateindex}
                \node[shape=circle,draw,inner sep=2pt] at (0,0.4) 
                {\alph{coordinateindex}};},
                mark=at position 1 with {\fill circle [radius=1pt];},
            }, decorate] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
        },
     },
    postaction=decorate
}
]
\draw [node at every point] (0,0) -- (1,2) -- (2.3,0) -- (2.5,1);
\end{tikzpicture}
\end{document}

enter image description here

If you want to use auto and swap, here's a suggestion.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,decorations.markings}
\newcounter{coordinateindex}
\begin{document}
\begin{tikzpicture}[>=stealth,initialize counter/.code={
            \setcounter{coordinateindex}{0}
        },
node at every point/.style={%
    decoration={show path construction,
        lineto code={%
            \path [decoration={markings,
                mark=at position 0 with {\fill circle [radius=1pt];},
                mark=at position .5 with {\arrow{>};},
                mark=at position 1 with {\fill circle [radius=1pt];},
            }, decorate] (\tikzinputsegmentfirst) --
            node[pos=0.5,auto,swap,shape=circle,draw,inner sep=2pt] 
                {\stepcounter{coordinateindex}\vphantom{bg}\alph{coordinateindex}} (\tikzinputsegmentlast);
        },
     },
    postaction=decorate
}
]
\draw [node at every point] (0,0) -- (1,2) -- (2.3,0) -- (2.5,1);
\end{tikzpicture}
\end{document}

enter image description here

(I would probably drop the swap.)

And here is a way to feed in the elements of a text array. (Note that the first element of the array has index 0, which is why the counter gets stepped later than in the above codes.)

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,decorations.markings}
\newcounter{coordinateindex}
\def\mytexts{{"koala","duck","marmot","mouse","moles","penguin"}}
\begin{document}
\begin{tikzpicture}[>=stealth,initialize counter/.code={
            \setcounter{coordinateindex}{0}
        },
node at every point/.style={%
    decoration={show path construction,
        lineto code={%
            \path [decoration={markings,
                mark=at position 0 with {\fill circle [radius=1pt];},
                mark=at position .5 with {\arrow{>};},
                mark=at position 1 with {\fill circle [radius=1pt];},
            }, decorate] (\tikzinputsegmentfirst) --
            node[midway,above,sloped] 
                {\pgfmathparse{\mytexts[\thecoordinateindex]}
                \pgfmathresult
                \stepcounter{coordinateindex}} (\tikzinputsegmentlast);
        },
     },
    postaction=decorate
}
]
\draw [node at every point] (0,0) -- (1,2) -- (2.3,0) -- (2.5,1);
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you very much! It works well now. The problem is when I would like to use "Some custom text" instead of a,b,c (or 1,2,3). I guess it's not possible without some kind of foreach or further tweaking. I think I'll just tabelize what a,b,c etc. means next to the image as it's not that big deal. – Jan Vorisek Dec 9 '18 at 14:46
  • @JanVorisek I added a way to add texts from an array of texts. – marmot Dec 9 '18 at 15:01
  • Thank you for your time. I'll try to modify my question to state more clearly I needed exactly this! – Jan Vorisek Dec 9 '18 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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