7
  • I use forest for the first time and have trouble understanding the (very good) manual.
  • I want to make the tree more compact (see annotation).
  • Which parameter controls that?

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

\begin{document}

\begin{forest} 
    forked edges,   
    for tree = {
        align = center, % needed for "\\" in nodes.
        draw,
        font = \tiny,   
        where level = 0{
        }{
          folder,
          grow' = 0,
        },  
    },
 [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]         
            ]   
        [5.1
            [5.1.1]
            [5.1.2]
            [5.1.3]
            [5.1.4]
            [5.1.5]         
            ]       
        ]
 ]
\end{forest}

\end{document}

enter image description here


Related

1 Answer 1

8

The topmost gap can be shortened using l=something on the children of the root (I have set it to 0.6 of the original l), but note that as we are shortening the distance, we also need to set l sep=0 on the root node.

The other gaps are artefacts of using folder multiple times on each node. The thing is, in the original code, folder resides within two loops, for tree and where level. That's a bad idea even in general, as it uses more time, but in this tree, the effect is visible as well. I usually suggest if level instead of where level in these cases, but here, for tree followed by for descendants seems even more elegant.

\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]         
      ]   
      [5.1
        [5.1.1]
        [5.1.2]
        [5.1.3]
        [5.1.4]
        [5.1.5]         
      ]       
    ]
  ]
\end{forest}

\end{document}

the result of compilation

6
  • Thanks for the good explanation! Mar 19, 2021 at 7:42
  • Is there a way to put 4.1 and 5.1 (which should be named 4.2 :)) side by side so that less vertical space is needed? I can create a new question if that is needed. Mar 19, 2021 at 7:45
  • 2
    There is: we need to make 4 a forked edges (instead of folder) node --- which is a bit tricky to achieve, as I don't think it would work if we first made it a folder and then a forked edges node. And I agree, it is more appropriate if this is a new question. Mar 19, 2021 at 7:51
  • Ok. I go to bed now and will post a question tomorrow. I will post the link here in the comments. Thanks again. Mar 19, 2021 at 7:53
  • FYI, this is the follow-up question: tex.stackexchange.com/questions/588083 Mar 19, 2021 at 17:44

You must log in to answer this question.

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