Notes in expressionlist

115 views
Skip to first unread message

Appolonia K

unread,
Jan 5, 2018, 1:21:48 PM1/5/18
to desmos-api-discuss
Hello,

It is not clear to me how I can add notes to the expressionlist

I have tried e.g.    
   {id:'note', latex:'\\"\\left\\{ This is a note \\right\\}',},

But that just leaves an empty slot

something like
      {id:'notes', latex:'\\text\\left\\{ This is a note \\right\\}', type: 'note'},

just leaves the row out altogether.

What is the correct syntax to add notes in the expressionlist?

Thanks for any help with this


Chris Lusto

unread,
Jan 5, 2018, 1:51:14 PM1/5/18
to desmos-api-discuss
Hi Appolonia,

We currently don't support adding notes programmatically, just expressions and tables. I think this is the first time someone has asked about adding notes through the API. Curious why this would be useful for you compared to just authoring the notes in the calculator and using getState/setState.

Appolonia K

unread,
Jan 8, 2018, 1:52:20 PM1/8/18
to desmos-api-discuss
Hello again, 
Thank you for your reply. It actually never occurred to me to save the state -  it was such a minimal application. 
However, there seems to be a problem with range restrictions when setting states 

"latex":"f(x)=w(x-q)+p\\left\\{x\\ge0\\right\\}"

seems to make the state not parseable by JSON and consequently not possible to load the saved state 


VM9774:1 Uncaught SyntaxError: Unexpected token l in JSON at position 1182
    at JSON
.parse (<anonymous>)
    at test_spara_state
.html:37

There seems to be something with the escaped latex, because when I remove the range restriction, it works 

Jason Merrill

unread,
Jan 8, 2018, 1:59:51 PM1/8/18
to desmos-api-discuss
The following works correctly for me:
Calc.setState({
  "version": 5,
  "graph": {
    "viewport": {
      "xmin": -10,
      "ymin": -6.597294484911551,
      "xmax": 10,
      "ymax": 6.597294484911551
    }
  },
  "expressions": {
    "list": [
      {
        "type": "expression",
        "id": "2",
        "color": "#2d70b3",
        "latex": "f(x)=w(x-q)+p\\left\\{x\\ge0\\right\\}"
      }
    ]
  }
})

I got this state by running
> Calc.setExpression({"latex":"f(x)=w(x-q)+p\\left\\{x\\ge0\\right\\}"})
> copy(Calc.getState())
in the Chrome dev tools console, starting with a blank slate.

If you're still having trouble getting this working correctly, could you post more exact detail of what code you ran when you saw the error?

Based on the message, I'm guessing that what might have happened is that you turned a JS object literal into a string by just putting quotes around it instead of running JSON.stringify on it, which will take care of adding extra escapes to backslashes and such.

Appolonia K

unread,
Jan 11, 2018, 2:34:37 PM1/11/18
to desmos-api-discuss
Yes, thank you, I did not stringify the raw string again. Originally I only did JSON.stringfy to create the string. Below works a charm, thank you! 

var state = JSON.stringify({"version":5,"graph":{"squareAxes":false, [...] "latex":"b=\\left(q_1,u\\left(q_1\\right)\\right)","color":"#2d70b3","style":"POINT"}]}})
 
var elt = document.getElementById('calculator0');
 
var calculator = Desmos.GraphingCalculator(elt, {
  keypad
: false
 
//, expressionsTopbar: false
 
, lockViewport: false
 
});
calculator
.setState(state);

Jason Merrill

unread,
Jan 11, 2018, 2:58:25 PM1/11/18
to desmos-api-discuss
Great, glad you got this working.

Just a note: the JSON.stringify call here isn't actually necessary. calculator.setState accepts object representations of a calculator state, and that's actually the preferred/documented form. If you do pass a string, we will call JSON.parse on it to attempt to turn it into an object; that's what's happening here.
Reply all
Reply to author
Forward
0 new messages