在以下示例中,我希望线段在每个顶点处连接。例如,我希望 E1 和 E2 有一个共同的端点,并且应该使用标签 V2 来标记该端点。我应该如何修改代码?谢谢。
\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}
答案1
如果将节点设置在路径上的点上方和下方,则它们不会干扰路径的连续性。例如:
\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}