Calculator Community > Lua

Creating/formating an evaluated Math Box from Lua script?

(1/2) > >>

Goran:
Hello,

I woud like to create Math Boxes from a Lua script, like:

    self.ed:setExpression('\\0el {v} ')

This works ok, but this shows input part only.
As variable 'v' is already evaluated, I would like to:
   a) show immediatelly the output part, too (i.e. the evaluated result)
   b) to format, i.e to set the attributes of such a Math Box, like it's possible manually when you add a Math Box in e.g. Notes application
       (like "Insert Symbol","Display Digits")

Lua Scripting API Reference says:

"\0el {...}" - Denotes a Math Box (Expression Box). Evaluated Math Box
expressions result in a pair of "\0el {...}" separated by a fi lled in '>'. See the
Guidebook for a list of valid math expressions for the Math Box.


So, can I give to self.ed:setExpression() method such
   'a pair of "\0el {...}" separated by a fi lled in '>'
string?
If yes, how does it look like exactly (example)?

Regarding the formating, the guide  further says:

'Rich text editors embed other formatting information in the text
string. This information may change in future releases, so using it is
not recommended. It is delimited by "\1 ...\".'

Where I can find more on the exact formating commands (the '...' part in "\1 ...\"?

Jim Bauwens:
[lua]D2Editor:setExpression[/lua] should contain some more info about the formatting. However, it's not possible to have the same functionality and options as the notes editor (actually it's possible but the method is very complex and very hacky. It requires some special Lua document creation and stuff, not so easy).

I personally use mathboxes in a D2Editor here https://github.com/adriweb/EEPro-for-Nspire/blob/master/2%20-%20%20FormulaPro/2%20-%20FPGui.lua#L365 but I suppose you already know and do what I do there.

Goran:
Thanks Jim for the quick answer.

If I forget the formatting part, I still do not understand the sentence from the reference guide on 'a pair of "\0el {...}" separated by a filled in '>'"

Is it some pure statement how a manually created and evaluated Math Box input/output is internally stored as a string (for e.g. parsing purposes),
or can we programatically insert a Math Box into D2Editor (a calculated variable) that would show the result (output part) immediatelly?

Regards,
Goran

Adriweb:
What about tmp = tostring(math.eval(yourCalculation)) and then setExpression('\\0el {'..tmp..'} ') ?

Goran:
Thanks adriweb, it finally clicked in!

So, I can put two (or any number of) '\\0el {...} Math Boxes into D2Editor,
fill the first one with a variable/function name and the right one with math.eval(Str)'ed expression,
separate them with any character like '=' (not just with a filled in '>' - the black right-pointing triangle),
and that's it!

All the time I was "locked down" to belief that I give to D2Editor a Math Box with an evaluated variable
name and that some mechanism will show me, if properly configured, also its evaluated value on the right side automatically.


On the formatting issue:

Thanks to Jim who sent me a link to '\\1' formating commands, I already use them.

Anyway, I've noticed the the following behavior:
the reference manual talks about '\\1 <command>...\\' pattern (note the ending backslashes).
My experience is that '\\1 <command>' is effecting the only the first word of '...', the ending '\\' is not used at all (so I see an extra backslash at the end of '...' text.
If I want to e.g. underline text

   'word1 word2 word3'

I have to put a formating command in front of each word, i.e.

   '\\1title word1 \\1title word2 \\1title word3'

(this can be automatized with using e.g.

   string.gsub("...","(%w+)","\\1title %1")

I did not try on the calculator, it's experience from using the PC software.
Did you have such experiences?

Thanks once more to both of you for the great (and quick!) support,
Goran

Navigation

[0] Message Index

[#] Next page

Go to full version