How to add color box to a non-citation reference?

Hello everyone. Newbie here, coming from LaTeX.

I am currently working on a template, but I am looking to implement color boxes around hyperlinks (cross-references, citations, urls and so on). What I’ve seen is just a discussion on GitHub, but is limited to cross-references and links, however, there are some issues.

When using the numbering for headings and equations shown below, those are rendered with the dot and the parentheses, respectively. Such aspect does not happen with figures and tables, but as mentioned before, with citations are not possible.

Headings
set heading(numbering: "1.")
set math.equation(numbering: "(1)")

I am looking for a (LaTeX) hyperref-like behaviour that may be able to work with all hyperlinks and render correctly. The code that I am using is the same that was shared in the discussion, just that I remove the colored text. I’ve tried to convert supp and num to string, so I could strip dots and parentheses, but the second case is from a context type and couldn’t find how to do it. I would appreciate help with all that.

Code for hyperlinks
#let separate-supplement-style(supp, num) = {
  box(supp + " " + num, stroke: 1pt + green, outset: (bottom:1.5pt, x:.5pt, y:.5pt))
}

#show ref: it => {
  let (element, target, supplement: supp) = it.fields()
  if element == none {
    return it
  }
  let non_cite_ref = element.fields()
  let supp = if supp == auto { non_cite_ref.supplement } else { supp }
  let num = context {
    let head-count = counter(heading).at(target)
    numbering(non_cite_ref.numbering, ..head-count)
  }
  link(target, separate-supplement-style(supp, num))
}

Hello. If you want to reference correct numbering for all elements, then you can just do:

#show ref: it => {
  // Skip bibliography citations.
  if it.element == none { return it }
  box(stroke: 1pt + green, outset: (bottom: 1.5pt, rest: .5pt), it)
}

#let bib = ```yaml
cite:
  type: article
  author: Author
  title: Title
```.text

#set heading(numbering: "1.")
#set math.equation(numbering: "(1)")

= Heading <heading>
#figure([], caption: []) <figure>
#figure(table(), caption: []) <table>
$ x $ <equation>

@heading

@figure

@table

@equation

@cite

#bibliography(bytes(bib))

Your code uses heading counter for all elements which is semantically incorrect.

3 Likes

Thank you very much, the code you provided is much better and cleaner.

1 Like

I think title should be “How to add color box to a non-citation reference?”

Solution doesn’t color hyperlink, it colors a box, and box is for non-citation references specifically, not for hyperlinks.

Done. I changed the title to match the solution. Thanks.

1 Like