[Tex/LaTex] The use of below/above right/left of node in Tikz

positioningtikz-nodetikz-pgf

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

Best Answer

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