# TikZ, coloring edge segments with different colors

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.

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!

# 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:

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:

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}


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

\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}

• How do you know all that stuff? :) – Dr. Manuel Kuehner Sep 28 '15 at 21:26
• This is strange... I took your first piece of code (the one under the aaa/aab/.../bbb graph) and made a new TeX file consisting only of that code. I compiled it with pdflatex (version 3.1415926-2.5-1.40.14 (TeX Live 2013/Debian)), but got a different output than the one you posted! The arrows drawn by the tricolored edges are black in my document, see here: imgur.com/tKoF1JW How do I get them colored? – Kjara Sep 29 '15 at 8:31
• I thought I found a solution... I thought it suffices to add the wanted arrow tip color as an option for the edge, like this: edge[tricolor={red}{red}{cyan},cyan] for an edge with a cyan-colored arrow tip. But it seems to overwrite some of the tricolor-colorings! – Kjara Sep 29 '15 at 8:55
• @JanaReini The problem is your outdated LaTeX system. In TeX Live2013 and 2014 the result is the one you linked to, but with TeX Live2015 the result is the one in my attached image. Most probably the problem lies in your version of PGF/TikZ. Try updating it, or better yet, updating your LaTeX system. – Gonzalo Medina Sep 29 '15 at 14:27
• @JanaReini I updated my code introducing some improvements. The problem with the arrow tip will remain, though, unless you update your packages/LaTeX system. – Gonzalo Medina Sep 29 '15 at 15:15