2

I need to label the edges and the loop on this graph, but I'm having some trouble.

\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}

Thanks for the help.

  • You can add nodes after to or to [<with options>], for example: \draw[bend left] (n2) to node {foo} (n3); – Qrrbrbirlbel Aug 28 '13 at 17:59
  • Welcome to TeX.SX! You can have a look on our starter guide to familiarize yourself further with our format. – Claudio Fiandrino Aug 28 '13 at 18:16
3

You can add nodes along the path if you pass them between to (after possible options) and the following coordinates. By default, the midway position is used which you can change by using the pos option or one of the following short-cuts (which correspond to the position in parentheses):

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    )

These options and their use are explained in the TikZ/PGF manual in section 16.8 “Placing Nodes on a Line or Curve Explicitly”, pp. 190ff., and the following section 16.9 “Placing Nodes on a Line or Curve Implicitly”, pp. 193f.

They also use the every node style so to avoid that they use the same style as your main nodes, do

\begin{scope}[every node/.style={…}]
  \node (n1) … {$x_1$};
  \node (n2) … {$x_2$};
\end{scope}

\draw (n1) to node {$t_{1,2}$} (n2);

Though, in your case I would specify the nodes and their position differently, namely with polar coordinates.

Code

\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}

Output

enter image description here

|improve this answer|||||
1

I realize you are likely looking for an answer within the context of tikz, but there are non-tikz approaches that, for certain types of problems, may have advantages.

\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}

enter image description here

|improve this answer|||||

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.