8

I can easily do trees with horizontal (square) edges in tikz-qtree (see MWE). The forest package is however a far superior alternative for my purposes. However, every attempt to do either horizontal or vertical trees with square edges in forest is either ignored, or leads to compilation failure (for example inserting the \tikzset line into the forest tree). Is this possible?

A helping hand would be very much appreciated here.

 \documentclass[10pt,twoside,a4paper]{memoir}
 \usepackage{graphicx}
 \usepackage{forest}
 \usepackage{tikz-qtree}

 \begin{document}

 \begin{tikzpicture}
 \tikzset{edge from parent/.style={draw,edge from parent path={(\tikzparentnode.south)-- +(0,-8pt)-| (\tikzchildnode)}}}
 \Tree [.ZZ
 [.Bax
 [.X
 [.Y [.A ] [.B ] ]
 [.Z [.C ] [.D ] ] ]
 [.F
 [.M [.E ] [.F ] ]
 [.G [.G ] [.H ] ] ] ]
 [.A
 [.B
 [.S  [.I P R T V U ] [.J ] ]
 [.I  [.K ] [.L ] ] ]
 [.M
 [.L  [.M ] [.N ] ]
 [.A  [.O ] [.P ] ] ] ] ] ]
 \end{tikzpicture}


 \begin{forest}
 [ZZ
 [Bax
 [X
 [Y [A ] [B ] ]
 [Z [C ] [D ] ] ]
 [F
 [M [E ] [F ] ]
 [G [G ] [H ] ] ] ]
 [A
 [B
 [S  [I [P][R][T][V][U]] [J ] ]
 [I  [K ] [L ] ] ]
 [M
 [L  [M ] [.N ] ]
 [A  [O ] [P ] ] ] ] ] ]
 \end{forest}

 \end{document}

MWE Output

  • Given the age of this question and the changes in forest you might want to accept the new answer over the existing accepted answer. – Alan Munn Jul 5 '17 at 23:07
14

You need to do \begin{forest} for tree={edge path=<your path>}} (without []!).

I adjusted the distance a little bit, better would be to use for example half the level distance and not a fixed distance.

One can also use my paths.ortho library (needs both tikzlibarypaths.ortho.code.tex and tikzlibarypaths.ortho.tex) and be available to use |-| instead of --.

Code

\documentclass[10pt,twoside,a4paper]{memoir}
\usepackage{graphicx}
\usepackage{forest}
\usepackage{tikz-qtree}
\begin{document}

\begin{tikzpicture}
\tikzset{edge from parent/.style={draw,edge from parent path={(\tikzparentnode.south)-- +(0,-8pt)-| (\tikzchildnode)}}}
\Tree [.ZZ
[.Bax
[.X
[.Y [.A ] [.B ] ]
[.Z [.C ] [.D ] ] ]
[.F
[.M [.E ] [.F ] ]
[.G [.G ] [.H ] ] ] ]
[.A
[.B
[.S  [.I P R T V U ] [.J ] ]
[.I  [.K ] [.L ] ] ]
[.M
[.L  [.M ] [.N ] ]
[.A  [.O ] [.P ] ] ] ] ] ]
\end{tikzpicture}


\begin{forest} for tree={
    edge path={\noexpand\path[\forestoption{edge}] (\forestOve{\forestove{@parent}}{name}.parent anchor) -- +(0,-12pt)-| (\forestove{name}.child anchor)\forestoption{edge label};}
}
[ZZ
[Bax
[X
[Y [A ] [B ] ]
[Z [C ] [D ] ] ]
[F
[M [E ] [F ] ]
[G [G ] [H ] ] ] ]
[A
[B
[S  [I [P][R][T][V][U]] [J ] ]
[I  [K ] [L ] ] ]
[M
[L  [M ] [.N ] ]
[A  [O ] [P ] ] ] ] ] ]
\end{forest}

\end{document}

Output

enter image description here

  • \forestove etc. shouldn't be used. Instead, the provided wrappers should be used. The internal stuff is liable to break on update as part of the internal implementation. The wrappers provide a user interface which is much more likely to survive changes to Forest's innards. – cfr Jul 5 '17 at 22:46
4

With current Forest, it is a simple question of loading the edges library and adding forked edges to the tree's preamble.

\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
  forked edges,
 [ZZ
 [Bax
 [X
 [Y [A ] [B ] ]
 [Z [C ] [D ] ] ]
 [F
 [M [E ] [F ] ]
 [G [G ] [H ] ] ] ]
 [A
 [B
 [S  [I [P][R][T][V][U]] [J ] ]
 [I  [K ] [L ] ] ]
 [M
 [L  [M ] [.N ] ]
 [A  [O ] [P ] ] ] ] ] ]
\end{forest}
\end{document}

squared edges in Forest

If you have only version 1 of Forest, this won't work. In that case, you should update. If that's not possible, note that macros such as \forestove are intended as part of the internal implementation and should be avoided in end-user code as they are liable to break on update. Instead, use the wrappers which Forest provides for this purpose, which are much more likely to survive any changes to Forest's innards.

\documentclass[border=10pt]{standalone}
\usepackage{forest}% version 1
\begin{document}
\begin{forest}
  for tree={
    parent anchor=south,
    edge path={
      \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- ++(0,-5pt) -| (.child anchor)\forestoption{edge label};
    }
  }
 [ZZ
 [Bax
 [X
 [Y [A ] [B ] ]
 [Z [C ] [D ] ] ]
 [F
 [M [E ] [F ] ]
 [G [G ] [H ] ] ] ]
 [A
 [B
 [S  [I [P][R][T][V][U]] [J ] ]
 [I  [K ] [L ] ] ]
 [M
 [L  [M ] [N ] ]
 [A  [O ] [P ] ] ] ] ] ]
\end{forest}
\end{document}

older Forest

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.