Fancy-units:0.1.0 - format numbers and units with styling

If you are thinking “why would we possibly need another units package?”, I can assure you that fancy-units actually brings something new to the table. While the existing packages are just aiming to be ports of siunitx, I tried to design the package to work/feel like native Typst. The input of numbers and units is entirely content-based, and you can use this to style (parts) of the numbers and units with the built-in Typst functions. There are also no unit macros necessary, you can just write down the units like regular text, and by default the decimal separator is directly inferred from the text language.

A few examples

Configuration

There are a few options to change the output format of numbers and units. Most importantly the uncertainty-mode to use absolute or relative uncertainties, and the per-mode to use regular fractions, in-line fractions or powers. For the full documentation, please see the manual.pdf


You can change these options through a state for an entire document/scope

#import "@preview/fancy-units:0.1.0": fancy-units-configure

#fancy-units-configure(
  uncertainty-mode: "plus-minus",
  per-mode: "slash",
  ...
)

or individually for each number and unit

#import "@preview/fancy-units:0.1.0": num, unit

#num(uncertainty-mode: "parentheses")[0.9(1)]
#unit(per-mode: "power")[W / kg]

Where to start?

If you are curious by now and want to play around with the package, you can use the following examples to get started

#import "@preview/fancy-units:0.1.0": fancy-units-configure, num, unit, qty

#fancy-units-configure(
  uncertainty-mode: "conserve",
  per-mode: "fraction"
)

$
  #num[123*(4)*e5] \
  #unit[kg m / s^2] \
  #qty[137][#text(red)[μ]:m] \
  #qty[1][M:#sym.Omega] \
  #unit(per-mode: "power")[kg / m^-2] \
  #unit[F] = #unit(per-mode: "fraction", unit-separator: sym.dot)[(s C)^2 / (m^2 kg)] \
  #unit[kg#math.cancel[^2] / #math.cancel[kg]] = #unit[kg]
$

Planned features

So far I am still planning to implement the following features:

  • Macros for custom units (I am already getting annoyed by having to type _E_#sub[rec] all the time…)
  • Support more built-in (styling) functions
  • Digit grouping to make long numbers easier to read (299 792 458 instead of 299792458)

If you have any suggestions or ideas how I could further improve the package, please let me know. :slight_smile:

14 Likes

This is really neat! I was wishing someone made something like this, I especially like the “styling” options. I’m sure there’s ways to add styling options like x10 vs E notation, truncating some long large numbers like this: 1296196107 => 1.296*10…, automatically converting cm based units to m, etc etc. I really like the package, but I’d recomend to take a look at other unit packages and see if you can merge your code into theirs, or theirs into yours. Most packages have very permissible licenses and it would be a bummer to have a fragmented ecosystem.

1 Like