Re: [NTG-context] Formatting numbers in LuaTeX

2010-02-15 Thread Peter Münster
On Mon, Feb 15 2010, Andreas Harder wrote:
> 
> But the "$·$"-construct is problematic. It do not work in display style
> (\let\calcmath\displaycalcmath) and without the dollar-signs the placement
> and the spacing are wrong.

I don't know, why "·" doesn't work, but you can try this:

local exp_replacements = {["*"] = "cdot"}

Cheers, Peter

-- 
Contact information: http://pmrb.free.fr/contact/


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


Re: [NTG-context] Formatting numbers in LuaTeX

2010-02-15 Thread Andreas Harder

Am 15.02.2010 um 20:35 schrieb Peter Münster:

> On Sun, Feb 14 2010, Hans Hagen wrote:
> 
>>>   sqrt = math.sqrt
>>>   pi = math.pi
>>>   function my_equation(s, r)
>> 
>> beware, this creates (and possibly overloads) sqrt, pi and my_equation as 
>> gobals
> 
> Indeed...
> 
> The following hack was *not* so quick, but there is a bug somewhere
> (a digit is added at the end):
> 
> \usemodule[calcmath]
> \startluacode
> local exp_replacements = {["*"] = "$·$"}
> local res_replacements = {["sqrt"] = "math.sqrt", ["π"] = "math.pi"}
> userdata = userdata or {}
> function userdata.result_as_string(r)
>   r = math.floor(1000 * r + 0.5) / 1000
>   r = tostring(r)
>   tex.write(r:gsub("%.", ","))
> end
> function userdata.filter_expression(s)
>   for k, v in pairs(exp_replacements) do
>   s = s:gsub(k, v)
>   end
>   tex.write(s)
> end
> function userdata.filter_result(s)
>   for k, v in pairs(res_replacements) do
>   s = s:gsub(k, v)
>   end
>   tex.write(s)
> end
> \stopluacode
> \def\calculate#1{%
>  \start
>\def\Expression{\ctxlua{userdata.filter_expression("#1")}}
>\def\Result{\ctxlua{userdata.filter_result("#1")}}
>\calcmath{\Expression=\ctxlua{userdata.result_as_string(\Result)}}
>  \stop
> }
> \starttext
> \startlines
>  \calculate{1+2}
>  \calculate{3/2-1}
>  \calculate{3*2}
>  \calculate{sqrt(2)}
>  \calculate{2*π}
> \stoplines
> \stoptext

I've noticed that one can even use such things as \calculate{3^2},  
\calculate{2^3} out of the box and it's easy to add
["e"] = "math.exp(1)", ["sin"] = "math.sin", …

But the "$·$"-construct is problematic. It do not work in display style 
(\let\calcmath\displaycalcmath) and without the dollar-signs the placement and 
the spacing are wrong.

Thank you for your elaboration.

Greetings
Andreas




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


Re: [NTG-context] Formatting numbers in LuaTeX

2010-02-15 Thread Hans Hagen

On 15-2-2010 20:35, Peter Münster wrote:


tex.write(r:gsub("%.", ","))


tex.write((r:gsub("%.", ",")))

gsub returns two values

-
  Hans Hagen | PRAGMA ADE
  Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
 tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
 | www.pragma-pod.nl
-
___
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
___


Re: [NTG-context] Formatting numbers in LuaTeX

2010-02-15 Thread Peter Münster
On Sun, Feb 14 2010, Hans Hagen wrote:

>>sqrt = math.sqrt
>>pi = math.pi
>>function my_equation(s, r)
>
> beware, this creates (and possibly overloads) sqrt, pi and my_equation as 
> gobals

Indeed...

The following hack was *not* so quick, but there is a bug somewhere
(a digit is added at the end):

\usemodule[calcmath]
\startluacode
local exp_replacements = {["*"] = "$·$"}
local res_replacements = {["sqrt"] = "math.sqrt", ["π"] = "math.pi"}
userdata = userdata or {}
function userdata.result_as_string(r)
r = math.floor(1000 * r + 0.5) / 1000
r = tostring(r)
tex.write(r:gsub("%.", ","))
end
function userdata.filter_expression(s)
for k, v in pairs(exp_replacements) do
s = s:gsub(k, v)
end
tex.write(s)
end
function userdata.filter_result(s)
for k, v in pairs(res_replacements) do
s = s:gsub(k, v)
end
tex.write(s)
end
\stopluacode
\def\calculate#1{%
  \start
\def\Expression{\ctxlua{userdata.filter_expression("#1")}}
\def\Result{\ctxlua{userdata.filter_result("#1")}}
\calcmath{\Expression=\ctxlua{userdata.result_as_string(\Result)}}
  \stop
}
\starttext
\startlines
  \calculate{1+2}
  \calculate{3/2-1}
  \calculate{3*2}
  \calculate{sqrt(2)}
  \calculate{2*π}
