zero icon indicating copy to clipboard operation
zero copied to clipboard

Automatic detection of quantities in written text

Open eltos opened this issue 4 months ago • 1 comments

Imagine you write a text like

The limit is about 3e8 m/s.

To make use of zero's formatting, you'd use #zero.zi.m-s[3e8] which is much more typing and reverses the order of number and unit, thus interrupting the writing flow. For non-builtin units it's even worse having to go to the document header and declaring the unit first. With other packages like unify using constructs like #qty(3e8, "m/s") is not much better either.

For my documents, I have compiled this helper method:

#import "@preview/zero:0.4.0"

#let auto-quantify(..options, fill: auto, body) = {
  let defaults = (omit-unity-mantissa: true, fraction: "inline")
  options = arguments(..defaults) + options
  
  let rnum = "[-\u{2212}]?\d+(?:.\d+)?(?:\+(?:\d+(?:.\d+)?)?-\d+(?:.\d+)?)?(?:e-?\d+)?"
  let runit = "[a-zA-ZΩµ%°^\d*/]+"
  let expression = regex("(" + rnum + ")[\u{00A0}~](" + runit + ")")
  show expression: it => {
    set text(..if fill != auto {(fill: fill)})
    let (value, unit) = it.text.match(expression).captures
    unit = unit.replace("*", " ") // explicit multipliation sign to avoid spaces in regex
    zero.zi.declare(unit)(value, ..options)
    //zero.zi.units.qty(value, unit, ..options)
  }
  
  body
}

Used as a show rule, it allows writing quantities like 3e8~m/s in a concise and natural way, with zero interruption of my flow of writing. The regex picks it up due to the no-break space and formats it nicely (color added for demonstration only):

#show: auto-quantify.with(product: sym.dot, fill: blue)

Examples:
- In 5~km turn left
- Power consumption: 15000~W
- Rigidity: $B rho = "18~T*m"$ // also works nicely in equations
- Current between -1e4~A and 2e5~A
- Gravity: 9.81+-0.01~m/s^2 (earth)
- Thickness: 13+1.5-2e-2~µm // or mu for µ
- Number only: 123456e7~1 works too by passing "1" as unit
Image

Would it be possible to include this auto-quantify method in zero? For common cases this regex makes "typsting" much more swift. The default options used above are of course my personal preference and can be changed/removed.

If you want I can prepare a PR.

eltos avatar Aug 21 '25 16:08 eltos

This is a very interesting idea and incredible that it works so well.

On the one hand, regex show rules are always somewhat hacky and you never quite know whatever matches they might pick up that are undesirable. Also, at first glance, it does not feel very idiomatic.

On the other hand, it looks very handy. And the user can always just decide whether they want to use this or not.

Let me think a bit about this!

Mc-Zen avatar Aug 21 '25 17:08 Mc-Zen