Canvas expressions

The maths written bare in a canvas attribute — a curve’s expression, a point’s coordinates — its variables, the parameters controls set, and its function set.

#Writing a canvas expression

A canvas expression is maths written bare in a canvas attribute, without braces: curve(expression: 4.9x^2), point(x: 1; y: m). The environment reads it, not the compiler, and works it out as the page renders, so its letters can be the graph's variables and the parameters the reader moves.

Two operands side by side are multiplied, as in an expression in braces: 4.9x^2, 2(x + 1), 2 sin(x). Unlike one, it reads |x - a| as the absolute value, and deg after a number as degrees: 60deg.

A {{…}} expression inside a canvas expression is worked out first and its value spliced in as text: curve(expression: {{2}}x) is 2x. A @let constant reaches a canvas expression this way:

@let(g: 9.8)

graph{
    curve(expression: {{g / 2}}x^2)
}

#Variables and parameters

Each attribute names its variables: x for a curve on the ordinary plane and theta on the polar one; x and y for contour, surface and field; t for a parametric coordinate. y and z are refused where they are not variables — Unknown identifier 'y' in expression.

Every other single letter is a parameter, set by a control. Parameters belong to the scene, so a slider in one environment drives an expression in another. In a graph or region, a letter no control declares still gets a slider (Controls).

The line y = mx at m = 2, a point at (1, m) labelled m, and a slider for m beneath the plot.The line y = mx at m = 2, a point at (1, m) labelled m, and a slider for m beneath the plot.
graph{
    slider:m(range: [0, 5]; default: 2)
    curve#f(expression: m * x)
    point(x: 1; y: m; label: $m$)
}
Warning
Letters in a canvas expression are case-sensitive: T and t are two parameters.

#Runs of letters

Only the function names, pi, e, theta and deg are read as words. Any other run of letters is a product of single-letter parameters: kx is k·x, so ax^2 + bx + c reads as written. A run of more than one letter compiles with a warning:

curve(expression: sigma x)
Warning canvas/implicit-product: 'sigma' reads as the product s·i·g·m·a: a parameter here is a single letter, so this is 5 of them

Two runs are refused outright:

  • a run followed by brackets that is not a function — 'round' is not a function here; the functions are …;
  • a run that would splice a constant into a word — 'median' is not a name here: it reads as m·e·d·i·a·n, splicing 'e'.

#The function set

  • sin, cos, tan, asin, acos, atan, sinh, cosh and tanh, with arcsin, arccos, arctan, sine, cosine and tangent as other spellings;
  • exp, sqrt, and log and ln, both natural;
  • abs, also spelled absolute;
  • erf, gamma, lgamma, choose and factorial;
  • min, max and clamp.
The set differs from the named operators in braces. A canvas expression has no round, ceil, floor or log10, and its min and max take at least two arguments. An expression in braces has no lgamma, no other spellings and no |x|.

#Every parametised- value type

An attribute whose type begins parametised- takes a canvas expression where a plain value would otherwise stand: a point's coordinates may be (a, 2a), a curve's domain [0, b], an integral's bound from: m - s. The Value types page lists each type and what it accepts, and every reference page names the type of each attribute.

Last updated