Re: [NTG-context] Use \unit for value and uncertainty

2020-05-09 Thread Otared Kavian
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
> 

Re: [NTG-context] Use \unit for value and uncertainty

2020-05-09 Thread Benjamin Buchmuller
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
___


Re: [NTG-context] Use \unit for value and uncertainty

2016-11-02 Thread Henri Menke
On 10/31/2016 05:30 PM, Florian Leupold wrote:
> Hello!
> 
> There does not seem to be a simple solution to using \unit with 
> uncertainties, see below.
> 
> Could someone maybe point me to the relevant source code for the parser for 
> \unit in MKIV? Then I would try to modify/improve the code myself.

phys-dim.mkiv
https://github.com/contextgarden/context-mirror/blob/beta/tex/context/base/mkiv/phys-dim.mkiv

phys-dim.lua
https://github.com/contextgarden/context-mirror/blob/beta/tex/context/base/mkiv/phys-dim.lua

> 
> Thanks a lot and best regards,
> Florian
> 
> 
>> On 25.09.16, at 20:11, Florian Leupold  wrote:
>>
>> Dear list
>>
>> Is it possible to use the units module (in MKIV) for value plusminus 
>> uncertainty in a way similar to the \SI command in LaTeX, like
>>  \unit{(1.59\pm2)e-19 coulomb} or
>>  \unit{1.59(2)e-19 coulomb}?
>>
>> Thanks and best regards,
>> Florian
>> ___
>> 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://tex.aanhet.net
>> archive  : http://foundry.supelec.fr/projects/contextrev/
>> wiki : http://contextgarden.net
>> ___
> 
> ___
> 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
> ___
> 

___
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
___

Re: [NTG-context] Use \unit for value and uncertainty

2016-10-31 Thread Florian Leupold
Hello!

There does not seem to be a simple solution to using \unit with uncertainties, 
see below.

Could someone maybe point me to the relevant source code for the parser for 
\unit in MKIV? Then I would try to modify/improve the code myself.

Thanks a lot and best regards,
Florian


> On 25.09.16, at 20:11, Florian Leupold  wrote:
> 
> Dear list
> 
> Is it possible to use the units module (in MKIV) for value plusminus 
> uncertainty in a way similar to the \SI command in LaTeX, like
>  \unit{(1.59\pm2)e-19 coulomb} or
>  \unit{1.59(2)e-19 coulomb}?
> 
> Thanks and best regards,
> Florian
> ___
> 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://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki : http://contextgarden.net
> ___

___
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
___

[NTG-context] Use \unit for value and uncertainty

2016-09-25 Thread Florian Leupold
Dear list

Is it possible to use the units module (in MKIV) for value plusminus 
uncertainty in a way similar to the \SI command in LaTeX, like
  \unit{(1.59\pm2)e-19 coulomb} or
  \unit{1.59(2)e-19 coulomb}?

Thanks and best regards,
Florian


signature.asc
Description: Message signed with OpenPGP using GPGMail
___
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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___