How do outlines and heading labels interact?

I was trying to make a document with an outline as well as headings with labels, and links to those headings. Minimal example:

#outline()
#link(<Foo>)[Go to Foo]
#heading[Foo <Foo>]

However, this naive construction leads to the following error:

error: label `<Foo>` occurs multiple times in the document
  ┌─ main.typ:2:1
  │
2 │ #link(<Foo>)[Foo]
  │  ^^^^^^^^^^^^^^^^

It doesn’t matter what the label is (e.g. heading Foo with the label <Bar> complains that the label <Bar> appears multiple times). Removing either the call to outline or the call to link resolves the error, however I conceptually need both.

What’s the usual workaround for this? In my actual use-case, I’m generating the headers/labels from input data. This seems like a fairly common edge-case to hit, but after a few hours of searching docs and forums I haven’t found anyone else mentioning this issue. I can, but would rather not, generate the outline manually.

Hello!

In your example the label is part of the heading, so indeed it appears both where the heading is located and as part of the outline, hence typst complains. You can fix this by attaching the label to the heading itself, instead of being part of the heading:

#outline()
#link(<Foo>)[Go to Foo]
#heading[Foo]<Foo>

or even:

#outline()
#link(<Foo>)[Go to Foo]
= Foo <Foo>

Ah, what an obvious mistake in retrospect. I was misled by the shorthand syntax = Foo <Foo>, mistakenly assuming that desugared to #heading(depth: 1)[Foo <Foo>], misreading the syntax section on the label function as relates to headings. Thanks for the clarification!