How to avoid namespace conflicts between packages

Hi all

I’m using the Touying package to develop course slides. I also like the tblr package a lot for helping me typesetting nice tables. However, both packages define a cols function. The former as an alternative to grid, the latter as a means to apply a function to all cells in a column of a table.

What is the best way to avoid these namespace clashes? Should I #import "@preview/tblr:0.5.0" as tblr and use everywhere the prefix tblr. when I want to call a function belonging to the tblr package? This gets cumbersome rather quickly…

Is there a way to tell Typst that in a certain context/code block/… I specifically want to use the namespace of tblr?

I would either go with selective imports
#import "@preview/tblr:0.5.0": cols as tcols, <other selective imports>

or with an alias. MWE:

#import "@preview/tblr:0.5.0": *
#let tcols = cols          // grab tblr's cols while it's still in scope

#import "@preview/touying:0.7.4": *
#import themes.university: *

#show: university-theme.with(
    aspect-ratio: "16-9",
    config-info(
      title: [Test Slides],
      author: [author],
      date: datetime.today(),
    ),
  )


== Slide 1

#cols[a][b]

== Slide 2

#context tblr(header-rows: 1, columns: 4,
  // formatting directives
  tcols(within: "body", 0, fill: gray.lighten(70%), hooks: strong),
  // content
  [Pakistan],      [165],  [803],   [206.2],
  [Bangladesh],    [147],  [144],   [1023.4],
  [Russia],        [142],  [17075], [8.4],  
)
2 Likes

I abbreviate package names most of the time to just two letter labels:

  • lilaq is shortened to lq (as per the author’s use in the examples)
  • tiptoe to tt
  • fletcher to fl
  • pavemat to pm

etc.

This makes it much easier to write while avoiding conflicts and keeps the document clean enough for yourself-in-two-weeks to still understand what’s going on.

1 Like

I wish there was a way in Typst to “open” a module or dictionary locally, importing all its names into scope. This would let us do things like:

#import "@preview/tblr:0.5.0" as tblr
#import "@preview/touying:0.7.4" as touying
#import themes.university: *

// names from touying are usable unqualified
open touying

{ // but here I am going to do a lot of table stuff,
  // so prefer 'tblr' locally for names in common
  open tblr
  ...
}

Doesn’t this work already?

#import "@preview/tblr:0.5.0"
#import "@preview/touying:0.7.4"

// names from touying are usable unqualified
#import touying: *
#cols[a][b]

#[
  // but here I am going to do a lot of table stuff,
  // so prefer 'tblr' locally for names in common
  #import tblr: *
  #tblr(columns: 2, cols(0, fill: gray))[a][b]
]

That’s only for modules though, see Dynamic modules · Issue #6419 · typst/typst · GitHub for dictionaries

3 Likes

Is there a language that does this?