Hi Benjamin, Thanks for sharing your nice code, which is very useful. However I wanted to let you know that it seems it does not work correctly with LuaMetaTeX version 2020.04.30 11:10, even though it works fine with ConTeXt mkiv. Best regards: Otared K.
On 9 May 2020, at 12:39, Benjamin Buchmuller
wrote: I know this is quite an old thread, but here is a minimal parser (\units with an “s”) that wraps around \digits and \unit to produce an acceptable output.
As I frequently need to write ranges (or measures of uncertainty), I find it convenient to able to type
\units{4.0 to 5.0 centi meter}
than
\digits{4.0}\,to\,\unit{5.0 centi meter}.
Also, the parser will take care of exponents and bracket them accordingly. :D
ranges: keyword “to”: 4.0 – 5.0 cm SEM: keyword “se”: 4.5 ± 0.5 cm SD: keyword “sd”: 4.5 (0.5) cm
If no keyword is present, the default behaviour is \unit{…}.
The code is not perfect (and the level of abstraction potentially not yet sufficient to make it part of the phys-dim.mkiv source), but maybe helpful.
\starttext
\startluacode userdata = userdata or {}
function userdata.units(input)
tbl = string.explode(input)
if tbl[2] == "to" then context.digits(tbl[1]) context.phys_units_space() context("--") context.phys_units_space() context.unit(table.concat(tbl, " ", 3)) elseif tbl[2] == "se" then local sx1 = string.split(tbl[1], "e") local sx2 = string.split(tbl[3], "e") if (sx1[2] == sx2[2]) and not (sx1[2] == nil) then context("(") context.digits(sx1[1]) context.phys_units_space() context("±") context.phys_units_space() context.digits(sx2[1]) context(")") context.digits("e" .. sx1[2]) context.phys_units_space() context.unit(table.concat(tbl, " ", 4)) else context.digits(tbl[1]) context.phys_units_space() context("±") context.phys_units_space() context.unit(table.concat(tbl, " ", 3)) end elseif tbl[2] == "sd" then local sx1 = string.split(tbl[1], "e") local sx2 = string.split(tbl[3], "e") if (sx1[2] == sx2[2]) and not (sx1[2] == nil) then context.digits(sx1[1]) context.phys_units_space() context("(") context.digits(sx2[1]) context(")") context.digits("e" .. sx1[2]) context.phys_units_space() context.unit(table.concat(tbl, " ", 4)) else context.digits(tbl[1]) context.phys_units_space() context("(") context.digits(tbl[3]) context(")") context.unit(table.concat(tbl, " ", 4)) end else context.unit(table.concat(tbl, " ")) end
end \stopluacode
\def\units#1{\ctxlua{userdata.units("#1")}}
Car 1 drives \units{4 to 5.2 kilo meter per hour}.
Car 2 drives \units{30.1 to 40.5 kilo meter per hour}.
Car 3 drives \units{40.= to 50.= kilo meter per hour}.
The average speed was \units{35,000 se 5000 meter per hour}.
The average speed was \units{35e3 se 0.5e3 meter per hour}.
The average speed was \units{35.2e3 se 5e2 meter per hour}.
\stoptext
The average speed was \units{35.2 se 5e2 meter per hour}.
The average speed was \units{_3.2 sd 5 meter per hour}. ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________