How to put stuff in its own package?

In this thread How to have a number with blue circle background in text? - #22 by Andrew, @Andrew gave me a piece of code which is working nicely.

When I put this code

#import "@preview/codly:1.3.0": *
#show: codly-init
#codly(
  highlight-fill: _ => none,
  highlight-stroke: _ => none,
  number-format: none,
  highlight-inset: 0pt, // https://github.com/Dherse/codly/issues/81
)

#let raw-count() = counter(figure.where(kind: raw)).get().first()
#let nnum-label(raw-num, n) = label(str(raw-num) + "-" + str(n))

#let nnum(n) = context {
  let label = nnum-label(raw-count(), n)
  let body = strong(text(0.8em, baseline: 0.1em, blue, numbering("⓵", n)))
  link(label, body)
}

#let add-markers(..markers) = context {
  let raw-num = raw-count() + 1
  let markers = for marker in markers.pos() {
    let n = marker.remove("nnum")
    ((..marker, tag: nnum(n), label: nnum-label(raw-num, n)),)
  }
  codly(highlights: markers)
}

#let unique-code-block(body) = context {
  [#figure(body)#label("code" + str(raw-count() + 1))]
}

into a separate file and create an example where I simply import it

#import "@local/raw-text:0.1.0": *

#add-markers((line: 2, start: 4, end: 4, nnum: 1), (line: 2, start: 8, nnum: 2))
#unique-code-block(```
 something
   more of this is great
 what is that?
```)

/ #nnum(1): first
/ #nnum(2): second

and compile I get

error: label `<1-1>` does not exist in the document
   ┌─ @local/raw-text:0.1.0/raw-text.typ:16:2
   │
16 │   link(label, body)
   │   ^^^^^^^^^^^^^^^^^

error: label `<1-2>` does not exist in the document
   ┌─ @local/raw-text:0.1.0/raw-text.typ:16:2
   │
16 │   link(label, body)
   │   ^^^^^^^^^^^^^^^^^

As a newbie I didn’t find the cause.

If you import from a package, it will only return symbols, but not any of the content. So, the codly-init is missing.

You need to move codly’s initialization to your main file, or import it like a template (show: it => {...; it}).

2 Likes

Thanks a lot @quachpas

Changing it like this

#import "@preview/codly:1.3.0": *

#let raw-text-init(body) = {
  show: codly-init
  codly(
    highlight-fill: _ => none,
    highlight-stroke: _ => none,
    number-format: none,
    highlight-inset: 0pt, // https://github.com/Dherse/codly/issues/81
  )
  body
}

#let raw-count() = counter(figure.where(kind: raw)).get().first()
#let nnum-label(raw-num, n) = label(str(raw-num) + "-" + str(n))

#let nnum(n) = context {
  let label = nnum-label(raw-count(), n)
  let body = strong(text(0.8em, baseline: 0.1em, blue, numbering("⓵", n)))
  link(label, body)
}

#let add-markers(..markers) = context {
  let raw-num = raw-count() + 1
  let markers = for marker in markers.pos() {
    let n = marker.remove("nnum")
    ((..marker, tag: nnum(n), label: nnum-label(raw-num, n)),)
  }
  codly(highlights: markers)
}

#let unique-code-block(body) = context {
  [#figure(body)#label("code" + str(raw-count() + 1))]
}

and adding

#show: raw-text-init

to my test file makes it work.

1 Like