Skip to content

Automatic detection of quantities in written text #38

@eltos

Description

@eltos

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions