# Draw connected line segments using tikz

In the following example, I want the line segments to be connected at each vertex. For example, I want E1 and E2 to have a common end point and label V2 should be used to label that end point. How should I modify the code? Thanks.

\documentclass[]{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\node at (0,0) (nodeA) {$V_1$};
\node at (2,2) (nodeB) {$V_2$};
\node at (3,0) (nodeC) {$V_3$};
\node at (5,1) (nodeD) {$V_4$};

\draw (nodeA) -- (nodeB) -- (nodeC) -- (nodeD);

\draw (nodeA) -- (nodeB) node [midway, above, sloped] (EdgeAB) {$E_1$};
\draw (nodeB) -- (nodeC) node [midway, above, sloped] (EdgeBC) {$E_2$};
\draw (nodeC) -- (nodeD) node [midway, above, sloped] (EdgeCD) {$E_3$};
\end{tikzpicture}

\end{document}


• Nodes have various anchors that you can use. You could replace (nodeB) by (nodeB.south) and (nodeC) by (nodeC.north). – Tom Bombadil Nov 23 '15 at 8:18

\documentclass[tikz,border=10pt]{standalone}
\draw (0,0) node (nodeA) [below]  {$V_1$} -- (2,2) node (nodeB) [above] {$V_2$} node [midway, above, sloped] (EdgeAB) {$E_1$} -- (3,0) node [below] (nodeC) {$V_3$} node [midway, above, sloped] (EdgeBC) {$E_2$} -- (5,1) node [above] (nodeD) {$V_4$} node [midway, above, sloped] (EdgeCD) {$E_3$};