# Highlighting some nodes of a TikZ binomial tree

In this binomial tree I have 4 columns of nodes.

I want the entire last column to be highlighted with light blue, and perhaps a border around this highlighting. Something like a opaque blue-ish rectangle or thin oval highlighting the entire last column of nodes.

Just started with TikZ and nothing is working so far.

I'm using pdfLaTeX.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=3.5cm,
row sep=1cm,
]
{
&                    &                     &  $^\tau S_0$     \\
&                    &  $^{2\Delta t}S_0$  &              \\
& $^{\Delta t}S_0$   &                     &  $^\tau S_1$     \\
$^0S_0$ &                    &  $^{2\Delta t}S_1$  &              \\
& $^{\Delta t}S_1$  &                      &  $^\tau S_2$     \\
&                    &  $^{2\Delta t}S_2$  &              \\
&                    &                     &  $^\tau S_2$     \\
};
\draw[->] (tree-4-1) -- (tree-3-2) node [midway,above] {$p$};
\draw[->] (tree-4-1) -- (tree-5-2) node [midway,below] {$1-p$};
\draw[->] (tree-3-2) -- (tree-2-3) node [midway,above] {};
\draw[->] (tree-3-2) -- (tree-4-3) node [midway,below] {};
\draw[->] (tree-5-2) -- (tree-4-3) node [midway,above] {};
\draw[->] (tree-5-2) -- (tree-6-3) node [midway,below] {};

\draw[->] (tree-2-3) -- (tree-1-4) node [midway,above] {};
\draw[->] (tree-2-3) -- (tree-3-4) node [midway,below] {};
\draw[->] (tree-4-3) -- (tree-3-4) node [midway,above] {};
\draw[->] (tree-4-3) -- (tree-5-4) node [midway,below] {};
\draw[->] (tree-6-3) -- (tree-5-4) node [midway,above] {};
\draw[->] (tree-6-3) -- (tree-7-4) node [midway,below] {};

\end{tikzpicture}

\end{document}

-

Here are two solutions. First solution uses column 4 style to add a border and to fill each node in fourth column of your matrix. Second solution uses fit and background libraries to put a new node on background fitting tree-1-4 and tree-7-4 nodes.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,backgrounds}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=3.5cm,
row sep=1cm,
% fill and draw each node of fourth column
column 4/.style={nodes={draw,fill=cyan!30,rounded corners}},
]
{
&                  &                   & $^\tau S_0$ \\
&                  & $^{2\Delta t}S_0$ &             \\
& $^{\Delta t}S_0$ &                   & $^\tau S_1$ \\
$^0S_0$ &                  & $^{2\Delta t}S_1$ &             \\
& $^{\Delta t}S_1$ &                   & $^\tau S_2$ \\
&                  & $^{2\Delta t}S_2$ &             \\
&                  &                   & $^\tau S_2$ \\
};
\draw[->] (tree-4-1) -- (tree-3-2) node [midway,above] {$p$};
\draw[->] (tree-4-1) -- (tree-5-2) node [midway,below] {$1-p$};
\draw[->] (tree-3-2) -- (tree-2-3) node [midway,above] {};
\draw[->] (tree-3-2) -- (tree-4-3) node [midway,below] {};
\draw[->] (tree-5-2) -- (tree-4-3) node [midway,above] {};
\draw[->] (tree-5-2) -- (tree-6-3) node [midway,below] {};

\draw[->] (tree-2-3) -- (tree-1-4) node [midway,above] {};
\draw[->] (tree-2-3) -- (tree-3-4) node [midway,below] {};
\draw[->] (tree-4-3) -- (tree-3-4) node [midway,above] {};
\draw[->] (tree-4-3) -- (tree-5-4) node [midway,below] {};
\draw[->] (tree-6-3) -- (tree-5-4) node [midway,above] {};
\draw[->] (tree-6-3) -- (tree-7-4) node [midway,below] {};

% put a new node on background fitting (tree-1-4) and (tree-7-4) nodes
\begin{scope}[on background layer]
\node[fit=(tree-1-4)(tree-7-4),
fill=blue!20,rounded corners,draw=blue,line width=1pt]{};
\end{scope}
\end{tikzpicture}
\end{document}


Edit: Here is another method to position your nodes (using positioning instead of matrix).

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,backgrounds}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\tikzset{
on grid,
node distance=1cm and 3.5cm,
col4/.style={fill=cyan!20,draw,rounded corners},
every node/.style={minimum size=1cm},
}
% first column
\node (n-0-0) at (-5,-3) {$^0S_0$};

