[Tex/LaTex] TikZ, coloring edge segments with different colors

coloredgetikz-pgf

In TikZ, I am looking for a way to color segments of a single edge with different colors, like the edge aba->bbb in the picture below.

How to color the edge aba->bbb according to the picture?

Any ideas?

For a presentation (I use the beamer package), I am trying to show how the edges of graph G^3 can be generated from the edges of G (see the definition in the picture) and thought this would be a good way. If you have any other ideas (possibly those that are easier to TeX), please tell me!

Best Answer

Second update

Now that I reread the question, here's a tricolor style; first the total length of the path is measured and then the three colors are applied to each third part of the path:

enter image description here

The code:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\newlength\mylen

\tikzset{
tricolor/.style n args={3}{
  decoration={
    markings,
    mark=at position 0.5 with {
      \node[draw=none,inner sep=0pt,fill=none,text width=0pt,minimum size=0pt] {\global\setlength\mylen{\pgfdecoratedpathlength}};
    },
  },
  draw=#1,
  dash pattern=on 0.333333\mylen off 0.666666\mylen,
  preaction={decorate},
  postaction={
    draw=#2,
    dash pattern=on 0.333333\mylen off 0.333333\mylen,dash phase=0.333333\mylen
  },
  postaction={
    draw=#3,
    dash pattern=on 0.333333\mylen off 0.666666\mylen,dash phase=0.333333\mylen
    },
  }
}
\begin{document}

\begin{frame}
\centering
\begin{tikzpicture}[
  line width=1pt,
  every node/.append style={draw,circle,minimum size=3em},
  >=latex
]
\node (a) at (-5,3) {a};
\node (b) at (-5,0) {b};
\node (bbb) at (0,0) {bbb};
\node (aaa) at (180:3) {aaa};
\node (baa) at (150:3) {baa};
\node (aba) at (120:3) {aba};
\node (aab) at (90:3) {aab};
\node (bba) at (60:3) {bba};
\node (bab) at (30:3) {bab};
\node (abb) at (0:3) {abb};
\draw[->,red!80!black]
  (a) -- (b);
\draw[->,cyan]
  (b) to[out=210,in=330,looseness=4] (b);
\draw[red!80!black,->]
  (aaa) -- (bbb);
\draw[->,tricolor={cyan}{red!80!black}{red!80!black}]
  (baa) to[out=-80,in=150] (bbb);
\draw[->,tricolor={red!80!black}{cyan}{red!80!black}]
  (aba) to[out=-100,in=120] (bbb);
\draw[->,tricolor={red!80!black}{red!80!black}{cyan}]
  (aab) -- (bbb);
\draw[->,tricolor={cyan}{cyan}{red!80!black}]
  (bba) to[out=280,in=60] (bbb);
\draw[->,tricolor={cyan}{red!80!black}{cyan}]
  (bab) to[out=260,in=30] (bbb);
\draw[->,tricolor={red!80!black}{cyan}{cyan}]
  (abb) -- (bbb);
\draw[cyan]
  (bbb) to[out=210,in=330,looseness=4] (bbb);
\end{tikzpicture}
\end{frame}

\end{document}

Of course, tricolor can be used in a true three different color context:

enter image description here

The code:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\newlength\mylen

\tikzset{
tricolor/.style n args={3}{
  decoration={
    markings,
    mark=at position 0.5 with {
      \node[draw=none,inner sep=0pt,fill=none,text width=0pt,minimum size=0pt] {\global\setlength\mylen{\pgfdecoratedpathlength}};
    },
  },
  draw=#1,
  dash pattern=on 0.333333\mylen off 0.666666\mylen,
  preaction={decorate},
  postaction={
    draw=#2,
    dash pattern=on 0.333333\mylen off 0.333333\mylen,dash phase=0.333333\mylen
  },
  postaction={
    draw=#3,
    dash pattern=on 0.333333\mylen off 0.666666\mylen,dash phase=0.333333\mylen
    },
  }
}
\begin{document}

\begin{frame}
\begin{tikzpicture}[line width=1pt]
\node (a) at (0,0) {a};
\node (b) at (2,-3) {b};
\node (c) at (4,3) {c};
\draw[->,tricolor={red}{blue}{brown}]
  (a) to[out=-60,in=90] (b);
\draw[->,tricolor={green}{magenta}{yellow}]
  (c) to[out=-60,in=90] (a);
\draw[->,tricolor={olive}{cyan}{orange}]
  (c.0) to[out=0,in=0] (b.0);
\end{tikzpicture}
\end{frame}

\end{document}

Update

The same initial idea, but now using a style:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{decorations}
\tikzset{
bicolor/.style 2 args={
  dashed,dash pattern=on 20pt off 20pt,->,#1,
  postaction={draw,dashed,dash pattern=on 20pt off 20pt,->,#2,dash phase=20pt}
  },
}
\begin{document}

\begin{frame}
\begin{tikzpicture}[line width=1pt]
\node (a) at (0,0) {a};
\node (b) at (2,-3) {b};
\node (c) at (4,3) {c};
\draw[bicolor={red}{blue}]
  (a) to[out=-60,in=90] (b);
\draw[bicolor={green}{magenta}]
  (c) to[out=-60,in=90] (a);
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

A simplistic possibility drawing the path twice, with the same dash pattern and different dash phase:

enter image description here

\documentclass{beamer}
\usepackage{tikz}

\begin{document}

\begin{frame}
\begin{tikzpicture}[line width=1pt]
\node (a) at (0,0) {a};
\node (b) at (2,-3) {b};
\draw[dashed,dash pattern=on 20pt off 20pt,->,red]
  (a) to[out=-60,in=90] (b);
\draw[dashed,dash pattern=on 20pt off 20pt,->,blue,dash phase=20pt]
  (a) to[out=-60,in=90] (b);
\end{tikzpicture}
\end{frame}
\end{document}