Macros
A macro is an expression with a name. Wherever the name appears it is replaced by that expression before anything is compiled, so Desmos never learns that a macro was involved - the graph is the one you would have written by hand.
Two kinds. Without a parameter list it stands for one expression:
macro TAU = 6.283185With one, it takes arguments and puts them into its body:
macro LERP(a, b, t) = a + (b - a) * tA macro stands for an expression and nothing else - never a statement, a block or a run of metadata. Reusable styling is a different thing, a style, and 19-styles.axis is about those; the end of this file shows the two side by side.
config { xmin: -7 xmax: 7 ymin: -3 ymax: 3}
macro TAU = 6.283185macro WAVE(k, phase) = sin(k * x + phase)
"Constants"
// A macro is not a variable: nothing named TAU reaches the graph, and the// number is written into each expression that used it.y = sin(TAU * x / 4) @ color: RED, lineWidth: 3period = TAU / 4 @ color: RED
"Arguments"
// An argument may be anything an expression may hold - a number, a name, an// expression, another macro.y = WAVE(1, 0) @ color: BLUEy = WAVE(2, TAU / 4) @ color: GREENy = WAVE(1, 0) + WAVE(2, TAU / 4) @ color: PURPLE, lineWidth: 3
// The substitution is made on the parsed expression, not on its text, so an// argument keeps its own grouping without brackets: this is sin((1 + 2) * x),// never sin(1 + 2 * x).y = WAVE(1 + 2, 0) @ color: ORANGE, lineOpacity: 0.4
// A macro is in scope for the whole file, wherever it is written and whatever// it is written above - so it belongs at the top level, outside every block. Its// body may be a whole equation, which then stands as a statement of its own.macro CIRCLE(size) = x ^ 2 + y ^ 2 = size ^ 2
folder "Everywhere an expression goes" { // Inside a block is no different: the substitution happens first, and // what comes out of it is read as the folder's entries. CIRCLE(1) @ color: BLACK, lineStyle: DASHED CIRCLE(2) @ color: BLACK, lineOpacity: 0.3}
"Macros and styles"
// Metadata is not an expression, so no macro can stand for it. A run of// properties written once and used on many statements is a style instead: it// has a name of its own, and `use:` applies it. Macros make expressions, styles// make metadata, and each is checked as what it is.style swatch { pointSize: 14; showLabel }style faint { color: #7b7b7b; lineOpacity: 0.35; lineStyle: DOTTED }
(-2, 1) @ use: swatch, color: RED, label: "one"(0, 1) @ use: swatch, color: BLUE, label: "two"(2, 1) @ use: swatch, color: GREEN, label: "three"
y = x @ use: fainty = -x @ use: faintOpen in playground →