% second column
\node[above right=of n-0-0] (n-1-0) {$^{\Delta t}S_0$};
\node[below right=of n-0-0] (n-1-1) {$^{\Delta t}S_1$};

% third column
\node[above right=of n-1-0] (n-2-0) {$^{2\Delta t}S_0$};
\node[below right=of n-1-0] (n-2-1) {$^{2\Delta t}S_1$};
\node[below right=of n-1-1] (n-2-2) {$^{2\Delta t}S_2$};

% fourth column
\node[col4,above right=of n-2-0] (n-3-0) {$^{\tau}S_0$};
\node[col4,below right=of n-2-0] (n-3-1) {$^{\tau}S_1$};
\node[col4,below right=of n-2-1] (n-3-2) {$^{\tau}S_2$};
\node[col4,below right=of n-2-2] (n-3-3) {$^{\tau}S_2$};

\draw[->] (n-0-0) -- (n-1-0) node [midway,above] {$p$};
\draw[->] (n-0-0) -- (n-1-1) node [midway,below] {$1-p$};
\foreach \col in {1,2}{
\pgfmathtruncatemacro{\nextcol}{\col+1}
\foreach \line in {0,...,\col}{
\pgfmathtruncatemacro{\nextline}{\line + 1}
\draw[->] (n-\col-\line) -- (n-\nextcol-\line);
\draw[->] (n-\col-\line) -- (n-\nextcol-\nextline);
}
}

\begin{scope}[on background layer]
\node[fit=(n-3-0)(n-3-3),inner sep=.5em,
fill=blue!20,rounded corners,draw=blue,line width=1pt]{};
\end{scope}
\end{tikzpicture}
\end{document}

-

Here is a solution with the new hobby package (this question gave me the opportunity to use it for the first time: couldn't resist, but I'm sure the answer could be improved). The idea behind is quite simple: the area to be highlighted is determined by a set of coordinate, that actually are the nodes of the matrix and it has been filled in background with the dedicated library.

The solutions provided by Paul Gaborit work pretty well, but I was intrigued by the fact of highlighting not only columns, but sparse elements in the tree.

The following example contains two parts highlighted: the one needed by the OP and three other nodes.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,backgrounds, hobby,calc}

\begin{document}

\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=3.5cm,
row sep=1cm,
]
{
&                    &                     &  $^\tau S_0$     \\
&                    &  $^{2\Delta t}S_0$  &              \\
& $^{\Delta t}S_0$   &                     &  $^\tau S_1$     \\
$^0S_0$ &                    &  $^{2\Delta t}S_1$  &              \\
& $^{\Delta t}S_1$  &                      &  $^\tau S_2$     \\
&                    &  $^{2\Delta t}S_2$  &              \\
&                    &                     &  $^\tau S_2$     \\
};
\draw[->] (tree-4-1) -- (tree-3-2) node [midway,above] {$p$};
\draw[->] (tree-4-1) -- (tree-5-2) node [midway,below] {$1-p$};
\draw[->] (tree-3-2) -- (tree-2-3) node [midway,above] {};
\draw[->] (tree-3-2) -- (tree-4-3) node [midway,below] {};
\draw[->] (tree-5-2) -- (tree-4-3) node [midway,above] {};
\draw[->] (tree-5-2) -- (tree-6-3) node [midway,below] {};

\draw[->] (tree-2-3) -- (tree-1-4) node [midway,above] {};
\draw[->] (tree-2-3) -- (tree-3-4) node [midway,below] {};
\draw[->] (tree-4-3) -- (tree-3-4) node [midway,above] {};
\draw[->] (tree-4-3) -- (tree-5-4) node [midway,below] {};
\draw[->] (tree-6-3) -- (tree-5-4) node [midway,above] {};
\draw[->] (tree-6-3) -- (tree-7-4) node [midway,below] {};

\begin{pgfonlayer}{background}
% the requested case
\draw[blue,fill=cyan!15,xscale=1.725] (tree-1-4.west) to[closed,curve through={(tree-1-4.north) .. (tree-1-4.east) .. (tree-7-4.east)..(tree-7-4.south).. (tree-7-4.west) }] (tree-1-4.west);

% another case

