[Tex/LaTex] change position of edge label and caption

graphicspositioningtikz-pgf

I have create the following graph

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,automata,positioning} 
\newcommand{\N}{\mathbb{N}} % natuerliche Zahlen
    \newcommand{\Z}{\mathbb{Z}} % ganze Zahlen
    \newcommand{\Q}{\mathbb{Q}} % rationale Zahlen
    \newcommand{\R}{\mathbb{R}} % reelle Zahlen
    \newcommand{\Prob}{\mathbb{P}} % Symbol fuer Wahrscheinlichkeitsma├č
    \newcommand{\E}{\mathbb{E}} % Symbol fuer Erwartungswert
    \newcommand{\QAt}{\mathbf{Q}_{A_{2}A_{2}}}
    \newcommand{\QAo}{\mathbf{Q}_{A_{1}A_{1}}}
    \newcommand{\QAot}{\mathbf{Q}_{A_{1}A_{2}}}
    \newcommand{\QAto}{\mathbf{Q}_{A_{2}A_{1}}}
    \newcommand{\QAtC}{\mathbf{Q}_{A_{2}C}}
    \newcommand{\QAtw}{\mathbf{Q}_{A_{2}\{\omega\}}}
    \newcommand{\QAow}{\mathbf{Q}_{A_{1}\{\omega\}}}
    \newcommand{\I}{\mathbf{I}}
    \newcommand{\vf}{\mathbf{v}}
    \newcommand{\Df}{\mathbf{D}}
    \newcommand{\af}{\pmb{\alpha}}
    \newcommand{\eef}{\mathbf{e}}
    \newcommand{\Psf}{\pmb{\Psi}}
    \newcommand{\Phf}{\pmb{\Phi}}
    \newcommand{\bef}{\pmb{\beta}}
    \newcommand{\Pf}{\mathbf{P}}
    \newcommand{\Qf}{\mathbf{Q}}
    \newcommand{\mf}{\mathbf{m}}
    \newcommand{\ef}{\pmb{\varepsilon}}
    \newcommand{\Hf}{\mathbf{H}}
    \newcommand{\Ef}{\mathbf{E}}
    \newcommand{\Mf}{\mathbf{M}}
    \newcommand{\Vf}{\mathbf{V}}
    \newcommand{\gf}{\mathbf{g}}
    \newcommand{\QSo}{\mathbf{Q}_{S' S'}}
    \newcommand{\QSot}{\mathbf{Q}_{S' S''}}
    \newcommand{\QSt}{\mathbf{Q}_{S'' S''}}
    \newcommand{\UP}[2]{\makebox[0pt]{\smash{\raisebox{1.5em}{$\phantom{#2}#1$}}}#2}
    \newcommand{\LF}[1]{\makebox[0pt]{$#1$\hspace{4.5em}}}
    \newcommand{\zfo}{\mathbf{z}_{1}}
    \newcommand{\zft}{\mathbf{z}_{2}}
    \newcommand{\zf}{\mathbf{z}}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[->,>=stealth,shorten >=1pt,auto,node distance=2cm,
                    semithick,
 every state/.append style={fill=red,draw=none,text=white,circle},
 every loop/.append style={looseness=5}]
  \path[use as bounding box] (-2.7,0.45) rectangle (15,-10.65);
  \node[state]         (A)              {$A_{2}(0)$};
  \node                (B) [right=of A] {$\{\omega\}$};
  \node[state]         (C) [right=of B] {$A_{1}(0)$};
  \node[state]         (D) [below=of A] {$A_{2}(1)$};
  \node                (E) [right=of D] {$\{\omega\}$};
  \node[state]         (F) [right=of E] {$A_{1}(1)$};
  \node[state]         (G) [below=of D] {$A_{2}$(n{-}2)};
  \node                (H) [right=of G] {$\{\omega\}$};
  \node[state]         (I) [right=of H] {$A_{1}$(n{-}2)};
  \node[state]         (J) [below=of G]  {$A_{2}$(n{-}1)};
  \node                (K) [right=of J] {$\{\omega\}$};
  \node[state]         (L) [right=of K] {$A_{1}$(n{-}1)};



  \path (A) edge              node {$\QAtw$}                        (B)
            edge [left]       node {$\QAt$}                         (D)
            edge              node {$\QAto$}                        (F)
        (C) edge [above]      node {$\QAow$}                        (B)
            edge [above]      node {$\QAot$}                        (D)
            edge [right]      node {$\QAo$}                         (F)
        (D) edge              node {$\QAtw$}                        (E)
                              node(aux) {}                          (G)
        (F) edge [above]      node {$\QAow$}                        (E)
                              node(aux1) {}                         (I)
        (G) edge              node {$\QAtw$}                        (H)
            edge [left]       node {$\QAt$}                         (J)
            edge              node {$\QAto$}                        (L)
        (I) edge [above]      node {$\QAow$}                        (H)
            edge              node {$\QAot$}                        (J)
            edge              node {$\QAo$}                         (L)
        (J) edge [above]      node {$1$}                            (K)
        (L) edge [above]      node {$1$}                            (K);
\node[below=8mm of aux,font=\bfseries\Huge]{\vdots};
\node[below=8mm of aux1,font=\bfseries\Huge]{\vdots};
\end{tikzpicture}
\caption{\"Ubergangsgraph von $X$.}\label{Abbildung4}
\end{figure}
\end{document}

It looks as follows
enter image description here

The proble is that I want to change the position of the edge label in the middle of the two subgraph and also to achieve that the caption is not in the graph. Thanks for any help

it should look as follows
enter image description here

Best Answer

The suggestion to use pos key by @marmot is great, but it won't get you to look exactly how you want. It takes you about 90% of the way. Here is what it looks like using only pos=0.8 (i.e. 80% along the line from the start node to the end node):

Using pos=0.8 in current OP

You'll notice that the labels are not exactly positioned where you want. You have two options (or both) to deal with this. The easiest is to also specify the position (e.g. above, east, below, etc.) in addition to pos=.... Here is the complete MWE, plus anchor and pos specified for those line labels:

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,automata,positioning} 
\newcommand{\N}{\mathbb{N}} % natuerliche Zahlen
    \newcommand{\Z}{\mathbb{Z}} % ganze Zahlen
    \newcommand{\Q}{\mathbb{Q}} % rationale Zahlen
    \newcommand{\R}{\mathbb{R}} % reelle Zahlen
    \newcommand{\Prob}{\mathbb{P}} % Symbol fuer Wahrscheinlichkeitsma├č
    \newcommand{\E}{\mathbb{E}} % Symbol fuer Erwartungswert
    \newcommand{\QAt}{\mathbf{Q}_{A_{2}A_{2}}}
    \newcommand{\QAo}{\mathbf{Q}_{A_{1}A_{1}}}
    \newcommand{\QAot}{\mathbf{Q}_{A_{1}A_{2}}}
    \newcommand{\QAto}{\mathbf{Q}_{A_{2}A_{1}}}
    \newcommand{\QAtC}{\mathbf{Q}_{A_{2}C}}
    \newcommand{\QAtw}{\mathbf{Q}_{A_{2}\{\omega\}}}
    \newcommand{\QAow}{\mathbf{Q}_{A_{1}\{\omega\}}}
    \newcommand{\I}{\mathbf{I}}
    \newcommand{\vf}{\mathbf{v}}
    \newcommand{\Df}{\mathbf{D}}
    \newcommand{\af}{\pmb{\alpha}}
    \newcommand{\eef}{\mathbf{e}}
    \newcommand{\Psf}{\pmb{\Psi}}
    \newcommand{\Phf}{\pmb{\Phi}}
    \newcommand{\bef}{\pmb{\beta}}
    \newcommand{\Pf}{\mathbf{P}}
    \newcommand{\Qf}{\mathbf{Q}}
    \newcommand{\mf}{\mathbf{m}}
    \newcommand{\ef}{\pmb{\varepsilon}}
    \newcommand{\Hf}{\mathbf{H}}
    \newcommand{\Ef}{\mathbf{E}}
    \newcommand{\Mf}{\mathbf{M}}
    \newcommand{\Vf}{\mathbf{V}}
    \newcommand{\gf}{\mathbf{g}}
    \newcommand{\QSo}{\mathbf{Q}_{S' S'}}
    \newcommand{\QSot}{\mathbf{Q}_{S' S''}}
    \newcommand{\QSt}{\mathbf{Q}_{S'' S''}}
    \newcommand{\UP}[2]{\makebox[0pt]{\smash{\raisebox{1.5em}{$\phantom{#2}#1$}}}#2}
    \newcommand{\LF}[1]{\makebox[0pt]{$#1$\hspace{4.5em}}}
    \newcommand{\zfo}{\mathbf{z}_{1}}
    \newcommand{\zft}{\mathbf{z}_{2}}
    \newcommand{\zf}{\mathbf{z}}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[->,>=stealth,shorten >=1pt,auto,node distance=2cm,
                    semithick,
 every state/.append style={fill=red,draw=none,text=white,circle},
 every loop/.append style={looseness=5}]
  \path[use as bounding box] (-2.7,0.45) rectangle (15,-10.65);
  \node[state]         (A)              {$A_{2}(0)$};
  \node                (B) [right=of A] {$\{\omega\}$};
  \node[state]         (C) [right=of B] {$A_{1}(0)$};
  \node[state]         (D) [below=of A] {$A_{2}(1)$};
  \node                (E) [right=of D] {$\{\omega\}$};
  \node[state]         (F) [right=of E] {$A_{1}(1)$};
  \node[state]         (G) [below=of D] {$A_{2}$(n{-}2)};
  \node                (H) [right=of G] {$\{\omega\}$};
  \node[state]         (I) [right=of H] {$A_{1}$(n{-}2)};
  \node[state]         (J) [below=of G]  {$A_{2}$(n{-}1)};
  \node                (K) [right=of J] {$\{\omega\}$};
  \node[state]         (L) [right=of K] {$A_{1}$(n{-}1)};



    \path (A)   edge                        node {$\QAtw$}  (B)
                edge [left]                 node {$\QAt$}       (D)
                edge [pos=0.8]              node {$\QAto$}  (F)
        (C)     edge [above]                node {$\QAow$}  (B)
                edge [above left,pos=0.8]   node {$\QAot$}  (D)
                edge [right]                    node {$\QAo$}       (F)
        (D)     edge                        node {$\QAtw$}  (E)
                                            node(aux) {}        (G)
        (F)     edge [above]                node {$\QAow$}  (E)
                                            node(aux1) {}       (I)
        (G)     edge                        node {$\QAtw$}  (H)
                edge [left]                 node {$\QAt$}       (J)
                edge [pos=0.8]              node {$\QAto$}  (L)
        (I)     edge [above]                node {$\QAow$}  (H)
                edge [above left,pos=0.8]   node {$\QAot$}  (J)
                edge                        node {$\QAo$}       (L)
        (J)     edge [above]                node {$1$}          (K)
        (L)     edge [above]                node {$1$}          (K);

\node[below=8mm of aux,font=\bfseries\Huge]{\vdots};
\node[below=8mm of aux1,font=\bfseries\Huge]{\vdots};
\end{tikzpicture}
\caption{\"Ubergangsgraph von $X$.}\label{Abbildung4}
\end{figure}
\end{document}

Add anchor and pos to the line label options

It looks to me this covers your need... however, if you want more flexibility, consider also using xshift or yshift for the node label. Here, I will xshift the bottom left QA1A2 label using xshift=25 (using 25 only to clearly show the change). Recall, positive xshift moves right and negative moves left; positive yshift moves up and negative moves down.

In this example, I change only:

%edge [above left,pos=0.8]  node {$\QAot$}  (J)% without xshift (current version)
%%%%to
edge [above left,pos=0.8]   node[xshift=25] {$\QAot$}   (J)

Adding xshift into the node label options

Hope these options give you flexibility you need!