Interactive variables#
Bind a control to a variable#
Write an assignment followed by a control directive. Moving the control changes the bound value and refreshes the cells and plots that depend on it.
Each control needs a named binding, so a standalone numeric expression has no variable to control.
a = 2 // @slider(min=0, max=5, step=0.1)
plot(sin(a * x))Interactive examples could not load. You can still copy the expressions.
Changes in documentation examples are temporary. In a note, a committed control change updates its assignment and can be undone. Editing the source changes the control definition.
Slider#
@slider(min=...,max=...,step=...) controls a number. Min and max are required, and step is optional. Add tick=true to request ticks.
a=2 // @slider(min=0,max=5,step=0.1)
a^2Interactive examples could not load. You can still copy the expressions.
Range#
Bind a two-number vector and use @range for two handles. It shares the slider's min, max, optional step, and tick settings.
r=[1,4] // @range(min=0,max=5,step=0.1)
r[2]-r[1]Interactive examples could not load. You can still copy the expressions.
Stepper#
@stepper(min=...,max=...) changes an integer with plus and minus controls. Use integer bounds and an integer initial value.
n=3 // @stepper(min=0,max=10)
n^2Interactive examples could not load. You can still copy the expressions.
Switch#
Bind true or false, then use @switch with no arguments.
enabled=true // @switch
enabled ? 10 : 0Interactive examples could not load. You can still copy the expressions.
Select#
@select(options=[...],style=dropdown) picks from a nonempty list. Use style=segmented for a segmented control.
Options accept the supported scalar strings, numbers, and booleans. Enter an initial value that matches the representation of the option you intend.
mode="small" // @select(options=["small","large"],style=segmented)
modeInteractive examples could not load. You can still copy the expressions.
Syntax and troubleshooting#
A directive can follow the assignment or sit on its own comment line. Named arguments use = and commas, and completion shows the accepted fields and values. Missing bounds, invalid options, or a mismatched binding can each prevent a useful control from appearing.
Place a bound variable above the cells that read it. Keep slider and animation parameter names separate when they represent different inputs. A whole drag is one undo step, and the transient drag values are separate from the final committed assignment.
Directives and plot options · Animating plots · Variables, globals and REPL