3
  • This is a follow-up to forest: How to influence length of edge (make the tree more compact).
  • The main question is, how to rearrange the branch "4.2" to save vertical space. Ideally, the branch "4.2" is flipped vertically (other ideas are welcome). (see green annotation)
  • Small additional question: Can I locally (for a specific branch) increase the separation to the neighbor branch? (see red annotation)

\documentclass{article}
\usepackage[edges]{forest}

\begin{document}

\begin{forest} 
  forked edges,
  l sep = 0, for children = {l*= 0.6},
  for tree = {
    align = center, % needed for "\\" in nodes.
    draw,
    font = \tiny,   
  },
  for descendants={
    grow' = 0,
    folder,
  },
  [0
    [1]
    [\ldots]
    [2
      [2.1]
      [2.2]
      [\ldots]
    ]
    [3
      [3.1]
      [3.2]       
    ]
    [4
      [4.1
        [4.1.1]
        [4.1.2]
        [4.1.3]
        [4.1.4]
        [4.1.5]         
      ]   
      [4.2
        [4.2.1]
        [4.2.2]
        [4.2.3]
        [4.2.4]
        [4.2.5]         
      ]       
    ]
  ]
\end{forest}

\end{document}

enter image description here

1 Answer 1

1

Not the prettiest code ever, but it does the job.

OP's "green" question can be addressed by making "4" a forked node. The problem in doing so is practical. The first shot at it might be to say that all the descendants of the root should be folder nodes, and then to override this by setting forked edges to node "4". However, this will not work, because folder style does lots of stuff which forked edge(s) will not override (and which is, in fact, hard to override in general). Therefore, the idea is to use a complex nodewalk which sets folder to all nodes except the root and its fifth child (node "4"). I have tried to make this (relatively) easy and general by defining the (nodewalk) style subtract.

Even this way, not all comes out perfectly. The node "4" which is both a child and a parent with a forked edge must be addressed manually. This is so as OP made the default distance between the parent and the children of the forked edges a bit shorter than the default (l*=0.6). This shortening makes sense as most of the forked edge children are folder parents, but it needs to be adjusted for node "4".

The "red" issue is easier to address. We shift "s" coordinates of some manually (after packing, but before computing xy). (This will be even easier to achieve in the next version of Forest, which will contain new keys for setting some additional s-space between individual nodes.)

\documentclass{article}
\usepackage[edges]{forest}

\forestset{
  for nodewalk={
    subtract/.style 2 args={
      save={@exceptions}{#2},
      Nodewalk/.process=__Rw {}{#1}{every step}{if in saved nodewalk={current}{@exceptions}{}{##1}}
    },
  }{},
}

\begin{document}

\begin{forest}
  for tree = {   % this applies to the entire tree
    align = center, % needed for "\\" in nodes.
    draw,
    font = \tiny,   
  },
  my forked nodes/.nodewalk style={current,5}, % for generality, if we decide to have more forked nodes
  for nodewalk={my forked nodes}{  % this applies to the parents of the forked edge nodes
    l sep = 0,
    for children = {
      l*= 0.6,
      forked edge
    },
  },
  for nodewalk={subtract={descendants}{my forked nodes}}{ % all other nodes are folder nodes
  %%% for nodewalk={   % in the absence of "subtract", this would work just as well
  %%%   for nodewalk'={1,descendantas},
  %%%   for nodewalk'={2,descendants},
  %%%   for nodewalk'={3,descendants},
  %%%   for nodewalk'={4,descendants},
  %%%   for nodewalk'={fake=5,descendants},
  %%% }{
    grow' = 0,
    folder,
  },
  % manual correction at the crossroads (node "4") of forked edge and folder ...
  for nodewalk={my forked nodes}{l*=1.47},
  % Increase the horizontal separation before "3" and "4"
  before computing xy={
    for nodewalk={4,next}{
      for current and following siblings={s+=0.5em}
    }
  },
  [0
    [1]
    [\ldots]
    [2
      [2.1]
      [2.2]
      [\ldots]
    ]
    [3
      [3.1]
      [3.2]       
    ]
    [4
      [4.1
        [4.1.1]
        [4.1.2]
        [4.1.3]
        [4.1.4]
        [4.1.5]         
      ]   
      [4.2
        [4.2.1]
        [4.2.2]
        [4.2.3]
        [4.2.4]
        [4.2.5]         
      ]       
    ]
  ]
\end{forest}

\end{document}

the result of compilation

2
  • Thanks. Also thanks for all the comments in the code. Looking forward to the next version of forest. Mar 22, 2021 at 14:48
  • The forest package is really impressive. It must be difficult to create such a flexible tool that is still usable at the same time. Mar 22, 2021 at 15:07

You must log in to answer this question.

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