How can I link to labels if the labels are created dynamically?

I’m gonna give a quick overview of the context, so it’s easier to see the issue.
I have a simple project in the root directory there are 3 folders and the main typst file. In folder called modules there is misc.typ that looks like this:

#let capitalize_first(s) = s.replace(s, upper(s.first())+s.trim(s.first()), )

#let heading(label_name, text) = [
    \
    #label(label_name)
    === #text
    \
]

#let sort-section(name) = [
  #let label_name = name+"-sort"
  #label(label_name)
  === #capitalize_first(name) Sort
  #let filename = "../sorts/" + name + ".typ"
  #include filename
]

#let contents-point(name, width) = [
  #let label_name = name+"-sort"
  #let point_content = capitalize_first(name) + " Sort"
  #h(width*10pt) -- #link(label_name)[#point_content] \
]

And in the main file I have something like this:

#import "modules/misc.typ" as misc

#set text(font: "FiraCode Nerd Font", size: 10pt)

#let sort-names = (
  "bogo",
  "quick",
  "merge",
  "heap",
  "insert",
  "bubble",
  "select",
  "counting",
  "radix",
  "shell",
  "tim",
)

= Basic Algorithms
\
Contents:\
\
#link(<sorting-algorithms>)[Sorting Algorithms] \
#for name in sort-names {
  misc.contents-point(name, 1) 
}

However the links don’t work, I think it’s because the #link is not dynamic.
Almost forgot, but the labels for the links are also made kind of using the misc.typ file function but when i hard coded it in the main file like this:

#link(<sorting-algorithms>)[Sorting Algorithms] \
  #h(10pt) -- #link(<bogo-sort>)[Bogo Sort] \
  #h(10pt) -- #link(<quick-sort>)[Quick Sort] \
  #h(10pt) -- #link(<merge-sort>)[Merge Sort] \
  #h(10pt) -- #link(<heap-sort>)[Heap Sort] \
  #h(10pt) -- #link(<insert-sort>)[Insert Sort] \
  #h(10pt) -- #link(<bubble-sort>)[Bubble Sort] \
  #h(10pt) -- #link(<select-sort>)[Select Sort] \
  #h(10pt) -- #link(<counting-sort>)[Counting Sort] \
  #h(10pt) -- #link(<radix-sort>)[Radix Sort] \
  #h(10pt) -- #link(<shell-sort>)[Shell Sort] \
  #h(10pt) -- #link(<tim-sort>)[Tim Sort] \

everything worked. Is there some sort of other workaround? Some other function?

Hi @Mathew, welcome to the forum! I have formatted your code, but could you maybe try to revise your post’s title to be a complete question as per the question guidelines:

Good titles are questions you would ask your friend about Typst.

A good title makes the topic easier to grasp, increasing your chances of getting a good answer quickly. We also hope by adhering to this, we make the information in this forum easy to find in the future.

It also seems that a lot of your code (for example, but not exclusively, sort-section) is not relevant to your question. Please try to reduce your code to the minimum necessary to show the problematic behavior, that will help us a lot as well.

Thanks!

You are just using the label name here instead of actually creating the label? In your hard-coded version, the links contain the labels.

Thanks, I’m new here so please excuse a bit of my silliness. I appreciate the correction greatly.

1 Like

Thanks I see the issue now. I thought that if I create the label inside contents-point it would somehow mess up where it points to and just wanted to refer to the label that is later created in the main file, but that doesn’t seem to be an issue.