8

In the figure below there are two nodes, s and e, I try to draw them in the middle of two node but above or below. I tried to methods above right of=1, xshift=-1.2cm and below right =3.4cm and 2.4cm of 2 none of which looks totally satisfactuary. I am sure that there is a better way than trial and error that I used.

\documentclass[border=1cm]{standalone}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows,fit,automata}
\usetikzlibrary{positioning}


\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=6cm,
    semithick]
\tikzstyle{every state}=[circle, black, draw=red, very thick]

\node[state]          (1)                         {$1$};

\node[state]         (2) [below of=1,yshift=1cm]  {$2$};
\node[state]         (3) [right of=1]             {$3$};
\node[state]         (4) [below of=3,yshift=1cm]  {$4$};

\node[state]         (s) [above right of=1, xshift=-1.2cm]  {$s$};
\node[state]         (e) [below right =3.4cm and 2.4cm of 2] {$e$};



\path   (1) edge node [sloped,above]            {6, 6}  (2)
        (4) edge node[sloped,above,near start]  {2, 2}  (1)
        (1) edge node[sloped,above]             {7, 4}  (3)
                                                

        (2) edge node[sloped,above ]            {5, 6}  (4)
        
        (3) edge node[sloped,above,near start]  {4, 4}  (2)                       
        (3) edge node [sloped,above]            {3, 6}  (4)

        (s) edge            node[sloped,above]  {8, 0} (1)
        (s) edge            node[sloped,above]  {6, 0} (3)
        (2) edge            node[sloped,above]  {4, 0} (e)
        (4) edge            node[sloped,above]  {9, 0} (e);
        
\end{tikzpicture}
\end{document}

enter image description here

2
  • I feel you should use ++(coordinate)? (although there are some parts of the code that I'm not particularly familiar with) Section 2.15, specifying coordinates, TikZ manual.
    – user202729
    Commented Nov 2, 2021 at 17:37
  • I have the feeling that this is rather a matter of taste. Where do you want, for instance, node e to be positioned in relation to where it is right now? A bit more upwards? Should, for example, the angle of the arrows pointing to node e be the same as those of the diagonal arrows of the inner four nodes? Commented Nov 2, 2021 at 18:13

3 Answers 3

11

See, if the following modification of your MWE gives what you after. Main changes are:

  • use of calc library
  • use of the positioning syntax
  • use of on grid option
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata,
                calc,
                positioning,
                quotes}


\begin{document}
    \begin{tikzpicture}[>=Stealth, auto,
     node distance = 2*\dis, on grid,
every state/.style = {circle, draw=red, very thick, 
                      minimum size=2em, inner sep=0pt},
 every edge/.style = {draw, semithick, -Stealth, shorten >=1pt},
 every edge quotes/.style = {sloped, font=\small}
                    ]
\def\dis{3cm}
    \begin{scope}[nodes=state]
\node   (n1)                {$1$};
\node   (n2) [below=of n1]  {$2$};
\node   (n3) [right=of n1]  {$3$};
\node   (n4) [below=of n3]  {$4$};
\node   (s)  [above=\dis of $(n1)!0.5!(n3)$] {$s$};
\node   (e)  [below=\dis of $(n2)!0.5!(n4)$] {$e$};
    \end{scope}
\path   (n1) edge ["{6, 6}"]    (n2)
        (n1) edge ["{7, 4}"]    (n3)
        
        (n2) edge ["{5, 6}"]    (n4)
        (n2) edge ["{4, 0}"]    (e)
        
        (n3) edge [near start,"{4, 4}"] (n2)
        (n3) edge ["{3, 6}"]    (n4)
        (n4) edge [near start,"{4, 4}"] (n1)
        (n4) edge ["{9, 0}"]    (e)

        (s)  edge ["{8, 0}"]    (n1)
        (s)  edge ["{6, 0}"]    (n3);
\end{tikzpicture}
\end{document}

enter image description here

2
  • 2
    Nice! +1 (I will upvote tomorrow when I can vote again.) Commented Nov 2, 2021 at 18:28
  • Very nice and elegant! Thanks! Commented Nov 2, 2021 at 19:11
