Automatic heading referencing with its text

Hey there, so, I’ve seen the two posts on automatic headings, yes they did not fulfill what I needed fully, so here I am posting : D

I have:

= Chapter 1
== Definition
== Lemma
===
some statemenet
===
statement 2

and the numbering convection is 1.1.i.a, so when I type e.g.

@1.2

I want it to render as:

Lemma 1.2, not as 1.2 (which I currently have).

here is the code (it is inside the template hence no shebangs)

  // Make a key like "1.13" from a numbering tuple (1, 19)
  let make-number-key = nums => {
    nums.map(str).join(".")
  }

  // Extract first word of heading text, e.g. "Lemma" / "Definition"
  let heading-kind = it => {
    let text = {
    if it.has("text") and type(it.text) == "string" {
        it.text
      } else if it.has("children") {
        it.children
          .map(c => if c.has("text") and type(c.text) == "string" { c.text } else { "" })
          .join("")
      } else {
        ""
      }
    }
    text = text.replace("\n", " ").trim()
    if text == "" {
      // "Section" // this was removed to inspect its effect
    } else {
      text.split(" ").at(0)
    }
  }

  show heading: it => context {
    // current heading numbering, e.g. (1, 19)
    let nums = counter(heading).get()
    let key = make-number-key(nums)
  
    // this will be "Lemma", "Definition", etc.
      let kind = heading-kind(it.body)
    
      // attach a figure so the heading becomes referenceable
      let fig = figure(
        kind: "heading",
        numbering: (..numbers) => numbering(heading-numbering, ..nums),
        supplement: kind,  // used if you manually show this figure
      )[]
    
      [
        #it
        #v(-1em)
        #fig
        #label(key)
      ]
    }
    
    
    show ref: it => {
      let el = it.element
      if el == none {
        // element not discovered yet or wrong label: fall back
        return it
      }
    
      // We only change references that point to headings
      if el.func() != heading {
        return it
      }
    
      // Extract first word of heading for the supplement
      let kind = heading-kind(el)
    
      // Display: "<Kind> <number>", e.g. "Lemma 1.19"
      link(
        el.location(),
        [
          #kind #space()
          #numbering(el.numbering, ..counter(heading).at(el.location()))
        ],
      )
    }
    
 

Your post is incomplete and incorrect, your question is easy:

let kind = it.body + heading-kind(it.body)
#let heading-numbering = "1.1"
#set heading(numbering:heading-numbering)

// Make a key like "1.13" from a numbering tuple (1, 19)
#let make-number-key = nums => { nums.map(str).join(".") }

// Extract first word of heading text, e.g. "Lemma" / "Definition"
#let heading-kind = it => {
    let text = {
      if it.has("text") and type(it.text) == "string" {it.text}
      else if it.has("children") {it.children
        .map(c =>
          if c.has("text") and type(c.text) == "string" { c.text }
          else { "" })
        .join("")} 
      else {""}}

    if type(text) == type(none) {text = " "}
    else {text = text.replace("\n", " ").trim()}

    if text == "" {}
      // "Section" // this was removed to inspect its effect}
    else {text.split(" ").at(0)}}

#show heading: it => context {
  // current heading numbering, e.g. (1, 19)
  let nums = counter(heading).get()
  let key = make-number-key(nums)

  // this will be "Lemma", "Definition", etc.
  let kind = it.body + heading-kind(it.body)
  
  // attach a figure so the heading becomes referenceable
  let fig = figure(
    kind: "heading",
    numbering: (..numbers) => numbering(heading-numbering, ..nums),
    supplement: kind,)[]  // used if you manually show this figure
  
  [#it
  #v(-1em)
  #fig
  #label(key)]}
    
#show ref: it => {
  let el = it.element

  if el == none {return it}
  // element not discovered yet or wrong label: fall back

  // We only change references that point to headings
  if el.func() != heading {return it}

  // Extract first word of heading for the supplement
  let kind = heading-kind(el)

  // Display: "<Kind> <number>", e.g. "Lemma 1.19"
  link(el.location())[
    //#kind #space()
    //#numbering(el.numbering, ..counter(heading).at(el.location()))
    ]}

#show link: x => text(blue, x)
#show ref: x => text(green, x)
= Chapter 1
== Definition
== Lemma
===
some statemenet
===
statement 2\
@1\
@1.2\
@1.2.2
1 Like

Hi. Make sure you read the pinned How to post in the Questions category topic on how to format code blocks correctly, for your next post.

1 Like