3

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!

1

2 Answers 2

4

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

7
  • Thank you ever so much. This is a nice version, however it does not seem to work for me. I always get syntax errors about missing brackets (}), if I am not mistaken.
    – lupo
    Commented Oct 23, 2015 at 10:13
  • Meh, I figured that out. Somehow the babel [ngerman] library conflicts with your code. I don't know why. When I removed it, the code worked.
    – lupo
    Commented Oct 23, 2015 at 10:23
  • 1
    It's working now. I was mistaken in my first comment, there were no brackets missing but babel interpreting " as an active character to do shorthands like "a. With \usepackage[shorthands=off,ngerman]{babel} I was able to disable the shorthands, because I don't need them, and now everything works fine.
    – lupo
    Commented Oct 23, 2015 at 10:46
  • 1
    Some babels change active characters. I usual use English in for my native languages, where is no such problems. I'm not expert for babels, but I'm pretty sure, that somewhere in SE is solution for this (I remember that I saw for similar cases solution for Spanish and French babels).
    – Zarko
    Commented Oct 23, 2015 at 10:46
  • 1
    I'm glad that you find solution for babel. I will store it in my private examples collection. Happy TeXing!
    – Zarko
    Commented Oct 23, 2015 at 10:49
3

You won't need a label style but add the nodes staright away.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,matrix}

\begin{document}
  \begin{tikzpicture}[>=Stealth,->,line width=1pt]
\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(2)-- node[below]{$50$}(1);
\draw (3)--node[below,sloped]{$25\%$}(2);
\draw[transform canvas={xshift=2pt, yshift=2pt}, shorten <= -1pt,] (3)-- node[rotate=180,below,sloped]{$100$}(1);
\draw[transform canvas={xshift=-2pt, yshift=-2pt}, shorten <= -1pt] (1)-- node[below,sloped]{$2\%$}(3);
\end{tikzpicture}

\end{document}

enter image description here

4
  • You can insert the node after the second point as well as before it. Tikz knows where midway is either way. Commented Oct 22, 2015 at 23:59
  • @JohnKormylo: That way one has to explicitly use midway or pos=0.5.
    – user11232
    Commented Oct 23, 2015 at 6:50
  • @HarishKumar: Just curiosity. How do you postprocess your results to get images like this one? Your images are always cropped and framed, but posted code doesn't use standalone nor command for frames.
    – Ignasi
    Commented Oct 23, 2015 at 6:58
  • @Ignasi: I am on windows and there is this snipping tool (I think from vista onwards). I take a screen shot and in settings of snipping tool keep the border on.
    – user11232
    Commented Oct 23, 2015 at 7:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .