* Formatting numbers in LuaTeX
@ 2010-02-14 10:42 Andreas Harder
2010-02-14 11:37 ` luigi scarso
2010-02-14 12:11 ` Peter Münster
0 siblings, 2 replies; 9+ messages in thread
From: Andreas Harder @ 2010-02-14 10:42 UTC (permalink / raw)
To: mailing list for ConTeXt users
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?
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?
Thank you in advance
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Formatting numbers in LuaTeX
2010-02-14 10:42 Formatting numbers in LuaTeX Andreas Harder
@ 2010-02-14 11:37 ` luigi scarso
2010-02-14 12:11 ` Peter Münster
1 sibling, 0 replies; 9+ messages in thread
From: luigi scarso @ 2010-02-14 11:37 UTC (permalink / raw)
To: mailing list for ConTeXt users
On Sun, Feb 14, 2010 at 11:42 AM, Andreas Harder
<andreas.harder@rz-online.de> 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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Formatting numbers in LuaTeX
2010-02-14 10:42 Formatting numbers in LuaTeX Andreas Harder
2010-02-14 11:37 ` luigi scarso
@ 2010-02-14 12:11 ` Peter Münster
2010-02-14 13:10 ` Andreas Harder
1 sibling, 1 reply; 9+ messages in thread
From: Peter Münster @ 2010-02-14 12:11 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Formatting numbers in LuaTeX
2010-02-14 12:11 ` Peter Münster
@ 2010-02-14 13:10 ` Andreas Harder
2010-02-14 21:01 ` Hans Hagen
0 siblings, 1 reply; 9+ messages in thread
From: Andreas Harder @ 2010-02-14 13:10 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Formatting numbers in LuaTeX
2010-02-14 13:10 ` Andreas Harder
@ 2010-02-14 21:01 ` Hans Hagen
2010-02-15 19:35 ` Peter Münster
0 siblings, 1 reply; 9+ messages in thread
From: Hans Hagen @ 2010-02-14 21:01 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Formatting numbers in LuaTeX
2010-02-14 21:01 ` Hans Hagen
@ 2010-02-15 19:35 ` Peter Münster
2010-02-15 19:38 ` Hans Hagen
2010-02-15 22:25 ` Andreas Harder
0 siblings, 2 replies; 9+ messages in thread
From: Peter Münster @ 2010-02-15 19:35 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Formatting numbers in LuaTeX
2010-02-15 19:35 ` Peter Münster
@ 2010-02-15 19:38 ` Hans Hagen
2010-02-15 22:25 ` Andreas Harder
1 sibling, 0 replies; 9+ messages in thread
From: Hans Hagen @ 2010-02-15 19:38 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Formatting numbers in LuaTeX
2010-02-15 19:35 ` Peter Münster
2010-02-15 19:38 ` Hans Hagen
@ 2010-02-15 22:25 ` Andreas Harder
2010-02-16 6:08 ` Peter Münster
1 sibling, 1 reply; 9+ messages in thread
From: Andreas Harder @ 2010-02-15 22:25 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Formatting numbers in LuaTeX
2010-02-15 22:25 ` Andreas Harder
@ 2010-02-16 6:08 ` Peter Münster
0 siblings, 0 replies; 9+ messages in thread
From: Peter Münster @ 2010-02-16 6:08 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2010-02-16 6:08 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-14 10:42 Formatting numbers in LuaTeX Andreas Harder
2010-02-14 11:37 ` luigi scarso
2010-02-14 12:11 ` Peter Münster
2010-02-14 13:10 ` Andreas Harder
2010-02-14 21:01 ` Hans Hagen
2010-02-15 19:35 ` Peter Münster
2010-02-15 19:38 ` Hans Hagen
2010-02-15 22:25 ` Andreas Harder
2010-02-16 6:08 ` Peter Münster
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).