3

I started using the package forest to create a factor tree (I'm brand new to it. Just looked at another post to get started).

But I am stuck expanding it. Not sure how I would go about.

Current code:

\documentclass{article}
\usepackage{forest}

\begin{document}
\begin{forest}
  [16
    [2
      %[#]%
      %[#]%
    ]
    [8
      [4]
      [2]
    ]
  ]
\end{forest}
\end{document}

Current output:

enter image description here

What I want to finish (expand the 4 and draw a circle around the 2's:

enter image description here

Any help would be appreciated

4
  • 1
    Try adding for tree = {if n children = {0}{circle, draw}{}} to the options of your tree. You might be interested in Q131689. Apr 8, 2023 at 23:13
  • I looked at Q131689, but that just seemed excessive. I believe that automates the entire factor tree. I just want to manually add, don't have too many to make. Not following what you mean by: "for tree = {if n children = {0}{circle, draw}{}}" where would I put that and what would it do? Appreciate it! Apr 8, 2023 at 23:26
  • 1
    Ah, I misunderstood you. Then just replace [4] with [4 [2][2]] and you added the two remaining factors. Apr 8, 2023 at 23:28
  • Thanks so much! That worked. Any recommendation on how to circle a number. If it is too many additional steps then probably not worth it. But if it is a quick thing that would be awesome to have. Apr 8, 2023 at 23:30

1 Answer 1

5

To add the remaining factors of 4 just add two [2] children to the [4]:

[4 [2][2]]

or with formatting

[4
  [2]
  [2]
]

Those are exactly the same.

For the leaves (nodes without children) we can use forests in-built check

if n children = {0}{<true>}{<false>}

This checks if the current node has 0 children and if it does it applies the options <true>, otherwise <false>.

Since you want this to be checked for all nodes automatically, we put this into for tree.


And since you probably want to use this for more than one tree, we package it up in a leaves as circles key so that you only have to add this option as an option to the forest environment. (These options are not between [ and ] as usual in LaTeX.)

I've adjusted the inner sep a bit for these nodes, the default value is a bit too big for circles in my opinion in this case.


The options added to these childless leaves are just normal TikZ node options. This now provides an automatic approach.

However, for each child, you can add TikZ (or forest keys) after the content of the node after a ,:

[2, red]

would add a node with content 2 at the current position in the tree that will be red as if it were \node [red] {2};.

Code

\documentclass{article}
\usepackage{forest}
\forestset{
  leaves as circles/.style={
    for tree={
      if n children = {0}{circle, draw, inner sep=+.15em}{}
    }
  }
}
\begin{document}
\begin{forest} leaves as circles
  [16
    [2]
    [8
      [4
        [2]
        [2]
      ]
      [2]
    ]
  ]
\end{forest}
\end{document}

Output

enter image description here

You must log in to answer this question.

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