Units in Glossy (or glossarium)

Hi everyone,
I was trying to make a simple addition to handle units, preferably with unify, but manually setting them is fine too, since it is not reused. I have provided the best I could come up with for using a theme for the glossary. If i should use glossarium or something else instead, that is fine, the end result should have units as I use that a lot. I honestly don’t really know how to proceed without making a fork of the glossy package.

Top of main.typ:

  #import "@preview/glossy:0.9.2": *
    #show: init-glossary.with(yaml("glossaries/sinter.yaml"))

Using the abbreviations and printing the glossary:

@wb:cap  @wb1 @wb13 @wb14 @wb15 @void
  #include "backmatter/symbols.typ"

backmatter/symbols.typ:

#import "@preview/glossy:0.9.2": *
#import "@preview/unify:0.8.1": *
// https://github.com/swaits/typst-collection/blob/main/glossy/src/themes/table.typ
#let theme-mytable = (
  section: (title, body) => {
    heading(level: 2, title)
    body
  },
  group: (name, index, total, body) => {
    if name != "" and total > 1 {
      heading(level: 2, name)
    }

    table(
      columns: 4,
      stroke: none,
      inset: (x, y) => {
        if (x == 0) {
          (left: 0pt, rest: 5pt)
        } else if (x == 2) {
          (right: 0pt, rest: 5pt)
        } else {
          5pt
        }
      },
      table.header([*Abbreviation*], [*Full Name*], [*Description*], [*Unit*]),
      ..body,
    )
  },
  entry: (entry, index, total) => {
    (
      entry.short + entry.label,
      entry.long,
      entry.description,
      if "unit" in entry {
        unit(entry.unit)
      } else {
        [[--]]
      },
    )
  },
)

// this is what prints the glossaries
= Glossary

#glossary(
  title: "sinter Abbreviations",
  theme: theme-mytable,
  groups: ("sinter machine")
)

#glossary(
  title: "other Abbreviations",
  theme: theme-mytable,
  groups: ("")
)

here is the sinter.yaml:

wb:
  short: WB
  long: windbox
  description: The windbox is the windbox. The windbox is the windbox. The windbox is the windbox. The windbox is the windbox. The windbox is the windbox. The windbox is the windbox. The windbox is the windbox. 
  group: sinter machine

wb1:
  short: WB
  long: windbox
  description: The windbox is the windbox. The windbox is the windbox. The windbox is the windbox. The windbox is the windbox. The windbox is the windbox. The windbox is the windbox. The windbox is the windbox. 
  group: sinter machine


void:
  short: ε 
  long: void fraction
  description: Fraction of empty space in a given volume.
  unit: nothing

wb13:
  short: WB
  long: windbox
  description: The windbox is the windbox.
  unit: "m"

wb14:
  short: WB
  long: windbox
  description: The windbox is the windbox.

wb15:
  short: WB
  long: windbox
  description: The windbox is the windbox.

result:
image
and then:

As you can see, the default unit, defined in the #let theme-mytable code, is used but not the units defined in the yaml.

I hope this is not too convoluted, if it is, i can make a simpler version, but the main focus is the #let theme-mytable is the most central part (me thinks).

Here is a simpler version, or at least self contained
view only project on typst.app

I thank you all in advance

The glossy package seems to not support custom fields, so if you want clean implementation forking / a feature request is probably the best solution.

If you just want a solution to have the unit show up in your abbreviations tables, and don’t mind some hacky code, you can partially sidestep glossy. The code imports the YAML file with the glossary terms a second time and directly looks up the unit for a given entry:

...
  entry: (entry, index, total) => {
    let data = yaml("sinter.yaml")
    (
      entry.short + entry.label,
      entry.long,
      entry.description,
      if "unit" in data.at(entry.label.value) {
        unit(data.at(entry.label.value).unit)
      } else {
        [[--]]
      },
    )
  },
)

Hi,

Sorry for the late reply, I had some crap to deal with and forgot about this.

It works well! Thanks.

I have already submitted a FR for glossy before making this post.

Could you help me with putting the units in square brackets? in the same way that “no unit” is

[[--]], I am supposed to put the unit in square brackets too. I could not find anything in unify itself yet (like a global setting for this, which would be the best option, since i need this document wide anyway)

I tried a few things like `math.lr([unit(data.at(entry.label.value).unit)])`

or just placing square brackets (single and dual) around the whole thing, but typst is not a fan of that.

So this:

      if "unit" in data.at(entry.label.value) {
        sym.bracket.l; unit(data.at(entry.label.value).unit); sym.bracket.r;
      } else {
        [[--]]
      },

seems to work. I will post an update about the global setting for unify too

Nice!

That’s because of the 3 modes Typst uses: content, code, and math. By default in an empty .typ file, you are in content mode, which interprets markup (e.g. *bold*). With # you enter code mode, with $...$ math mode and with [...] you can get back to content mode.

The more readable version would be:

if "unit" in data.at(entry.label.value) {
  [[#unit(data.at(entry.label.value).unit)]]
} else {
  [[--]]
},

Inside the curly braces, we are still in code mode (which started with #let theme-mytable = ...). With the first square brackets, you enter content mode, and then the second set of square brackets is interpreted as content, and for the call to unit(...) you have to again switch to code mode with #.

Thanks,

I fundamentally know about that but I forgot to add the # to make the unit command back into a command. Of course that just prints the entire unit(...) thing into the page as is.

I was out of the typst mindset, since I did not use it for a bit, lol.