8

So I can do this:

tree one

But I want to do this: tree two

That is, its easy to add text alongside the connectors (either slanting alongside them or straight), but I would like part of the connector to be flat with the caption text along the flat part for better readability.

I thought perhaps I could do this by creating a fake node that would be just a line with text above it. Not sure how to implement that or if there is an easier way.

Thanks for any ideas?

Code for first figure:

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

\tikzstyle{decision} = [rectangle, minimum height=18pt, minimum width=18pt, draw=blue, fill=none, ultra thick, inner sep=0pt]
\tikzstyle{chance} = [circle, minimum width=18pt, draw=blue, fill=none, ultra thick, inner sep=0pt]
\tikzstyle{line} = [draw=none]

\tikzset{
grow=right,
sloped,
join=miter,
level 1/.style={sibling distance=5cm,level distance=5.2cm},
level 2/.style={sibling distance=4cm, level distance=6.7cm},
level 3/.style={sibling distance=3cm, level distance=6.7cm},
edge from parent/.style={ultra thick, draw=blue},
edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)},
every node/.style={text ragged, inner sep=1mm}
}

\begin{tikzpicture}[]
\small
\node[decision]{}
    child{node[line]{}
      edge from parent
        node[above]{Option 2}
    }
    child{node[chance]{}
      child{node[line]{}}
      child{node[line]{}}
      edge from parent
            node[below]{Option 1}
        };
\end{tikzpicture}

\end{document}  
3
  • And will the text always be of the form "Option #"? May 14, 2014 at 20:39
  • Oh no, not necessarily. Just a short description of that branch. May 14, 2014 at 20:41
  • 4
    Your "I can do this" should be accompanied by a minimal working example (MWE) always. It makes it far quicker for the community to get going on a solution than having to rewrite code from scratch. Always.
    – Werner
    May 14, 2014 at 20:53

1 Answer 1

17

You can use the forest package. A little example (adjust the settings according to your needs):

\documentclass{article}
\usepackage{forest}

\tikzset{
Above/.style={
  midway,
  above,
  font=\scriptsize,
  text width=1.5cm,
  align=center,
  },
Below/.style={
  midway,
  below,
  font=\scriptsize,
  text width=1.5cm,
  align=center
  }
}

\begin{document}

\begin{forest} 
for tree={
  grow=east,
  draw=cyan,
  circle,
  line width=0.2pt,
  parent anchor=east,
  child anchor=west,
  edge={draw=cyan},
  edge label={\Huge\color{black}},
  edge path={
    \noexpand\path[\forestoption{edge}]
      (!u.parent anchor) -- ([xshift=-1.6cm].child anchor) --    
      (.child anchor)\forestoption{edge label};
  },
  l sep=2cm,
} 
[,rectangle, s sep=35pt,
  [,edge label={node[Below]{option1}}
    [,edge label={node[Below]{a longer text goes here}}
    ]
    [,edge label={node[Above]{text}}
    ]
  ]
  [,edge label={node[Above]{option2}}
    [,edge label={node[Below]{a longer text goes here}}
    ]
    [,edge label={node[Above]{text}}
    ]
  ]
]
\end{forest}

\end{document}

enter image description here

3
  • @user2726768 You're welcome! I made a simplification to the code that might be of interest for you. May 14, 2014 at 21:03
  • @Zarko Maybe you should ask a new question, especially since I'm not sure that the author of this answer is available at the moment. That is, I expect that he'll try to answer when he returns, but you might get a faster answer if you open your question up. just a suggestion, obviously.
    – cfr
    Feb 16, 2016 at 22:00
  • @Zarko He is the Forest Master. (The package author is the Forest Tsar, of course.) But you can't always get the best ;).
    – cfr
    Feb 16, 2016 at 22:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .