OMD Documentation

omdParenthesisNode

Represents a parenthetical grouping in a mathematical expression, such as (x + 2). This node is crucial for enforcing the correct order of operations and visually grouping parts of an expression.

Class Definition

export class omdParenthesisNode extends omdNode

Constructor

new omdParenthesisNode(astNodeData)

Creates a new omdParenthesisNode instance.

During construction, it creates omdGroupNode instances for the opening and closing parentheses and an omdNode for the inner expression.

Static Methods

fromString(expressionString)

Creates an omdParenthesisNode from a string representation of a parenthesized expression. The input string must start and end with parentheses.

Public Properties

Public Methods

computeDimensions()

Calculates the dimensions of the parenthesis node. It sums the widths of the opening parenthesis, the inner expression, and the closing parenthesis. The height is determined by the maximum height of these components plus a small amount of padding that scales with font size.

updateLayout()

Updates the layout of the parenthesis node and its children. It positions the inner expression and then centers the open and closed parentheses vertically around the mathematical baseline of the inner expression. This ensures proper visual alignment, especially for expressions containing superscripts or subscripts.

getAlignmentBaseline()

Returns the vertical y-coordinate for alignment. For a parenthesis node, this is the baseline of its inner expression.

clone()

Creates a deep, structural clone of the parenthesis node, including its inner expression and omdGroupNode children. The cloned node's provenance array is updated to include the original node's ID.

isConstant()

Checks if the inner expression is a constant value.

getValue()

Returns the numerical value of the inner expression, if it is constant.

toMathJSNode()

Converts the omdParenthesisNode back into its math.js AST representation. It creates a ParenthesisNode AST object with the converted inner expression.

toString()

Converts the node to its string representation, including the parentheses.

evaluate(variables)

Evaluates the inner expression of the parenthesis node.

isNecessary()

Checks if the parentheses are mathematically necessary to maintain the correct order of operations, based on the parent node's context and the type of the inner expression. For example, parentheses around a simple constant or variable are generally not necessary, but they are often required around complex expressions within a power or binary operation.

Internal Methods

Example

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

// Create a node representing (x + 2)
const innerExpressionAst = math.parse('x + 2');
const parenNode = new omdParenthesisNode({ content: innerExpressionAst });

// Set font size and render
parenNode.setFontSize(28);
parenNode.initialize();

// The simplification engine might remove these parentheses if they are not necessary
// const simplified = parenNode.simplify();

// Add to an SVG container to display
// const svgContainer = new jsvgContainer();
// svgContainer.addChild(parenNode);
// document.body.appendChild(svgContainer.svgObject);

See Also

↑ Top