Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

Roughly expected behaviour

share|improve this question
1  
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
1  
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
add comment

1 Answer

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}

Output

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.