quartocross-referencetypst

How to refer to section in a Quarto typst document by name instead of number?


The following code in an *.qmd leads to referencing by number:

---
format: typst
section-numbering: "1"
---

# First chp

A link to @sec-chp2

# Second chp {#sec-chp2}

Result:
referencing uses the words Section 2

How can I change the code so that I get a reference saying "A link to chapter 2 Second chp" instead of "A link to Section 2"?


Solution

  • You can define your own show rule for ref:

    ---
    format:   
      typst:     
        include-before-body:        
          - text: |      
              #set heading(numbering: "1.1", supplement: [chapter])
    
              #show ref: it => if it.element.func() != heading { it } else {
                let h = it.element // heading
                let c = context {
                  let n = numbering("1.1", ..counter(heading).at(h.location())) // numbering
                  return h.supplement + " " + n + " " + h.body
                }
                link(it.target, c)
              }
    ---
    
    # First chp {#sec-chp1}
    
    A link to @sec-chp2
    
    # Second chp {#sec-chp2}
    
    A link to @sec-chp2-sub1
    
    ## First subchp of second chp {#sec-chp2-sub1}
    
    A link @sec-chp1
    

    enter image description here