Can you do piecewise functions and domain/range in latex?

346 views
Skip to first unread message

Spencer Clark

unread,
Jun 19, 2017, 4:41:35 PM6/19/17
to desmos-api-discuss
Hi,

Self-learning web developer here. I'm trying to set an expression to be a piecewise function with calculator.setExpressions. However, they do not seem to want to recognize the curly braces, which is the only way I know to make it work. I tried a workaround by doing a domain/range restriction, but those also use curly braces. If I set latex = 'y={0<x<3:x}', when I load the webpage the expression just has y=0<x<3:x with an error saying it doesn't understand. I can manually add the curly brace before the 0 then it graphs fine, but is there a way to have latex actually put in the curly brace for a piecewise function? I know curly braces are already used for something else (like clarifying division and exponents), and that's probably part of it.

Jason Merrill

unread,
Jun 19, 2017, 4:50:24 PM6/19/17
to desmos-api-discuss
The issue here is that the latex property passed into setExpression has to use LaTeX syntax, and in LaTeX, curly braces need to be escaped with backslashes in order to be printed (otherwise, they function as grouping operators and are not printed).

If you write expressions in the calculator, and then copy-paste them, they will paste as LaTeX. Doing this for the expression you asked about gives

y=\left\{0<x<3:x\right\}

There's one more wrinkle though: in Javascript strings, backslashes must be escaped by an additional backslash, so if I want to use this in a call to setExpression, it should look like

calc.setExpression({ latex: 'y=\\left\\{0<x<3:x\\right\\}' });

I wish that the system gave better feedback about LaTeX syntax errors, and it's something I'd like to improve in the future, but it wouldn't actually help in this case (and many other cases where this issue comes up), because the string you're using is valid LaTeX; it just means something different than what you intended.

Spencer Clark

unread,
Jun 20, 2017, 1:23:04 PM6/20/17
to desmos-api-discuss
Thanks, that worked.

At first I did try to copy and paste and got a lot of \left\ and stuff like that, but I did not know it was part of the syntax, and even if I did I probably would have forgotten about escaping the backslashes. Thanks again.
Reply all
Reply to author
Forward
0 new messages