OMD Documentation

omdVariableNode

Represents a variable (like x, y, a, b) in mathematical expressions. This node handles the visual representation of variables, their evaluation, and conversion to math.js AST.

Class Definition

export class omdVariableNode extends omdLeafNode

Constructor

new omdVariableNode(nodeData)

Creates a new omdVariableNode instance.

Static Methods

fromName(name)

Creates a variable node directly from a given name string. This is a convenience method for creating simple variable nodes without needing to construct a full AST object.

Public Properties

Public Methods

computeDimensions()

Calculates the dimensions of the node based on its text content, adding a small amount of padding around the variable name to improve visual spacing.

updateLayout()

Updates the layout of the node. This method primarily calls the superclass's updateLayout.

toMathJSNode()

Converts the omdVariableNode to a math.js-compatible AST format. It creates a SymbolNode with the variable's name, id, and provenance.

highlight(color)

Applies a highlight to the node's background and sets the variable's text color to white for better contrast. This method respects the isExplainHighlighted lock.

clearProvenanceHighlights()

Clears any provenance-related highlights from the node and resets the variable's text color to its default (black).

toString()

Converts the variable node to its string representation, which is simply its name.

evaluate(variables)

Evaluates the variable by looking up its value in the provided variables map. If the variable is not defined in the map, it throws an error.

Internal Methods

Examples

Creating Variables

import { omdVariableNode } from '@teachinglab/omd';
import * as math from 'mathjs';

// From a name string
const nodeX = omdVariableNode.fromName('x');
const nodeTheta = omdVariableNode.fromName('θ');

// From AST data (e.g., from math.js parse result)
const astNode = math.parse('y');
const nodeY = new omdVariableNode(astNode);

// Direct string name (less common, but supported)
const nodeZ = new omdVariableNode('z');

Rendering Variables

import { omdVariableNode } from '@teachinglab/omd';
// import { jsvgContainer } from '@teachinglab/jsvg'; // if rendering directly

const node = omdVariableNode.fromName('x');
node.setFontSize(24);
node.initialize(); // Computes dimensions and layout

// To render, typically add to a parent node or an omdDisplay
// const svgContainer = new jsvgContainer();
// svgContainer.addChild(node);
// document.body.appendChild(svgContainer.svgObject);

See Also

↑ Top