\draw[blue,fill=cyan!15,xscale=1.1] (tree-5-2.west) to[closed,curve through={([tension in=1.4]$(tree-5-2.west)!0.5!(tree-4-3.west)$)..([tension in=-0.75]tree-4-3.west)..(tree-2-3.west)..(tree-2-3.north)..(tree-2-3.east) .. ([tension in=-0.1]tree-4-3.east)..(tree-5-2.south east)}] (tree-5-2.west);
\end{pgfonlayer}
\end{tikzpicture}

\end{document}


Result:

-
Brilliant. Won't transfer the correct answer, but thank you. – user16208 Sep 8 '12 at 7:27
+1. It's very beautiful. – Paul Gaborit Sep 8 '12 at 7:38
@PaulGaborit: thanks! – Claudio Fiandrino Sep 8 '12 at 7:54
Great answer, Claudio! :) – Paulo Cereda Sep 10 '12 at 9:24

I think I found a really interesting solution because of the simplicity.

The idea is the construction of a path that simply go through the nodes of interest. To highlight, the path must have width greater than the nodes. This path should be in a back layer to not cover the nodes.

Here is the result:

And the code:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\tikzset{
my box/.style = {
, line cap = round
, line join = round
}
}

\newcommand{\highlight}[3]{
\path [my box, line width = #1, draw = #2] #3;

\pgfmathsetmacro{\innerlinewidth}{0.9 * #1}
\path [my box, line width = \innerlinewidth, draw = #2!20] #3;
}

\begin{document}
\begin{tikzpicture}
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=3.5cm,
row sep=1cm,
] {
&                   &                     &  $^\tau S_0$ \\
&                   &  $^{2\Delta t}S_0$  &              \\
& $^{\Delta t}S_0$  &                     &  $^\tau S_1$ \\
$^0S_0$ &                   &  $^{2\Delta t}S_1$  &              \\
& $^{\Delta t}S_1$  &                     &  $^\tau S_2$ \\
&                   &  $^{2\Delta t}S_2$  &              \\
&                   &                     &  $^\tau S_2$ \\
};

\draw[->] (tree-4-1) -- (tree-3-2) node [midway,above] {$p$};
\draw[->] (tree-4-1) -- (tree-5-2) node [midway,below] {$1-p$};
\draw[->] (tree-3-2) -- (tree-2-3) node [midway,above] {};
\draw[->] (tree-3-2) -- (tree-4-3) node [midway,below] {};
\draw[->] (tree-5-2) -- (tree-4-3) node [midway,above] {};
\draw[->] (tree-5-2) -- (tree-6-3) node [midway,below] {};

\draw[->] (tree-2-3) -- (tree-1-4) node [midway,above] {};
\draw[->] (tree-2-3) -- (tree-3-4) node [midway,below] {};
\draw[->] (tree-4-3) -- (tree-3-4) node [midway,above] {};
\draw[->] (tree-4-3) -- (tree-5-4) node [midway,below] {};
\draw[->] (tree-6-3) -- (tree-5-4) node [midway,above] {};
\draw[->] (tree-6-3) -- (tree-7-4) node [midway,below] {};

\begin{pgfonlayer}{background}
\highlight{12mm}{blue}{(tree-2-3.center) -- (tree-4-3.center) -- (tree-5-2.center)}
\highlight{20mm}{green}{(tree-1-4.center) -- (tree-3-4.center) -- (tree-5-4.center) -- (tree-7-4.center)}
\end{pgfonlayer}
\end{tikzpicture}
\end{document}


And thus, we can obtain other types of results using the library decoration. Some examples below.

The result with snake decoration:

And the tikzset code:

\tikzset{
my box/.style = {
, line cap = round
, line join = round
, decorate
, decoration = {
snake
, segment length = 20mm
, amplitude = 1mm
}
}
}


Another result, with random steps:

And the tikzset code:

\tikzset{
my box/.style = {
, line cap = round
, line join = round
, decorate
, decoration = {
random steps
, segment length = 10mm
, amplitude = 1mm
}
}
}

-
Nice one. You can also use a double line to get the path drawn only once which would make this even more concise : \path [my box, line width=0.2*#1,double, double distance=0.9*#1,double=#2!20, draw, #2] #3; – percusse Oct 18 '12 at 2:34
Thanks, @percusse! I did not know this feature. Where can I find more information about it? – tecepe Oct 18 '12 at 3:17
You can find it in the TikZ manual page 160 for the version 2.10. – percusse Oct 18 '12 at 3:33
Wow: absolutely nice!! – Claudio Fiandrino Oct 18 '12 at 6:32