6

What about using a TikZ matrix?

\documentclass[border=1cm]{standalone}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata, matrix}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[->, >=Stealth, shorten >=1pt, semithick,
    every state/.style={circle, black, draw=red, very thick},
    descr/.style={sloped,above}]
\matrix[matrix of nodes, nodes={state},
    column sep =2cm, row sep =4cm] 
    {&|[name=s]|$s$\\
    |[name=1]|$1$&&|[name=3]|$3$\\
    |[name=2]|$2$&&|[name=4]|$4$\\
    &|[name=e]|$e$\\};     
\path (1) edge node[descr] {6, 6} (2)
      (4) edge node[descr,near start] {2, 2} (1)
      (1) edge node[descr] {7, 4} (3)
      (2) edge node[descr] {5, 6} (4)
      (3) edge node[descr,near start] {4, 4} (2)           
      (3) edge node[descr] {3, 6} (4)
      (s) edge node[descr] {8, 0} (1)
      (s) edge node[descr] {6, 0} (3)
      (2) edge node[descr] {4, 0} (e)
      (4) edge node[descr] {9, 0} (e);
\end{tikzpicture}
\end{document}

enter image description here

You can also choose a different row sep for some rows:

\documentclass[border=1cm]{standalone}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata, matrix}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[->, >=Stealth, shorten >=1pt, semithick,
    every state/.style={circle, black, draw=red, very thick},
    descr/.style={sloped,above}]
\matrix[matrix of nodes, nodes={state},
    column sep =2cm, row sep =4cm] 
    {&|[name=s]|$s$\\[-2cm]
    |[name=1]|$1$&&|[name=3]|$3$\\
    |[name=2]|$2$&&|[name=4]|$4$\\[-2cm]
    &|[name=e]|$e$\\};     
\path (1) edge node[descr] {6, 6} (2)
      (4) edge node[descr,near start] {2, 2} (1)
      (1) edge node[descr] {7, 4} (3)
      (2) edge node[descr] {5, 6} (4)
      (3) edge node[descr,near start] {4, 4} (2)           
      (3) edge node[descr] {3, 6} (4)
      (s) edge node[descr] {8, 0} (1)
      (s) edge node[descr] {6, 0} (3)
      (2) edge node[descr] {4, 0} (e)
      (4) edge node[descr] {9, 0} (e);
\end{tikzpicture}
\end{document}

enter image description here

4

An alternative to Zarko's answer, that still using the calc library to position nodes exactly between to other nodes (since this, I assume, is the main issue here).

I have left as much of your code as possible as it was and only added some spaces and removed libraries and packages that are not needed to produce this example.

I also removed the \tikzstyle command and incorporated its options into the other options, since this seems to be considered the current standard.

\documentclass[border=1cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{arrows, automata, calc}

\begin{document}

\begin{tikzpicture}[->, >=stealth', shorten >=1pt, node distance=6cm, semithick,
    every state/.style={circle, black, draw=red, very thick}]

\node[state] (1)                                 {$1$};
 
\node[state] (2) [below of=1, yshift=1cm]        {$2$};
\node[state] (3) [right of=1]                    {$3$};
\node[state] (4) [below of=3, yshift=1cm]        {$4$};

\node[state] (s) at ([yshift=3cm]$(1)!0.5!(3)$)  {$s$};
\node[state] (e) at ([yshift=-3cm]$(2)!0.5!(4)$) {$e$};

\path (1) edge node[sloped, above]             {6, 6} (2)
      (4) edge node[sloped, above, near start] {2, 2} (1)
      (1) edge node[sloped, above]             {7, 4} (3)

      (2) edge node[sloped, above]             {5, 6} (4)
    
      (3) edge node[sloped, above, near start] {4, 4} (2)                       
      (3) edge node[sloped, above]             {3, 6} (4)

      (s) edge node[sloped, above]             {8, 0} (1)
      (s) edge node[sloped, above]             {6, 0} (3)
      (2) edge node[sloped, above]             {4, 0} (e)
      (4) edge node[sloped, above]             {9, 0} (e);
        
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .