How to draw tree diagrams in Typst?

hi there,
I’m fairly new to typst (I have been using Latex before), and in latex i could create trees using the forest package. Is there any way to create trees in typst? From what i could find none of the packages do exactly what i need.
In latex, by writing this in the preamble:

\usepackage[edges]{forest}
\forestset{
	mydefault/.style={
		for tree={
			draw,
			circle,
			minimum size=1cm,
			inner sep=2pt,
			s sep=1cm,
			l=1.5cm
		}
	}
}

and this in the code:

\begin{forest}
                mydefault
                [2 [1] [4 [3] [5]]]
\end{forest}

It would create this:

Hey @Michael_Ronin, welcome to the forum! I’ve changed your question post’s title and added a tag to better fit our guidelines: How to post in the Questions category

For future posts, make sure your title is a question you’d ask to a friend about Typst. :wink:

cetz has a tree example

#import "@preview/cetz:0.3.4": canvas, draw, tree

#set page(width: auto, height: auto, margin: .5cm)

#let data = (
  [A], ([B], [C], [D]), ([E], [F])
)

#canvas({
  import draw: *

  set-style(content: (padding: .2),
    fill: gray.lighten(70%),
    stroke: gray.lighten(70%))

  tree.tree(data, spread: 2.5, grow: 1.5, draw-node: (node, ..) => {
    circle((), radius: .45, stroke: none)
    content((), node.content)
  }, draw-edge: (from, to, ..) => {
    line((a: from, number: .6, b: to),
         (a: to, number: .6, b: from), mark: (end: ">"))
  }, name: "tree")

  // Draw a "custom" connection between two nodes
  let (a, b) = ("tree.0-0-1", "tree.0-1-0",)
  line((a, .6, b), (b, .6, a), mark: (end: ">", start: ">"))
})

3 Likes