4

I'm trying to typeset a directed graph with tikz.

This is what I got so far:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzset{
  net node/.style = {draw, circle, minimum size=8mm},
  net edge/.style = {->, >=triangle 45}, % triangle 45 arrow is bigger than latex arrow % auto,
  net cut/.style = {shorten >=-10mm, shorten <=-10mm, rounded corners=10mm, color=red},
  net cross/.style = {sloped, allow upside down, pos=.3},
}

\begin{document}
    \begin{tikzpicture}
        \newcommand{\edge}[5][]{\draw[net edge, #1] (#3) -- coordinate[net cross, name=#2] node[pos=.7, auto]{#5} (#4);}

        \node[net node, name=s ] at ( 0, 0) {$s$};
        \node[net node, name=n1] at (-2,-2) {$n_1$};
        \node[net node, name=n2] at ( 2,-2) {$n_2$};
        \node[net node, name=t]  at ( 0,-4) {$t$};

        \edge[swap] {e1}  {s} {n1} {1}
        \edge       {e2}  {s} {n2} {1}
        \edge[swap] {e3} {n1} {n2} {1}
        \edge       {e4} {n1}  {t} {1}
        \edge       {e5} {n2}  {t} {1}

        \draw[net cut] (e1) -- (e2);
        \draw[net cut] (e4) -- (e3) -- (e2);
        \draw[net cut] (e4) -- (e5);
        \draw[net cut] (e5) -- (e3) -- (e2); % Just a test!
    \end{tikzpicture}
\end{document}

The result is almost OK:

However there is some room for improvement:

  • Somehow, one cut has a sharp bend. I realized this can be circumvented by using net cross/.style = {}, however then the intersection is in the middle of the edge and I don't want that

  • It would be great if the cuts crossed the edges at 90 degrees. I already tried by making the crosses sloped and using draw with to and in and out, however it seems that one cannot specify an angle relative to a node or coordinate's orientation

I want to leave the graph code as high level as possible, i. e. it would be great if I could say something like e. g. "I want to make a cut across edges e1, e2, e4".

Any ideas?

  • 4
    Welcome to TeX.SE! Please, be so kind and merge your code snipets on one small, complete document starting with \documentcass{...} and ending with \end{document}. Help us to help you! – Zarko Jan 28 '17 at 10:44
  • @Zarko Thank you, didn't really think about making it easy to reproduce when posting the question. Edited the question. – joernschellhaas Jan 28 '17 at 11:27
4

I'm afraid that I don't understand your question correctly. ANyway, see, if the following solution gives result which is at least on one part close to what you like to have:

enter image description here

I completely rewrote your code, however you still can restore parts which I due to misunderstanding of yours question omitted:

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

\begin{document}
    \begin{tikzpicture}[
xs/.style = {xshift=#1 mm},
ys/.style = {yshift=#1 mm},
every edge quotes/.style = {auto, pos=0.5, % <-- =.3?
                            inner sep=2pt, font=\footnotesize}
                        ]
    \begin{scope}[every node/.style={circle, draw, minimum size=8mm}]
\node (s)  at ( 0, 0) {$s$};
\node (n1) at (-2,-2) {$n_1$};
\node (n2) at ( 2,-2) {$n_2$};
\node (t)  at ( 0,-4) {$t$};
    \end{scope}
\draw[-triangle 45] 
    (s)  edge ["1"] (n1)
    (s)  edge ["1"] (n2)
    (n1) edge ["1"] (n2)
    (n1) edge ["1"] (t)
    (n2) edge ["1"] (t);
\draw[rounded corners=10mm, red, densely dashed] 
    ([xs=-11]  s.west)  -- ([ys=-11] s.south)   -- ([xs= 11]  s.east)
    ([xs=-11]  t.west)  -- ([ys= 11] t.north)   -- ([xs= 11]  t.east)
    ([ys= 11] n1.north) -- ([xs= 11] n1.east)   -- ([ys=-11] n1.south)
    ([ys= 11] n2.north) -- ([xs=-11] n2.west)   -- ([ys=-11] n2.south);
    \end{tikzpicture}
\end{document}

As you can see, I use TikZ library quoetes for labeling edges between nodes. For "cuts" I use dashed line but this is simple to change into solid ones. If some cuts had to be done by straight lines, this should not be difficult to add or deleted surpluses dashed ones.

  • Good solution, thank you. I was hoping to have a solution in which I wouldn't have to specify any parameters (like xshift and yshift in your solution). I clarified my question regarding that, however I don't think it is possible without parameters. So I'm going to draw it manually like you did. – joernschellhaas Jan 31 '17 at 15:46

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.