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 a follow-up to Gonzalo Medina's answer to this question.

The solution offered there includes the following code at the start of the forest environment:

  edge path={
    \noexpand\path[\forestoption{edge}]
      (!u.parent anchor) -- +(0,-13pt) -|   
      (.child anchor)\forestoption{edge label};
  },

How does this code achieve the desired layout? I am unclear how the explanation of the relevant option on page 33 of the manual relates to the resulting diagram. In general, I find forest's documentation rather difficult to follow when it comes to customising tree layout (as opposed to specifying the relationships between nodes etc.) but I would like to understand it since forest seems to be more powerful and flexible in principle than the available alternatives.

For completeness, the full code of Gonzalo Medina's solution is:

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{forest} 
where n children=0{font=\sffamily}{},
for tree={
  edge path={
    \noexpand\path[\forestoption{edge}]
      (!u.parent anchor) -- +(0,-13pt) -|   
      (.child anchor)\forestoption{edge label};
  },
  l sep=10pt,
} 
[\mbox{}
  [$C1_2$
    [$C1$ [a]]
    [$C2$ [b]]
    [$C3$ [c]]
    [$C4$ [d]]
  ]
  [$C2_2$
    [$C5$ [e]]
    [$C6$ [f]]
    [$C7$ [g]]
    [$C8$ [h]]
  ]
]
\end{forest}

\end{document}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

Perhaps the following variation to the example helps to understand the specification:

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{forest} 
where n children=0{font=\sffamily}{},
for tree={
  draw=cyan,
  line width=0.2pt,
  parent anchor=east,
  child anchor=west,
  edge path={
    \noexpand\path[\forestoption{edge}]
      (!u.parent anchor) -- +(0,-13pt) -|   
      (.child anchor)\forestoption{edge label};
  },
  l sep=10pt,
} 
[root
  [$C1_2$
    [$C1$ [a]]
    [$C2$ [b]]
    [$C3$ [c]]
    [$C4$ [d]]
  ]
  [$C2_2$
    [$C5$ [e]]
    [$C6$ [f]]
    [$C7$ [g]]
    [$C8$ [h]]
  ]
]
\end{forest}

\end{document}

enter image description here

In

\noexpand\path[\forestoption{edge}]
  (!u.parent anchor) -- +(0,-13pt) -|   
  (.child anchor)\forestoption{edge label};

!u refers to the current node; so

!u.parent anchor 

starts a path on the declared parent anchor for the current node; then

-- +(0,-13pt)

causes the path to move downwards 13pt and

-| (.child anchor)

makes the path go horizontally and then vertically until it reaches the specified anchor for the child nodes. \forestoption simply access the values for the option used in its argument.

share|improve this answer
    
Thank you! The second half of this is the bit I was really struggling with. This is really extremely helpful. –  cfr Apr 20 at 18:15
    
@cfr You're welcome! –  Gonzalo Medina Apr 20 at 20:42

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.