How do I conditionally include heading name and number in custom ref show rule better?

Hi all,

I wrote a ref show rule to be able to link to strong and emph elements, as I intermittedly use them for subsections. Now, I do have something that seems to work as it should, but it frankly looks like a mess and I have to believe there is a much better way to do this. I tried my best but things like context and query-ing are not really clicking just from reading the reference, so I thought I’d ask if anyone here knows of a better way to write this rule?

I wanted to achieve the following behaviour:

  • If referencing an element that is under the same heading, only display a supplement “section” and its body.

  • If referencing an element that is under a different heading, display the heading supplement and its number as well as “section” and its body.

Here’s an example:

#set text(font: "Source Sans Pro", size: 10pt, lang: "EN")
#set heading(numbering: "1.1", supplement: "Chapter")

= Heading 1

*Subsection 1* <sub-1>

This section has a reference to the following subsection (see @sub-2)

*Subsection 2* <sub-2>

This section has a reference to a subsection outside of its own heading (see @sub-outside)

= Heading 2

*Subsection* <sub-outside>

With result:

Here is what I have now:

#show ref: it => {
  let el = it.element
  if el == none or (el.func() != emph and el.func() != strong) { return it }
  let heading_at = counter(heading).display(at: el.location())
  let heading_here = counter(heading).display(at: here())
  if heading_at != heading_here {
    let heading_name = query(selector(heading).before(el.location())).last().supplement
    return link(el.location(), [#heading_name #heading_at section #el.body])
  }
  link(el.location(), [section #el.body])
}

Any help would be greatly appreciated! Feel free to leave some links behind to reference pages if applicable. Thank you!

I think the code for the references is probably about as good as it gets, dealing with counters and queries is going to be ugly basically no matter what (though you do not have to display the counter, getting their values at the locations suffices).

What I don’t understand is using strong/emph for displaying subsections. Is there a good reason for you to be doing this, beyond easier styling? In general, you should avoid re-inventing the wheel and instead re-style default elements since this allows typst to better understand what is happening in your document and generate more accessible PDFs. See e.g. Accessibility Guide - Typst Documentation

Thanks for your input!

I had a think about your accessibility concerns and you’re right!

My main reason for choosing to use strong/emph for subsections was that it was the first thing I thought of that didn’t have numbering that was easy to style. I primarily used them for level 4 subsections, and I thought 2.3.4.1 would be a bit excessive for a heading number.

I wanted levels 1-3 to have numbers, not onward from there, but that was something I hadn’t figured out how to do yet with headings. Even then, I got greeted with not being able to reference headings without numbering, so I thought this would be a fine solution.

With the accessibility in mind, though, I changed them into level 4 headings and subsequently completely changed my rule to account for them counting as headings, and generally making it a bit more understandable to myself. Also comparing the counters raw rather than comparing the resulting string as you suggested.

With that I think if I wanted this to be written better it’s best to open a new question.
So thank you!

#set text(font: "Source Sans Pro", size: 10pt, lang: "EN")
#show selector.or(
  ..(1, 2, 3).map(n => heading.where(level: n, outlined: true)),
): set heading(numbering: "1.1", supplement: "Chapter")

#show ref: it => {
  let el = it.element
  if el == none or el.func() != heading or (el.func() == heading and el.level != 4) { return it }

  let find_prev_biggest(loc) = {
    let q = query(selector(heading).before(loc)).filter(h => {h.level < el.level})
    if q != () { return q.last() }
    else { return none }
  }

  let prev_here = find_prev_biggest(here())
  let prev_at = find_prev_biggest(el.location())
  if prev_at == none or prev_here == none {
     return link(el.location(), [#lower(el.supplement) "#el.body"])
  }
  let prev_ct_here = counter(heading).at(prev_here.location())
  let prev_ct_at = counter(heading).at(prev_at.location())

  if prev_ct_at == prev_ct_here {
    return link(el.location(), [#lower(el.supplement) "#el.body"])
  }
  let heading_supp = prev_at.supplement
  return link(el.location(), [#heading_supp #numbering(prev_at.numbering, ..prev_ct_at) #lower(el.supplement) "#el.body"])
}

Resulting in

= Heading 1 <h1>

Next heading (see @h2)

==== Inside 1 <sub-1>

Following subsection (see @sub-2)

==== Inside 2 <sub-2>

Subsection further, outside of its own heading (see @sub-outside)

= Heading 2 <h2>

Previous heading (see @h1)

==== Outside <sub-outside>

Subsection earlier, outside of its own heading (see @sub-1)