3

I am trying to draw an undirected graph with some edges colored with two colors side-by-side. I really like this solution suggested by Symbol 1, but I cannot get it to work for straight edges.

Any help would be greatly appreciated!

MWE:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzset{
    semithick,
    node distance = 2cm,
    dot/.style={circle,fill,inner sep=2pt}
}
\tikzset{
    side by side/.style 2 args={
    line width=2pt,
    #1,
    postaction={
        clip,postaction={draw,#2}
        }
    }
}
\tikzstyle{every state}=[draw = black,thick,fill = white,minimum size = 4mm]
\tikzstyle{selected edge} = [draw,line width=2pt,-,red!50]

\begin{document} \begin{tikzpicture} \node[dot] (0) {}; \node[dot] [right of=0] (1) {}; \node[dot] [below right of=1, yshift=15] (2) {}; \node[dot] [below of=0] (3) {}; \node[dot] [right of=3] (4) {};

          \path (0) edge[side by side={red!50}{blue!50},bend left] (1);
          \path (0) edge[side by side={red!50}{blue!50},bend right] (3);
          \path (1) edge (2);
          \path (1) edge[selected edge] (4);
          \path (1) edge (4);
          \path (3) edge[selected edge] (1);
          \path (3) edge (1); 
          \path (3) edge[side by side={red!50}{blue!50},bend right] (4);
          \path (4) edge[side by side={red!50}{blue!50},bend right] (2);
  \end{tikzpicture}

\end{document}

enter image description here

  • 1
    Maybe you could try to play with node angles to begin and end your lines in two colours. And not related, but you mixed tikzset and tikzstyle here. The latter is deprecated nowadays. Try and use only tikzset. – SebGlav Feb 14 '21 at 09:58
  • @SebGlav Thank you for the suggestion! I am quite new to tikz, if you don't mind - how do I change the node angles? – Emilia Dunfelt Feb 14 '21 at 11:54
  • Experiment with bend right=angle. where angle is 10 or lower. If it gets too small, the second color vanishes. This is not a solution, but maybe you can live with a weakly bent edge (unless there is a better solution). – gernot Feb 14 '21 at 12:11
  • @gernot Thank you! I settled for an 8 degree angle, it looks a bit ridiculous but it is probably more than enough for this purpose (it's only a graph to demonstrate something for my CS-assignment). – Emilia Dunfelt Feb 14 '21 at 12:24

1 Answers1

1

As per my answer on the linked Q, the nfold library can help.

That said, for straight lines this could be achieved with built-in tools by just shifting the start and end points by half the line width orthogonally to the direction of the line, the same way shift left and shift rightof TikZ-CD does, but nfold can do it, too.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{nfold}
\makeatletter
\tikzset{
  side by side/.style args={#1:#2}{
    postaction={path only,draw=#1,offset=+.5\pgflinewidth},
    postaction={path only,draw=#2,offset=+-.5\pgflinewidth}},
  side by side'/.style={path only,side by side={#1}},
  offset/.code=
    \tikz@addoption{%
      \pgfgetpath\tikz@temp
      \pgfsetpath\pgfutil@empty
      \pgfoffsetpath\tikz@temp{#1}}}
\makeatother
\begin{document}
\begin{tikzpicture}[
  dot/.style={circle, fill, inner sep=+2pt, outer sep=+0pt},
  thick,
]
\node foreach[count=\i from 0]\p in {(0, 0), (2, 0), (3.5, -1), (0, -2), (2, -2)}
  [dot] (\i) at \p {};
\path
  (1) edge (2)
  [every edge/.append style={side by side=blue!50:red!50}]
      edge (3) edge (4)
  [bend right]
      edge (0)
           (0) edge (3)
                    (3) edge (4)
                             (4) edge (2);
\end{tikzpicture}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821