[Tex/LaTex] Problem with tikz and rotating labels

labelsnodesrotatingtikz-pgf

I am fairly new to LaTeX and especially to tikz. I tried to draw a directed graph which actually turned out the way I wanted it to. I don't know how well written this code is, but it works. Code in question:

\begin{tikzpicture}[>=Stealth,->,line width=1pt, domain=0:2,label/.style={%
   postaction={ decorate,transform shape,
   decoration={ markings, mark=at position 0.5 with \node #1;}}}]
\matrix [matrix of math nodes,
column sep={1.5cm,between origins},
row sep={2.2cm,between origins},
nodes={circle, draw, minimum size=1cm}] {
& |(1)| J &   & |(2)| A & \\
&  & |(3)| R &  \\};
\draw[label={[below]{$50$}}] (2)--(1);
\draw[label={[below]{$25 \%$}}] (3)--(2);
\draw[transform canvas={xshift=2pt, yshift=2pt}, shorten <= -1pt, label=    {[below]{$100$}}] (3)--(1);
\draw[transform canvas={xshift=-2pt, yshift=-2pt}, shorten <= -1pt, label=    {[below]{$2 \%$}}] (1)--(3);
\end{tikzpicture}

However, after discovering a working solution to add labels to the lines drawn, I now have the problems that the topmost label is upside down. Why this is so, is clear to me, however I don't know how to fix it.

I have read something about doing stuff like

\draw[label={[below, rotate=180]{$50$}}] (2)--(1);

but it's not working.

If you happen to know how to solve my problem or improve the structure of the directed graph, I would be very grateful!

Best Answer

Your picture remind me on automaton, so why don't use automata library?

    \documentclass[border=3mm,
               tikz,
               ]{standalone}
\usetikzlibrary{arrows.meta,automata,positioning,babel}

    \begin{document}
    \begin{tikzpicture}[
     node distance = 30mm,
     on grid,
     auto,
                 > = {Stealth[length=5pt,width=4pt]},
every state/.style = {draw=blue!50,very thick,fill=blue!20}
                        ]
\node[state] (J) {$J$};
\node[state] (R) [below right=of J] {$R$};
\node[state] (A) [above right=of R] {$A$};
\path[->]   (A) edge            node[above]          {50}    (J)
            (J) edge[bend left] node[pos=0.4,sloped] {2\%}   (R)
            (R) edge            node[pos=0.7,sloped] {25\%}  (A)
            (R) edge[bend left] node[pos=0.4,sloped] {100}   (J)
            ;
    \end{tikzpicture}
    \end{document}

It gives:

enter image description here

Since sloped option of edge nodes doesn't put edge labels on the middle of the pat, I manually correct their position (by trial).

Upgrade: by use of library quotes the draw this automation is even simpler:

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

    \begin{document}
    \begin{tikzpicture}[
     node distance = 30mm,
     on grid,
                 > = {Stealth[length=5pt,width=4pt]},
every state/.style = {very thick},
every edge quotes/.style = {sloped, anchor=north}
                        ]
\node[state] (J) {$J$};
\node[state] (R) [below right=of J] {$R$};
\node[state] (A) [above right=of R] {$A$};
\path[->]   (A) edge ["50"]             (J)
            (J) edge[bend left,"2\%"]   (R)
            (R) edge["25\%"]            (A)
            (R) edge[bend left,"100"]   (J);
    \end{tikzpicture}
    \end{document}

Obtained picture is almost the same (this time in "black and white" version). And here is not necessary manual adjusting of edge labels ...

Edit: I was not aware with possible implication of used babel on the provided code. One solution, how to copy with this provide OP:

\usepackage[shorthands=off,ngerman]{babel}

Another, more general -- as remind me Gonzalo Medina -- is use babel library:

\usetikzlibrary{arrows.meta,automata,quotes,positioning,%
                babel% for avoid complications with any language in babel
                }

I correct both above examples accordingly.

enter image description here