4

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}

enter image description here

  • 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
6

If you set the nodes above and below points on the path, then they will not interfere with the continuity of the path. For example:

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \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$};
\end{tikzpicture}
\end{document}

continuous path

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.