\stoplines
\stoptext

Cheers, Peter

-- 
Contact information: http://pmrb.free.fr/contact/


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


Re: [NTG-context] Formatting numbers in LuaTeX

2010-02-14 Thread Hans Hagen

On 14-2-2010 14:10, Andreas Harder wrote:


\startluacode
   local floor = math.floor
   local round = function(n) return floor(1000*n+0.5)/1000 end

   sqrt = math.sqrt
   pi = math.pi
   function my_equation(s, r)
 r = round(r)
 r = tostring(r)
 tex.print(s:gsub("*", "$·$") .. "=" .. r:gsub("%.", ","))
   end
\stopluacode


beware, this creates (and possibly overloads) sqrt, pi and my_equation 
as gobals


Hans


-
  Hans Hagen | PRAGMA ADE
  Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
 tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
 | www.pragma-pod.nl
-
___
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
___


Re: [NTG-context] Formatting numbers in LuaTeX

2010-02-14 Thread Andreas Harder

Am 14.02.2010 um 13:11 schrieb Peter Münster:

> On Sun, Feb 14 2010, Andreas Harder wrote:
> 
>> 1) Is it possible to have a comma as decimal separator?
>> 
>> 2) If you look at the above definition, it works fine with +, –, /, but
>> not with *. Is it possible to expand my macro that it will also work with
>> a multiplication or even with the last two examples?
> 
> Hello Andreas,
> 
> Just a quick hack:
> 
> \usemodule[calcmath]
> \startluacode
> sqrt = math.sqrt
> pi = math.pi
> function my_equation(s, r)
>   r = tostring(r)
>   tex.print(s:gsub("*", "×") .. "=" .. r:gsub("%.", ","))
> end
> \stopluacode
> \def\calculate#1{\calcmath{\ctxlua{my_equation("#1", #1)}}}
> \starttext
> \startlines
>  \calculate{1+2}
>  \calculate{3/2-1}
>  \calculate{3*2}
>  \calculate{sqrt(2)}
>  \calculate{2*pi}
> \stoplines
> \stoptext

That’s nice! Thank you very much! 

I've found an posting on the LuaTeX-mailing-list and incorporated it, so one 
have rounded results (I didn't find a way to use "\%.2f"). So, if someone is 
interested:

\usemodule[calcmath]

\startluacode
  local floor = math.floor
  local round = function(n) return floor(1000*n+0.5)/1000 end

  sqrt = math.sqrt
  pi = math.pi
  function my_equation(s, r)
r = round(r)
r = tostring(r)
tex.print(s:gsub("*", "$·$") .. "=" .. r:gsub("%.", ","))
  end
\stopluacode

\def\calculate#1{\calcmath{\ctxlua{my_equation("#1", #1)}}}

\starttext

\startlines
 \calculate{1+2}
 \calculate{3/2-1}
 \calculate{3*2}
 \calculate{sqrt(2)}
 \calculate{2*pi}
\stoplines

\stoptext


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


Re: [NTG-context] Formatting numbers in LuaTeX

2010-02-14 Thread Peter Münster
On Sun, Feb 14 2010, Andreas Harder wrote:

> 1) Is it possible to have a comma as decimal separator?
> 
> 2) If you look at the above definition, it works fine with +, –, /, but
> not with *. Is it possible to expand my macro that it will also work with
> a multiplication or even with the last two examples?

Hello Andreas,

Just a quick hack:

\usemodule[calcmath]
\startluacode
sqrt = math.sqrt
pi = math.pi
function my_equation(s, r)
r = tostring(r)
tex.print(s:gsub("*", "×") .. "=" .. r:gsub("%.", ","))
end
\stopluacode
\def\calculate#1{\calcmath{\ctxlua{my_equation("#1", #1)}}}
\starttext
\startlines
  \calculate{1+2}
  \calculate{3/2-1}
  \calculate{3*2}
  \calculate{sqrt(2)}
  \calculate{2*pi}
\stoplines
\stoptext

Cheers, Peter

-- 
Contact information: http://pmrb.free.fr/contact/


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


Re: [NTG-context] Formatting numbers in LuaTeX

2010-02-14 Thread luigi scarso
On Sun, Feb 14, 2010 at 11:42 AM, Andreas Harder
 wrote:
> Hi all!
>
> I played a little bit with LuaTeX in ConTeXt and two questions have arisen.
>
> \usemodule[calcmath]
> \starttext
> \def\calculate#1{\calcmath{#1=\ctxlua{tex.print(#1)}}}
> \calculate{1+2} \par
> \calculate{3/2-1}\par % the output should be 0,5
> \calculate{3*2}\par
> % \calculate{sqrt(2)} \calculate{2*π} % 2*\pi
> \stoptext
>
> 1) Is it possible to have a comma as decimal separator?
string.format ?
-- 
luigi
___
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
___