3

Is it possible to mix grow directions in forest in tikz?

I want to obtain something like

enter image description here

using forest.

So after the 2 first levels, I want to start 'listing' the items (or growing east) instead of downwards/south.

MWE:

\documentclass[border=10pt]{standalone}

\usepackage{forest}

\begin{document}

\begin{forest}
[Root
    [A
        [A1]
        [A2]
    ]
    [B
        [B1]
        [B2]
    ]
    [C
        [C1]
        [C2]
    ]
]
\end{forest}

\end{document}
1
  • What version of Forest do you have?
    – cfr
    Jun 14 '16 at 0:49
6

Sure. I would tend to square the edges of the first children for consistency, although this is not required. (In fact, it is much easier not to with the current version of Forest because forked edges and folder do not get along at all well together!)

Note that this requires version 2 of Forest. You can do the same in version 1 but it is considerably more complex.

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
  where level=1{
    child anchor=north,
    !u.parent anchor=south,
    before computing xy={
      l*=.5,
    },
    if={n==(int((n_children("!u")+1)/2))}{
      calign with current edge
    }{},
    edge path'={(!u.parent anchor) -- ++(0,-5pt) -| (.child anchor)},
    for tree={
      folder,
      grow'=0
    }
  }{},
  for tree={
    edge={thick},
  },
  before typesetting nodes={
    tempcounta'/.max={level}{root,tree},
    for tree={
      rounded corners,
      top color/.wrap pgfmath arg={blue!#1!cyan}{100*((level())/((tempcounta)+1))},
      +edge/.wrap pgfmath arg={blue!#1!cyan}{100*((level())/((tempcounta)+1))},
      bottom color/.wrap pgfmath arg={blue!#1!cyan}{100*((level()+1)/((tempcounta)+1))},
      draw/.wrap pgfmath arg={blue!#1!cyan}{100*((level()+1)/((tempcounta)+1))},
      thick,
    }
  }
  [Root
    [A
        [A1]
        [A2]
    ]
    [B
        [B1
          [a]
          [b]
        ]
        [B2]
    ]
    [C
        [C1]
        [C2]
    ]
  ]
\end{forest}
\end{document}

The colouring is just for fun.

forkish edges with folder below

Forest 1

If you only have version 1 of Forest and cannot update, the above code will give compilation errors. The following is somewhat less flexible, but should work even with the older Forest. (At least, it works for me.)

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
    for tree={
    if level=0{}{
      if level=1{
        tier=spread,
        before typesetting nodes={
          prepend={[, phantom]}
        },
      }{
        child anchor=west,
        edge path={
          \noexpand\path [\forestoption{edge}] (!to tier=spread.south west) +(2.5pt,0) |- (.child anchor)\forestoption{edge label};
        }
      },
    },
  },
  before packing={
    where level=2{
      if={n_children("!u")>1}{
        if n=1{
          grow=0,
        }{
          !u1.append=!n
        }
      }{}
    }{}
  },
  where level=1{
    child anchor=north,
    !u.parent anchor=south,
    if={n==(int((n_children("!u")+1)/2))}{
      calign with current edge
    }{},
    edge path={
          \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- ++(0,-5pt) -| (.child anchor)\forestoption{edge label};},
  }{},
  for tree={
    rounded corners,
    top color=white,
    edge={thick, blue},
    bottom color=blue,
    thick,
    draw=blue,
  }
  [Root
    [A
        [A1]
        [A2]
    ]
    [B
        [B1]
        [B2]
    ]
    [C
        [C1]
        [C2]
    ]
  ]
\end{forest}
\end{document}

version 1 version

2
  • Looks good! But it appears that I might have a different forest version? I am using ShareLaTeX, and I get the errors: Package pgfkeys Error: I do not know the key '/forest/package@options/edges' and I am going to ignore it. Perhaps you misspelled it. and Package pgfkeys Error: I do not know the key '/forest/tempcounta'/.max', to which you passed '{level}{root,tree}', and I am going to ignore it. Perhaps you misspelled it.
    – Jamgreen
    Jun 14 '16 at 8:07
  • @Jamgreen Please see edit above. The code for current Forest is nicer and more flexible, but the same kind of effect can be produced with earlier Forest, albeit not quite as robustly. For example, I removed the additional level from this example because it was not in your MWE and would introduce additional complexity. (That is, you could adapt this for another level, but it won't work out-of-the-box.)
    – cfr
    Jun 14 '16 at 12:22

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.