How to use codelst or codly inside fletcher nodes?

I’m trying to generate a figure that illustrates call paths in a complex piece of code. The way I imagined doing this, was using fletcher nodes containing code snippets (using raw text blocks, e.g. c .... ) with relevant information - such as function calls. The call graph would then be represented with arrows between such nodes.

For instance:

#import "@preview/fletcher:0.5.3" as fletcher: diagram, node, edge
#import "@preview/codelst:2.0.2": sourcecode

#diagram(node-stroke: 1pt, edge-stroke:1pt, node-shape: rect,
 {
      node(sourcecode[```c
         void caller() {
              call_foo();
              ...
         }
     ```])
   edge("-|>")
      node(sourcecode[```c
         void call_foo() {
              ...
         }
     ```])
 })

Unfortunately the fletcher nodes render as if the code block has a very narrow width and the node outline doesn’t actually include its content.

I tried something similar with codly with similar results.

So now for the questions:

  • am I doing something obviously wrong?
  • should I be using an entirely different approach? (I’m actually trying to convert diagrams made with a combination of d2 and plantuml)

Thanks for any pointers!

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

Make sure your title is a question you’d ask to a friend about Typst. :wink:

Replying to myself in case someone else wonders about the same thing.
One simple workaround is to simply set a width on the node.

Codelst is partially to blame here, because it wraps raw blocks in a zero-width container:

#let a = sourcecode[```c
void call_foo() {
    ...
}
```]

#context assert(measure(a).width == 0pt)

You can get around that by explicitly setting node widths, or by wrapping sourcecode blocks in a block of fixed width, etc. For example:

node(width: 5cm, sourcecode[```c
void caller() {
  call_foo();
  ...
}
```])

Alternatively, if you don’t need line numbers:

#import "@preview/fletcher:0.5.3" as fletcher: diagram, node, edge

#diagram(
  node-stroke: 1pt,
  edge-stroke: 1pt,
  node-shape: rect,
  {
  node(```c
  void caller() {
    call_foo();
    ...
  }
  ```)
  edge("-|>")
  node((1, 0), ```c
  void call_foo() {
    ...
  }
  ```)
})
1 Like