Referencesubstitute

<substitute>

Substitutes math expressions into another expression

<substitute> is a General Operator component that replaces one character or string from a math or a text with another specified string.

Note that substitution is used for variables in math expressions, not numerical values. Doenet ignores requests for substitution of strict numerical values in math expressions.

Attributes and Properties

Attributes for <substitute>

Number display (5)
avoidScientificNotation

boolean. Default value: false. Whether to render numbers in full decimal form rather than scientific notation.

displayDecimals

integer. Default value: 2. Number of decimal places to display when rendering this number.

displayDigits

integer. Default value: 3. Number of significant digits to display when rendering this number.

displaySmallAsZero

number. Default value: 1e-14. Threshold below which numbers are displayed as zero.

padZeros

boolean. Default value: false. Whether to pad displayed numbers with trailing zeros to fill the requested digits/decimals.

Other (7)

_componentWithSelectableType. Pattern to match in the source value.

boolean. Default value: false. Whether matching is case-sensitive (text mode).

boolean. Default value: false. Whether matching is restricted to whole words (text mode).

boolean. Default value: false. Whether the case of the original is preserved when substituting.

_componentWithSelectableType. Value to substitute in place of each match.

keyword. Level of simplification applied after substitution.

ValueDescription
none (default)No simplification is applied.
fullFully simplify the resulting expression.
numbersSimplify numeric subexpressions only, leaving symbolic structure intact.
numbersPreserveOrderLike numbers, but does not reorder commutative operands.
normalizeOrderReorder commutative operands into a canonical form without simplifying values.

keyword. Type of values being substituted.

ValueDescription
mathSubstitute within math expressions.
textSubstitute within text values.
Common to all components (9)
copy

reference. Create an independent copy of another component by reference. Enter a references a $name.

disabled

boolean. Default value: false. Whether this component is disabled and cannot be interacted with.

extend

reference. Extend another component by reference, inheriting its children and attributes. Enter a reference as $name.

fixed

boolean. Default value: false. Whether this component's value is fixed and cannot be modified.

fixLocation

boolean. Default value: false. Whether this component's location is fixed (preventing it from being moved while still allowing other modifications).

hide

boolean. Default value: false. Whether to hide this component from the rendered output.

isResponse

boolean. Default value: false. Whether this component is treated as a response for the purposes of assessment.

name

text. The name used to reference this component from elsewhere in the document.

styleNumber

integer. Default value: 1. The style number used to select this component's visual styling from the available style definitions.

Properties for <substitute name="s">

Number display (5)
$s.avoidScientificNotation

boolean. Whether to render numbers in full decimal form rather than scientific notation.

$s.displayDecimals

integer. Number of decimal places to display when rendering this number.

$s.displayDigits

integer. Number of significant digits to display when rendering this number.

$s.displaySmallAsZero

number. Threshold below which numbers are displayed as zero.

$s.padZeros

boolean. Whether to pad displayed numbers with trailing zeros to fill the requested digits/decimals.

Other (5)
$s.matchCase

boolean. Whether matching is case-sensitive (text mode).

$s.matchWholeWord

boolean. Whether matching is restricted to whole words (text mode).

$s.preserveCase

boolean. Whether the case of the original is preserved when substituting.

$s.simplify

text. Level of simplification applied after substitution.

$s.type

text. Type of values being substituted.

Common to all components (4)
$s.doenetML

text. The DoenetML source code that produced this component.

$s.hide

boolean. Whether to hide this component from the rendered output.

$s.isResponse

boolean. Whether this component is treated as a response for the purposes of assessment.

$s.styleNumber

integer. The style number used to select this component's visual styling from the available style definitions.

Examples

Example: substitute a different variable in a math

In this example, the <substitute> component is used to swap out one variable from a user-input expression for another. Two attributes are required:

  • match, which defines the content to replace
  • replacement, which defines the new content

The default type for substitution is math.

The modified expression is rendered by referencing the name of the <substitute> component with $sub.


Example: Replace one word with another in a text string

In this example, the <substitute> component is used to swap out one word from a user’s <textInput> for another. The type = "text" specification is made for the <substitute> component; this is required as the default type is math.

The modified text is rendered by referencing the name of the <substitute> component with $sub.


Example: substitute a user-provided value in a math

In this example, the user provides the substituted value in the math expression.

Attribute Examples

Attribute Example: type

The default substitution type is math. Use type="text" to switch the type of substitution to text.


Attribute Example: match, replacement

The match attribute specifies content to be replaced with the <substitution> component.

The replacement attribute specifies new content to be inserted with the <substitution> component.


Attribute Example: simplify

The <substitution> component accepts the standard simplify attribute for math components.


Attribute Example: matchWholeWord

By default, Doenet separates strings into individual, case-insensitive characters when finding matches for substitution. If a strict match of the entire word is desired, use the the matchWholeWord attribute


Attribute Example: matchCase

By default, matching is case-insensitive with the <substitution> component. Use matchCase to change this default behavior.


Attribute Example: preserveCase

To preserve the case of the letter in the original text string, use the preserveCase attribute.