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.

This is related to the question How to make a connection in a tree with a dashed line?. Therein, it is asked how one can make one connection between a parent node and a child dashed.

The final solution proposed is along the line of alternating the edge from parent[solid] and edge from parent[dashed], which is not only ugly, but also incomplete: when drawing nodes at each child, the node inherits (albeit in a weird fashion) the dashed or solid property of the edge from parent.

Let's start with a common preamble:

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{trees}
\def\es{edge from parent[solid]}
\def\ed{edge from parent[dashed]}
\begin{document}
\begin{tikzpicture}[every node/.style={draw},level/.style={sibling distance=20mm/#1}]

Now let's start with what is natural to write.

  \node {}
  child{
    node {}
    child{
      node {}
      child{
        node {}
        child{node{}}
        child}
      child{node{}}}
    child
    child{
      child}
    \ed}
  child {child[missing] child{child} child {child{child child} child}};

this outputs:

First example

This was the original problem called for in the aforementioned question.

The proposed solution, adding \es every other places, resuts in:

  \node {}
  child{
    node {}
    child{
      node {}
      child{
        node {}
        child{node{}}
        child}
      child{node{}}
      \es}
    child{\es}
    child{
      child
      \es}
    \ed}
  child {child[missing] child{child} child {child{child child} child}};

which is displayed as:

Second

has two serious problems: the redundancy, and the fact that a node appears dashed.

Finally, after many attempts, I came up with the following solution:

  \node {}
  child{
    node {}
    child{
      node {}
      child{
        node {}
        child{node{}}
        child}
      child{node{}}}
    child
    child{
      child};
    \path \ed;}
  child {child[missing] child{child} child {child{child child} child}}

which nicely displays as:

Third

Question: Why? Why do I need a \path? Why doesn't it work with just \ed?

Thanks!

share|improve this question

1 Answer 1

I hope it's ok to provide an answer which is not really an answer but an advertisement for my own (tikz-based) tree drawing package, forest. (Until recently, it's available on ctan and also included in TeX Live.)

In forest, the properties of each node and parent-child edge can be set individually. So the stated problem is solved by simply saying edge=dashed at the desired location. (If one wants to set this property for the whole (sub)tree, for tree={edge=dashed} must be used.)

Here's the code. I didn't bother with specifying some nodes as coordinates, since this is irrelevant for the answer, but it can be done: just say coordinate at the node. I did demonstrate what to do with the missing node, though.

\documentclass{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={draw}
  [
    [,edge=dashed[[[][]][]][][[]]]
    [,calign=first[[]][[[][]][]]]
  ]
\end{forest}
\end{document}

The result

share|improve this answer

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.