How to have a number with blue circle background in text?

I tried this

#let bluenum(num) = {
    highlight(fill: blue, radius: 9pt, extent: 7pt,)[#num]
}

#let bnum1 = { bluenum(1) }

This is very interesting #bnum1

But this isn’t exactly a circle. :grin: What’s the best way to have this with a real circle?

Hi there, something like this?

This is more #box(circle(fill: blue, "1"))

Thanks a lot. I tried #circle but missed #box.

So this looks now quite ok

#let cnum(num) = {
  box(height: 0.9em, circle(fill: blue, [#set align(center + horizon)
      #num]))
}

#let cnum1 = { cnum(1) }

This is very interesting #cnum1

Using unicode characters also seem to be a possibility

#let nnum1 = { strong(text(fill:blue, [❶])) }

This is very interesting #nnum1

I got curious if it was possible to generalize using Unicode characters and turns out it is:

#let nnum(num) = {
  if num < 1 or num > 10 { panic("Given number must be between 1 and 10 (inclusive)") }
  let numId = "❶".to-unicode() + (num - 1)
  strong(text(fill:blue, str.from-unicode(numId)))
}

Edit: vmartel08 pointed out that numbering already has the capability to display numbers this way, so this function is unnecessary.

Lots of symbols are actually available for automatic numbering. Numbering Function – Typst Documentation

Counting symbols are 1, a, A, i, I, α, Α, , , , , , , א, , , *, ١, ۱, , , , , and . They are replaced by the number in the sequence, preserving the original case.

2 Likes

You don’t need curly braces in let assignment. text.fill and text.size can be passed without name.

#let nnum1 = text(blue)[*❶*]
#let nnum1 = strong(text(blue)[❶])

This is very interesting #nnum1

Ah, thanks. Didn’t know. Is this specifically when I call built in functions?

Thanks for this functions.

numbering is indeed interesting.

I am thinking if it would be possible to combine numbering with the function @gezepi gave. Then one could invoke that function without a parameter and
“❶” would get incremented automatically when that function would be called again.

#let nnum(num) = {
  strong(text(blue, numbering("①", num)))
  strong(text(blue, numbering("⓵", num)))
}

#range(51).map(i => nnum(i + 1)).map(list.item).join()

Automatic counting:

#set page(height: auto)

#let nnum() = {
  let counter = counter("blue-number")
  counter.step()
  context {
    strong(text(blue, counter.display("①")))
    strong(text(blue, counter.display("⓵")))
  }
}

#for _ in range(51) [- #nnum()]
output

2 Likes

What exactly do you refer to?

Actually I want to have some text organized like this

image

where I can add additional information to 1 and 2. below the given text.

Here it would perhaps be best if I call a function with a number parameter like for example #cnum(1) and #cnum(2)

But then I got curious (due to @vmartel08 reference to counting) how to be able to do automatical counting.

1 Like

Or perhaps in better words: I like to have a verbatim block where I can include such numbers. Later I want to refer to those numbers

How do you want to refer to them later?

Maybe you should create a new general question for that.

When I use #nnum(1) aso (from your first example) I later can refer to them again with #nnum(1) aso.

Somehow like this:

#text(font: "FiraCode Nerd Font")[
NAME    TYPE       SIZE USED PRIO\
/dev/zram0#nnum(1) partition 30,6G 4,2G  100#nnum(2)
]

/ #nnum(1): name of the swap device
/ #nnum(2): swap priority

What if you have 2 code blocks with blue circles?

Codly highlights + auto counter solution
#import "@preview/codly:1.3.0": *
#show: codly-init

#set page(height: auto)

#let ncounter = counter("blue-number")
#let nnum() = {
  ncounter.step()
  context strong(text(blue, ncounter.display("①")))
}
#show raw.where(block: true): it => it + ncounter.update(0)

#codly(
  highlight-fill: _ => none,
  highlight-stroke: _ => none,
  highlights: (
    (line: 2, tag: nnum(), label: <a>),
    (line: 4, tag: nnum(), label: <b>),
  ),
)

#figure(```
ciea
astehasi

stehasi
ieasti
```) <code>

@code:2 & @code:4

or

@a & @b

For multiple numbers per line with latest version
#codly(
  highlight-fill: _ => none,
  highlight-stroke: _ => none,
  highlight-inset: 0pt, // https://github.com/Dherse/codly/issues/81
  highlights: (
    (line: 2, start: 4, end: 4, tag: nnum(), label: <a>),
    (line: 2, start: 8, tag: nnum(), label: <b>),
  ),
)

#figure(```
code
this that
```) <code>

@a, @b

codly is interesting. Thanks.

When having more than one code block one could use

#let nnum(num) = {
  strong(text(blue, numbering("⓵", num)))
}

which you gave previously.

So you expect nnum(1) to refer to different code block (lines) depending on location of the reference (with respect to code blocks)?

Yes, as I reference to nnum(...) directly after the code block.