From: Benjamin Buchmuller <benjamin.buchmuller@gmail.com>
To: fleupold@posteo.net, henrimenke@gmail.com
Cc: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: Use \unit for value and uncertainty
Date: Sat, 9 May 2020 12:39:51 +0200 [thread overview]
Message-ID: <3D454F21-981C-49ED-886F-52B1FC9C9488@gmail.com> (raw)
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
___________________________________________________________________________________
next reply other threads:[~2020-05-09 10:39 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-09 10:39 Benjamin Buchmuller [this message]
2020-05-09 12:52 ` Otared Kavian
-- strict thread matches above, loose matches on Subject: below --
2016-09-25 18:11 Florian Leupold
2016-10-31 16:30 ` Florian Leupold
2016-11-02 10:27 ` Henri Menke
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3D454F21-981C-49ED-886F-52B1FC9C9488@gmail.com \
--to=benjamin.buchmuller@gmail.com \
--cc=fleupold@posteo.net \
--cc=henrimenke@gmail.com \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).