# Nest branches in chains in tikz

I am trying to make a picture illustrating Cocke-Younger-Kasami. I am using tikz for this and already found out how to have arrows between table cells. Now I'm having the following problem: I need to branch more than one time and don't know how to do it. I already skimmed the manual but haven't found anything. Hopefully, you can help me.

Here is my code:

\documentclass[a4paper]{scrartcl}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,scopes,arrows}
\begin{document}
\begin{tikzpicture}[every on chain/.style={join={by <-}}]
\matrix[matrix of math nodes,column sep=2em,row sep=0.3em] (mx) {
V_{1,n}                                                \\
V_{1,n-1} & V_{2,n}                                    \\
\vdots    & \vdots  & \vdots                           \\
V_{1,2}   & V_{2,3} & \ldots  & V_{n-1,n}              \\
V_{1,1}   & V_{2,2} & \ldots  & V_{n-1,n-1} & V_{n,n}  \\[1ex]
w_1 & w_2 & \ldots  & w_{n-1} & w_n                    \\
};
{[start chain]
\chainin (mx-1-1);
{[start branch] \chainin (mx-2-1); \chainin (mx-3-1);}
{[start branch] \chainin (mx-2-2); \chainin (mx-3-2);}
}
\end{tikzpicture}
\end{document}


Roughly expected behaviour:

• Welcome to TeX.SE. While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages (especially with tikz) so that those trying to help don't have to recreate it. – Peter Grill Feb 15 '12 at 17:08
• I see that both chains are linked to 1-1, what are you looking for here? What is your expected behavior? – adn Feb 15 '12 at 17:43

In principle I know a way, but stragely \chainin is not defined when used inside \foreach loops. In the example I tried to do this, but it did not work. The node names are parsed correctly, to test this I drew the colored circles.

### Update 2013-03-14 by Qrrbrbirlbel

The problem is already documented in using foreach to draw a chain with a branch.
TikZ’ \foreach, chains and the scopes library do not play nice together.

Though it is possible to use the full version of the scope environment.

## Code

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,chains,scopes,arrows}
\begin{document}
\begin{tikzpicture}[every on chain/.style={join={by <-}}]
\matrix[
matrix of math nodes,
column sep=2em,
row sep=1.3em,
] (mx) {
V_{1,n}                                                \\
V_{1,n-1} & V_{2,n}                                    \\
\vdots    & \vdots  & \vdots                           \\
V_{1,2}   & V_{2,3} & \ldots  & V_{n-1,n}              \\
V_{1,1}   & V_{2,2} & \ldots  & V_{n-1,n-1} & V_{n,n}  \\[1ex]
w_1 & w_2 & \ldots  & w_{n-1} & w_n                    \\
};
\foreach \y in {2,...,5}
{   \foreach \x in {2,...,\y}
{   \pgfmathtruncatemacro{\lastx}{\x-1}
\pgfmathtruncatemacro{\lasty}{\y-1}
\begin{scope}[start chain] \chainin (mx-\lasty-\lastx);
\begin{scope}[start branch] \chainin (mx-\y-\lastx);\end{scope}
\begin{scope}[start branch] \chainin (mx-\y-\x);\end{scope}
\end{scope}
%            \fill[fill=blue,opacity=0.3] (mx-\lasty-\lastx.north) circle (0.2);
%                \fill[fill=red,opacity=0.3] (mx-\y-\lastx.west) circle (0.2);
%                \fill[fill=green,opacity=0.3] (mx-\y-\x.east) circle (0.2);
}
}
\end{tikzpicture}
\end{document}