Numerical Calculations
Is there a way to for ConTeXt to do basic arithmetic operations? For example, \calc{1+2} and have it return 3 or \calc{1/3.8} and have it return 0.263157894736842 (or rounded) Troy
On 12/19/2012 07:09 PM, Troy Henderson wrote:
Is there a way to for ConTeXt to do basic arithmetic operations? For example,
\calc{1+2} and have it return 3 or
\calc{1/3.8} and have it return 0.263157894736842 (or rounded)
Troy
http://wiki.contextgarden.net/Programming_in_LuaTeX#Examples
On 12/19/2012 7:09 PM, Troy Henderson wrote:
Is there a way to for ConTeXt to do basic arithmetic operations? For example,
\calc{1+2} and have it return 3 or
\calc{1/3.8} and have it return 0.263157894736842 (or rounded)
\luaexpr{1+2} ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
On 12/19/2012 7:09 PM, Troy Henderson wrote:
\calc{1/3.8} and have it return 0.263157894736842 (or rounded)
\ctxcommand{format("@0.3f",1/3.8)} @ as well as % ... just lua format but @ is more tex friendly ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
Do \luaexpr{1/3.8} and \ctxcommand{format("@0.3f",1/3.8)} return numbers or strings? That is, I would like to use these results in a \if or \ifnum command to exit a \doloop. Troy
Le 20 déc. 12 à 05:43, Troy Henderson a écrit :
Do \luaexpr{1/3.8} and \ctxcommand{format("@0.3f",1/3.8)} return numbers or strings? That is, I would like to use these results in a \if or \ifnum command to exit a \doloop.
Troy Bonjour, \ctxlua{context(type(\luaexpr{1/3.8}))} gives : number Roland
___________________________________________________________________________________ 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 ___________________________________________________________________________________
On 12/19/2012 8:19 PM, Roland Thiers wrote:
Le 20 déc. 12 à 05:43, Troy Henderson a écrit :
Do \luaexpr{1/3.8} and \ctxcommand{format("@0.3f",1/3.8)} return numbers or strings? That is, I would like to use these results in a \if or \ifnum command to exit a \doloop.
Troy Bonjour, \ctxlua{context(type(\luaexpr{1/3.8}))} gives : number
sure, but the context command pipes it back to tex as characters that get tokenized Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
On 12/19/2012 7:43 PM, Troy Henderson wrote:
Do \luaexpr{1/3.8} and \ctxcommand{format("@0.3f",1/3.8)} return numbers or strings? That is, I would like to use these results in a \if or \ifnum command to exit a \doloop.
they return tokens i.e. \ifnum\luaexpr{math.round(1/3.8)}>1\relax should work ok; it all depends on what you do, but you could also set a counter ans use that: \ctxlua{tex.count.scratchcounter=math.round(1/3.8)} \ifnum\scratchcounter>0 ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
···
Do \luaexpr{1/3.8} and \ctxcommand{format("@0.3f",1/3.8)} return numbers or strings? That is, I would like to use these results in a \if or \ifnum command to exit a \doloop.
Fwiw there are Lua-side conditionals available as well: http://www.ntg.nl/pipermail/ntg-context/2012/068673.html with these maybe you won’t need TeX for the tests. Regards Philipp -- () ascii ribbon campaign - against html e-mail /\ www.asciiribbon.org - against proprietary attachments
participants (5)
-
Hans Hagen
-
Philipp Gesang
-
Roland Thiers
-
Thomas A. Schmitz
-
Troy Henderson