2

I need to draw several graphs (the graph theory kind) with arrows drawn next to the edges. Here is a rough sketch of the kind of thing I mean:

enter image description here

I'm aware of tkz-graph and would like to use it if possible - I have looked through its manual (I don't understand French but I can understand the code examples) but I don't see anything like this.

Alternatively I'm happy to use just plain tikz to do it, but I don't know how to draw things parallel to the edges in this way. (I want to do it automatically, not position the arrows by hand.)

Is there a straightforward way to do it, either in tkz-graph or just in tikz?

9
  • The TikZ graph drawing stuff is documented in the TikZ manual in English. Maybe that helps?
    – cfr
    Feb 27, 2016 at 4:20
  • @cfr it helps a little bit, but the manual is over 850 pages and doesn't deal with the tikz-graph package as far as I can tell, so a more specific solution would be helpful. I'm reasonably familiar with tikz, but don't know how to do this particular task.
    – N. Virgo
    Feb 27, 2016 at 4:24
  • So would an MWE showing what you're tried. And the entire manual is not devoted to graph drawing. It has a section with a relevant title and everything, even if you don't like indices.
    – cfr
    Feb 27, 2016 at 4:26
  • @Nathaniel, than you have an old TikZ manual. Recent (v 3.0.1a) has 1161 pages. So I advise you to upgrade your TikZ package.
    – Zarko
    Feb 27, 2016 at 4:29
  • @cfr if I could post a mwe I would already know how to answer the question. All I have tried so far is to look up how to do it, and I have failed at that. Beyond that there is nothing else I can try besides asking here.
    – N. Virgo
    Feb 27, 2016 at 4:31

1 Answer 1

2

A pure TikZ solution with help of `mathtools˙package:

\documentclass[tikz,border=3mm]{standalone}
    \usetikzlibrary{chains,positioning}
    \usepackage{mathtools}

\begin{document}
\begin{tikzpicture}[
    start chain = A going right,
                    ]
    \begin{scope}[every node/.style={circle, draw, minimum size=1.5em, 
                                     inner sep=2pt, on chain}] 
\foreach \i in {1,...,5} \node {};
%
\node (B-1) [below=of A-2] {A};
\node (B-2) [below=of A-4] {B};
    \end{scope}
    \begin{scope}[every node/.style={above, sloped}]
\draw[densely dotted, thick] 
                (A-1) -- (A-2)
                (A-2) -- node {$\xleftarrow{1}$}  (A-3)
                (A-3) -- (A-4)
                (A-4) -- node {$\xleftarrow{1}$} (A-5);
\draw[thick]    (B-1) -- node {$\xleftarrow{1}$} (A-2)
                (B-1) -- node {$\xrightarrow{1}$}(A-3)
%
                (B-2) -- node[below] {$\xleftarrow[1]{}$} (A-3)
                (B-2) -- node {$\xleftarrow{1}$} (A-4)
                ;
%
\draw[double]   (A-1) --  (B-1)  
                (A-5) -- node[below] {$\xleftarrow[2]{}$} (B-2);
    \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

Addendum: An alternative solution with help of pic:

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

\begin{document}
\begin{tikzpicture}[
    start chain = A going right,
pics/AB/.style args = {#1/#2/#3}{code={
    \draw[solid,-{Straight Barb[length=1mm]},very thick]
        (-3mm,#1 2mm) -- node [#2,font=\footnotesize,inner sep=2pt] {#3} ++ (6mm,0mm);}
                            },
pics/BA/.style args = {#1/#2/#3}{code={
    \draw[solid,{Straight Barb[length=1mm]}-,very thick]
        (-3mm,#1 2mm) -- node [#2,font=\footnotesize,inner sep=2pt] {#3} ++ (6mm,0mm);}
                            }
                    ]
    \begin{scope}[every node/.style={circle, draw, minimum size=1.5em, 
                                     inner sep=2pt, on chain}] 
\foreach \i in {1,...,5} \node {};
%
\node (B-1) [below=of A-2] {A};
\node (B-2) [below=of A-4] {B};
    \end{scope}
    \begin{scope}[sloped]
\draw[densely dotted, thick] 
                (A-1) -- (A-2)
                (A-2) -- pic {BA=/above/1} (A-3)%node {$\xleftarrow{1}$}  (A-3)
                (A-3) -- (A-4)
                (A-4) -- pic {AB=/above/1} (A-5);
\draw[thick]    (B-1) -- pic {AB=/left/1} (A-2)
                (B-1) -- pic {AB=-/above left/1} (A-3)
%
                (A-3) -- pic {BA=-/below left/1} (B-2)
                (B-2) -- pic {AB=/left/1} (A-4);
%
\draw[double]   (A-1) --  (B-1)  
                (A-5) -- pic {BA=-/below right/2} (B-2);
    \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

Edit: in the first version of addendum I mess up the arrow directions. Now this is corrected.

6
  • Thanks, it looks good, and I think I can modify it for my other graphs. But is there a way to prevent the numbers from being rotated like that?
    – N. Virgo
    Feb 27, 2016 at 5:42
  • @Nathaniel, in the first solution numbers are part of arrows, consequently they are always oriented with arrow. However, meantime I added another solution, where numbers are oriented as you like to have them.
    – Zarko
    Feb 27, 2016 at 5:55
  • I much prefer the look of the first version, with the Computer Modern arrows. However, rather than bug you about how to achieve the exact effect I want, I'll accept this answer and ask a separate question, so that I can understand how to do it myself.
    – N. Virgo
    Feb 27, 2016 at 7:52
  • The new question is here: tex.stackexchange.com/questions/296216/…
    – N. Virgo
    Feb 27, 2016 at 8:02
  • Many apologies for the unaccept. It occurred to me that the author of tkz-graph is active on the site and might post an answer. If that doesn't happen, I'll accept this again.
    – N. Virgo
    Feb 27, 2016 at 15:12

You must log in to answer this question.

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