6

I have the following LaTeX code for a timeline (using tree), but I'd like the two empty boxes following the "no" box to disappear, and instead to just have a straight arrow to action3 -- something like this:

yes -> action1 -> action2 -> action3
no  -----------------------> action3

Here is the code:

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{trees,arrows}

\begin{document}

\begin{tikzpicture}[level distance=1in,sibling distance=.25in,scale=.65]
\tikzset{edge from parent/.style=
        {thick, draw, -latex,
            edge from parent fork right},
            every tree node/.style={draw,minimum width=0.7in,text width=0.7in, align=center},grow'=right}
\Tree
    [. {do?}
            [. {yes }
                [. {action1}
                   [. {action2}
                      [. {action3}
                      ]
                   ]
                ]
            ]
            [. {no }
               [. {}
                 [. {}
                   [. {action3}
                   ]
                 ]
                ]
            ]
    ]

\end{tikzpicture}
\end{document}
0
4

With tikz-qtree, you can remove edges using \edge[draw=none]; and later connect the no-node and the action3-node with \draw.

\Tree
    [. {do?}
            [. {yes }
                [. {action1}
                   [. {action2}
                      [. {action3}
                      ]
                   ]
                ]
            ]
            [.\node(no){no}; \edge[draw=none];
               [ \edge[draw=none];
                 [ \edge[draw=none];
                   [.\node(a3){action3};
                   ]
                 ]
                ]
            ]
    ]
\draw[-latex] (no)--(a3);

enter image description here

1
  • It is always better to post a complete example. Could you make this compilable? – cfr Nov 24 '15 at 1:22
3

Thank you, both. Here is the solution I came up with after posting the question: (Note, the code also moves the yes and no to above the arrows) (Also note the \edge[-] command to avoid the additional intermediate arrow ends)

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{trees,arrows}

\begin{document}

\begin{tikzpicture}[level distance=1.5in,sibling distance=.25in,scale=.65]
\tikzset{edge from parent/.style={thick, draw, -latex, edge from parent fork right},
     every tree node/.style={draw,minimum width=0.7in,minimum height=0.65in,text width=0.7in, align=center},grow'=right}
\Tree
[. \node {do?};
    \edge node[above, pos=0.7] {yes};
    [. {action1}
        [. {action2}
            [. {action3} ]
        ]
    ]
    \edge[-] node[below, pos=0.6] {no};
    [
     \edge[-] {};
        [
         \edge {};
            [. {action3} ]
        ]
     ]
]
\end{tikzpicture}

\end{document} 

Here is what it looks like: enter image description here

2

Commenting . {} twice:

\Tree
    [. {do?}
            [. {yes }
                [. {action1}
                   [. {action2}
                      [. {action3}
                      ]
                   ]
                ]
            ]
            [. {no }
               [ %. {}
                 [ %. {}
                   [. {action3}
                   ]
                 ]
                ]
            ]
    ]

you get:

screenshot of output

This should be further improved removing the two arrow tips (and I guess this would require some deeper change in the code).

1

One advantage of forest is that you can use tier to tell the package that certain nodes should be on the same level of the tree, even if some have more intervening nodes than others.

For example, in the code below

    if n children=0{tier=terminums}{},

says that terminal nodes should all be placed on the same tier of the tree. So the 2 action 3 nodes can be placed without needing to create dummy nodes for the one with fewer ancestors.

A style label me={}{} is created for convenient placement of the yes and no labels on the edges. The first argument is added to the label node's options and can be used to specify a relative position e.g. above right or an anchor e.g. anchor=north west or whatever. The second argument gives the content of the label.

The big advantage of the package is that, once you've configured a style, you can specify trees very concisely. For example:

  [do?
    [action 1, label me={above, anchor=south west}{yes}
      [action 2
        [action 3]
      ]
    ]
    [action 3, label me={below, anchor=north west}{no}]
  ]

produces

action chains

Complete code:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  label me/.style n args=2{
    delay={edge label/.wrap value={node [midway, #1, font=\scriptsize] {#2}}}
  },
  for tree={
    grow'=0,
    draw,
    text width=15mm,
    minimum height=7mm,
    parent anchor=east,
    child anchor=west,
    edge={->},
    text centered,
    edge path={
      \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- +(3mm,0) |- (.child anchor)\forestoption{edge label};
    },
    if n children=0{tier=terminums}{},
    l sep+=5mm,
  }
  [do?
    [action 1, label me={above, anchor=south west}{yes}
      [action 2
        [action 3]
      ]
    ]
    [action 3, label me={below, anchor=north west}{no}]
  ]
\end{forest}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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