[Tex/LaTex] Tikz edges overlapping

tikz-pgf

I want to draw a simple graph with the tikz library with the code below:

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=0.5cm and 3.5cm, auto, semithick]
\tikzset{state/.style={rectangle,rounded corners,draw=black,very thick,inner sep=20pt,sibling distance=25mm}}

\node[state,align=center] (A)               {A};
\node[state,align=center] (B) [right =of A] {B};

\path (A) edge [align=center,above] node {$A \rightarrow B$} (B);
\path (B) edge [align=center,below] node {$B \rightarrow A$} (A);

\end{tikzpicture}

My problem is that the edges are overlapping. I read through some comments here and found a solutions but it is not really satisfying (think I am too inexperienced to get it right)

\path ($(A)+(0,0.5)$) edge [align=center,above] node {$A \rightarrow B$} ($(B)+(0,0.5)$);

With this solution is the arrow is calculated from the center of the node and not from its border where it should start. Is it possible to let the arrow start and end at the border of the node?

I also found a second solution:

\path (A)++(0,0.5) edge [align=center,above] node {$A \rightarrow B$} (B)++(0,-0.5);

Here the arrow is also calculated from the center of the node but the second calculation ((B)++(0,-0.5)) is not applied. Do you have any ideas how I get my described behavior?

Best Answer

your code is very strange:

    \begin{tikzpicture}[->,>=stealth',
    shorten >=1pt,
    auto,
    node distance=0.5cm and 3.5cm, 
    auto, % why you need second time
    semithick,
% merged from tikzset
state/.style={rectangle,  % state is predefined name in `automata` library, 
                          % don't redefine it!
              rounded corners,                                               
              draw=black, very thick,
              inner sep=20pt,
              sibling distance=35mm % this do nothing
              }
                    ]
\node[state,align=center] (A)  {A}; % you not define text width, 
                                    % so there is nothing to align!
...
     \end{tikzpicture}

inspired with nice marmot solution let me show an alternative:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                positioning,
                quotes
                }

\begin{document}
    \begin{tikzpicture}[auto,
    node distance=0.5cm and 3.5cm,
    box/.style={rectangle, rounded corners, draw, very thick,
                inner sep=20pt}
                    ]
\node (A) [box] (A)         {A};
\node (B) [box,right=of A]  {B};
\draw[-Stealth, thick]
    ([yshift= 7pt] A.east) edge ["$A \rightarrow B$"] ([yshift= 7pt] B.west)
    ([yshift=-7pt] B.west)  to  ["$A \rightarrow B$"] ([yshift=-7pt] A.east);
    \end{tikzpicture}
\end{document}

enter image description here

you might wish to combine both solutions at defining coordinates of arrows :-)

edit:

  • style of edge quotes you can define with every edge quotes, for exmple:

    every edge quotes/.style={align=center,font=\footnotesize, sloped}

    with this style (applied to all edge quotes) you write two (or more) lines edge labels aligned to its slope:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                positioning,
                quotes
                }
\usepackage{amssymb}

\begin{document}
    \begin{tikzpicture}[auto,
    node distance=0.5cm and 3.5cm,
    box/.style={rectangle, rounded corners, draw, very thick,
                inner sep=20pt},
     every edge quotes/.style={align=center, font=\footnotesize, sloped},
     every edge/.style = {draw, -Stealth, thick}
                    ]
\node   (A) [box] (A)        {A};
\node   (B) [box,right=of A] {B};
\path   (A.south east)  edge ["$A \rightarrow B$\\
                              {$A, B \in \mathbb{Z}$}",above] (B.north west)
                             % option `above` cener label to ege length
                             % second line is in curly braces because
                             % expresion contain comma: $A, B ...
        (B)             edge [red, opacity=0.25, "$A \rightarrow B$"] (A);
    \end{tikzpicture}
\end{document}

enter image description here

  • note: above code differ from the first in definition of edges. here it is explicit defined and consequently you can use edge for both arrows meanwhile at the first you can notice that for the second arrow is used to. this also cause, that now all options in brackets are applied to edge and not only to edge label as before.