0

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?

  • 1
    (i) you should provide complete, but small document, which show your problem. (ii) you use strange option in your nodes definitions. (iii) you need to clarify, what is overlapped. i van't reproducing anything like this. – Zarko Mar 12 '18 at 15:57
  • 1
    @Cilenco Please consider posting a MWE. Begins with \documentclass ends with \end{document}. Cutting and pasting is far more easy than guessing your preamble. – Denis Mar 12 '18 at 15:58
3

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.
  • You are right this is much cleaner thank you for this. How can I set the text of an edge above or under the arrow with the quotes library? Can I get line breaks in it? And is it possible to rotate the text according to the arrow angle if it is not horizontally? – Cilenco Mar 12 '18 at 16:48
  • (i) all is explained in tikz & pgf manual. it is huge, but read third part TikZ is kein zeichen program is sufficient for start. i will ad some more information to my answer soon. – Zarko Mar 12 '18 at 17:39
2

There are tons of different ways to make them non-overlapping, here is one of them.

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,arrows}
\begin{document}
\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.40) edge [align=center,above] node {$A \rightarrow B$} (B.140);
\path (B.220) edge [align=center,below] node {$B \rightarrow A$} (A.-40);

\end{tikzpicture}
\end{document}

enter image description here

By changing the angles you can move them closer.

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.