Author Topic: Creating/formating an evaluated Math Box from Lua script?  (Read 5630 times)

0 Members and 1 Guest are viewing this topic.

Offline Goran

  • LV2 Member (Next: 40)
  • **
  • Posts: 32
  • Rating: +0/-0
    • View Profile
Creating/formating an evaluated Math Box from Lua script?
« on: July 22, 2013, 02:22:19 pm »
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 ...\"?

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Creating/formating an evaluated Math Box from Lua script?
« Reply #1 on: July 22, 2013, 03:17:13 pm »
[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.

Offline Goran

  • LV2 Member (Next: 40)
  • **
  • Posts: 32
  • Rating: +0/-0
    • View Profile
Re: Creating/formating an evaluated Math Box from Lua script?
« Reply #2 on: July 22, 2013, 04:00:29 pm »
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

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Creating/formating an evaluated Math Box from Lua script?
« Reply #3 on: July 22, 2013, 04:05:50 pm »
What about tmp = tostring(math.eval(yourCalculation)) and then setExpression('\\0el {'..tmp..'} ') ?
« Last Edit: July 22, 2013, 04:06:15 pm by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Goran

  • LV2 Member (Next: 40)
  • **
  • Posts: 32
  • Rating: +0/-0
    • View Profile
Re: Creating/formating an evaluated Math Box from Lua script?
« Reply #4 on: July 23, 2013, 10:46:37 am »
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

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Creating/formating an evaluated Math Box from Lua script?
« Reply #5 on: July 23, 2013, 11:07:41 am »
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?

Yes, this is normal behavior. The internal XML representation is even worse:
Code: [Select]
<leaf name="1word" fi="1" np="1" tc0="0" fc0="268435199" fn0="TI-Nspire Sans" fs0="9" fst0="0" fest0="0" feun0="0" fesub0="0" fesup0="0" cc0="0" pp0="4">What </leaf>
                <leaf name="1word" fi="1" np="1" tc0="0" fc0="268435199" fn0="TI-Nspire Sans" fs0="9" fst0="0" fest0="0" feun0="0" fesub0="0" fesup0="0" cc0="0" pp0="3">the </leaf>
                <leaf name="1word" fi="1" np="1" tc0="0" fc0="268435199" fn0="TI-Nspire Sans" fs0="9" fst0="0" fest0="0" feun0="0" fesub0="0" fesup0="0" cc0="0" pp0="4">heck</leaf>

This would be for three words without markup and all the same style. Grouping the words together works but then you can get into problems with line breaks. So, it's absolutely normal that you have to repeat and repeat your stuff^^

Offline Goran

  • LV2 Member (Next: 40)
  • **
  • Posts: 32
  • Rating: +0/-0
    • View Profile
Re: Creating/formating an evaluated Math Box from Lua script?
« Reply #6 on: July 23, 2013, 02:58:56 pm »
One more detail:

Are by any chances known/do exist '\\1' formatting commands for font sizes (mixing of sizes within the same D2Editor?
Is this possible at all?

Regards,
Goran

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Creating/formating an evaluated Math Box from Lua script?
« Reply #7 on: July 26, 2013, 10:53:32 am »
Sorry for the late reply. I don't think it's possible. The sizes are actually 'parameters' for the 1word, 1title, etc commands, but as we can't supply commands using the \keyword syntax it isn't possible.