我需要标记此图上的边和环,但遇到了一些麻烦。
\begin{tikzpicture}
[scale=.5,auto=left,every node/.style={circle,fill=blue!20}]
\node (n2) at (0,8) {$x_{2}$};
\node (n1) at (-3,6) {$x_{1}$};
\node (n3) at (3,6) {$x_{3}$};
\node (n4) at (2,2) {$x_{4}$};
\node (n5) at (-2,2) {$x_{5}$};
\foreach \from/\to in {n1/n3,n1/n5,n5/n4,n3/n4,n1/n2}
\draw (\from) -- (\to);
\draw[bend left] (n2) to (n3);
\draw[bend right] (n2) to (n3);
\draw(n4) to [out=160,in=100,distance=3cm] (n4);
\end{tikzpicture}
谢谢您的帮助。
答案1
to
如果将节点传递到以下坐标之间(可能的选项之后),则可以沿路径添加节点。默认情况下,midway
使用位置,您可以使用pos
选项或以下快捷方式之一(对应于pos
括号中的选项)进行更改:
at start (pos = 0 )
very near start (pos = 0.125)
near start (pos = 0.25 )
midway (pos = 0.5 )
near end (pos = 0.75 )
very near end (pos = 0.875)
at end (pos = 1 )
这些选项及其用法在TikZ/PGF 手册在第 16.8 节“将节点明确地放置在直线或曲线上” (第 190 页及后续页) 以及下面的第 16.9 节“将节点隐式地放置在直线或曲线上” (第 193 页及后续页) 中。
它们也使用every node
样式,因此为了避免它们使用与主节点相同的样式,请
\begin{scope}[every node/.style={…}]
\node (n1) … {$x_1$};
\node (n2) … {$x_2$};
\end{scope}
\draw (n1) to node {$t_{1,2}$} (n2);
不过,在您的情况下,我会以不同的方式指定节点及其位置,即使用极坐标。
代码
\documentclass[tikz,convert=false]{standalone}
\begin{document}
\begin{tikzpicture}[auto=right]
\foreach \cnt in {1,...,5}
\node[circle, outer sep=+0pt, fill=blue!20] (n\cnt)
at (3.25*360/5-\cnt*360/5:2) {$x_\cnt$};
\path [every node/.style={font=\scriptsize, inner sep=+.1667em}]
(n1) edge node {$t_{1,3}$} (n3)
edge node {$t_{1,5}$} (n5)
edge node[swap] {$t_{1,2}$} (n2)
(n4) edge node {$t_{3,4}$} (n3)
edge[out=160, in=100, distance=1.6cm]
node[swap] {$t_{4}$} (n4)
edge node[swap] {$t_{4,5}$} (n5)
(n2) edge[bend left] node[swap] {$t_{2,3}$} (n3)
edge[bend right] node[swap] {$t_{3,2}$} (n3);
\end{tikzpicture}
\end{document}
输出
答案2
我知道您可能正在寻找的答案,但对于某些类型的问题,tikz
非方法可能具有优势。tikz
\documentclass{article}
\usepackage{tikz}
\usepackage{stackengine}
\begin{document}
\def\stackalignment{l}
\newsavebox{\mypic}
\sbox\mypic{
\begin{tikzpicture}
[scale=.5,auto=left,every node/.style={circle,fill=blue!20}]
\node (n2) at (0,8) {$x_{2}$};
\node (n1) at (-3,6) {$x_{1}$};
\node (n3) at (3,6) {$x_{3}$};
\node (n4) at (2,2) {$x_{4}$};
\node (n5) at (-2,2) {$x_{5}$};
\foreach \from/\to in {n1/n3,n1/n5,n5/n4,n3/n4,n1/n2}
\draw (\from) -- (\to);
\draw[bend left] (n2) to (n3);
\draw[bend right] (n2) to (n3);
\draw(n4) to [out=160,in=100,distance=3cm] (n4);
\end{tikzpicture}
}
\bottominset{$s_{12}$}{%
\bottominset{$s_{23}$}{%
\bottominset{$s_{34}$}{%
\bottominset{$s_{45}$}{%
\bottominset{$s_{51}$}{%
\bottominset{loop}{%
\usebox{\mypic}%
}{1.6cm}{1.8cm}
}{1.2cm}{0.3cm}
}{0.1cm}{1.7cm}
}{1.4cm}{3.3cm}
}{3.2cm}{3.0cm}
}{3.0cm}{0.8cm}
\end{document}