Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fractional values as main quantities #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified examples/example.pdf
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/example.typ
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#set text(lang:"en")
Working with english characters:
$ num("-1.32865+-0.50273e-6") $
$ num("123/456") $
$ num("1/100e6") $
$ num("1/100+-1.2") $
$ qty("1.3+1.2-0.3e3", "erg/cm^2/s", space: "#h(2mm)") $
$ qty("123/456+-1.2", "erg/cm^2/s", space: "#h(2mm)") $
$ numrange("1,1238e-2", "3,0868e5", thousandsep: "'") $
$ qtyrange("1e3", "2e3", "meter per second squared", per: "/", delimiter: "\"to\"") $

Expand Down
46 changes: 30 additions & 16 deletions lib.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#let _re-num = regex("^(-?\d+(\.|,)?\d*)?(((\+(\d+(\.|,)?\d*)-(\d+(\.|,)?\d*)))|((((\+-)|(-\+))(\d+(\.|,)?\d*))))?(e([-\+]?\d+))?$")
#let _re-num = regex("^(-?\d+(\.|,)?\d*(/\d+(\.|,)?\d*)?)?(((\+(\d+(\.|,)?\d*(/\d+(\.|,)?\d*)?)-(\d+(\.|,)?\d*(/\d+(\.|,)?\d*)?)))|((((\+-)|(-\+))(\d+(\.|,)?\d*(/\d+(\.|,)?\d*)?))))?(e([-\+]?\d+))?$")
#let _unicode-exponents = (("\u2070", "0"), ("\u00B9", "1"), ("\u00B2", "2"), ("\u00B3", "3"), ("\u2074", "4"), ("\u2075", "5"), ("\u2076", "6"), ("\u2077", "7"), ("\u2078", "8"), ("\u2079", "9"), ("\u207A", "+"), ("\u207B", "-"))

#let _format-float(f, decsep: "auto", thousandsep: "#h(0.166667em)") = {
Expand Down Expand Up @@ -48,6 +48,17 @@
string
}

#let _format-frac(f) = {
let string = ""
let split = str(f).split("/")
let numerator = split.at(0)
let denominator = split.at(1)

string += numerator + "/" + denominator

string
}

#let _format-num(
value, exponent: none, upper: none, lower: none,
multiplier: "dot", thousandsep: "#h(0.166667em)") = {
Expand All @@ -60,9 +71,12 @@
/// - `thousandsep`: The separator between the thousands of the float.

let formatted-value = ""
if value != none {
if value != none and not str(value).contains("/") {
formatted-value += _format-float(value, thousandsep: thousandsep).replace(",", ",#h(0pt)")
}
if value != none and str(value).contains("/") {
formatted-value += _format-frac(value)
}
if upper != none and lower != none {
if upper != lower {
formatted-value += "^(+" + _format-float(upper, thousandsep: thousandsep) + ")"
Expand Down Expand Up @@ -103,17 +117,17 @@

let upper = none
let lower = none
if captures-value.at(14) != none {
upper = captures-value.at(14)
if captures-value.at(20) != none {
upper = captures-value.at(20)
lower = none
} else {
upper = captures-value.at(5)
lower = captures-value.at(7)
upper = captures-value.at(7)
lower = captures-value.at(11)
}

let formatted = _format-num(
captures-value.at(0),
exponent: captures-value.at(17),
exponent: captures-value.at(25),
upper: upper,
lower: lower,
multiplier: multiplier,
Expand Down Expand Up @@ -590,17 +604,17 @@

let upper = none
let lower = none
if captures-value.at(14) != none {
upper = captures-value.at(14)
if captures-value.at(20) != none {
upper = captures-value.at(20)
lower = none
} else {
upper = captures-value.at(5)
lower = captures-value.at(7)
upper = captures-value.at(7)
lower = captures-value.at(11)
}

let formatted-value = _format-num(
captures-value.at(0),
exponent: captures-value.at(17),
exponent: captures-value.at(25),
upper: upper,
lower: lower,
multiplier: multiplier,
Expand Down Expand Up @@ -681,8 +695,8 @@
let formatted = _format-range(
captures-lower.at(0),
captures-upper.at(0),
exponent-lower: captures-lower.at(17),
exponent-upper: captures-upper.at(17),
exponent-lower: captures-lower.at(25),
exponent-upper: captures-upper.at(25),
multiplier: multiplier,
delimiter: delimiter,
thousandsep: thousandsep,
Expand Down Expand Up @@ -721,8 +735,8 @@
let formatted-value = _format-range(
captures-lower.at(0),
captures-upper.at(0),
exponent-lower: captures-lower.at(17),
exponent-upper: captures-upper.at(17),
exponent-lower: captures-lower.at(25),
exponent-upper: captures-upper.at(25),
multiplier: multiplier,
delimiter: delimiter,
space: space,
Expand Down