Skip to main content

TangleTex

TangleTex is a MathTex with numbers you can change in place, like wigglystuff's TangleLatex, rendered on the canvas. Mark numbers with \tangle{name} and matrices with \tanglematrix{name}. Every number is backed by a ValueTracker, so the formula follows animations and updaters. A matrix collapses to a symbol and animates open with scene.play(tex.expand(name)). makeTangle(tex, scene) adds the pointer and keyboard interaction.

The plane is transformed live by R, and the green arrow is r = R x. Rest the mouse on R or x to expand it. Drag any number left or right, or click it to type a value. With the keyboard, tab to a number and use the arrow keys. The entries of r are readOnly parameters: the example sets them from the other values, and the reader cannot drag them.

Usage​

import { TangleTex, makeTangle } from 'manim-web';

const tex = new TangleTex({
latex: String.raw`\vec y = \tanglematrix{R}\,\vec x + \tangle{b}`,
anchor: 'left', // keep the left edge still while widths change
parameters: {
k: { value: 2, min: 0, max: 5, step: 0.1, color: '#ffcc33' },
t: { value: 0.5, step: 0.05, display: 'symbol', symbol: '\\theta' },
b: { value: 1, step: 0.5 },
},
matrices: {
R: {
entries: [
[String.raw`\tangle{k}\cos\tangle{t}`, String.raw`-\tangle{k}\sin\tangle{t}`],
[String.raw`\tangle{k}\sin\tangle{t}`, String.raw`\tangle{k}\cos\tangle{t}`],
],
},
},
});
scene.add(tex);

makeTangle(tex, scene, { onChange: (name, value) => redraw() });

A repeated marker (k above appears four times) is one value: dragging any copy changes them all.

Animating​

await scene.play(tex.expand('R')); // symbol morphs into the matrix
await scene.play(tex.getTracker('k').animateTo(3)); // every k updates
await scene.play(tex.collapse('R'), tex.getTracker('b').animateTo(2));

tex.setValue(name, value) (snapped to step, clamped to [min, max]), tex.setExpanded(name, expanded) and tex.reset() change the formula without animation. tex.getValues() returns every value, and tex.getMatrix(name) returns a matrix as numbers.

Parameters​

OptionDefaultMeaning
valuerequiredInitial value, in [min, max].
min, max-Infinity, +InfinityBounds.
step1Snap increment.
digitsdecimals of stepDecimal places shown.
display'number''symbol' shows symbol until dragged or edited.
symbolthe nameLaTeX shown for display: 'symbol'.
colorthe formula colorColor of the number's glyphs.
pixelsPerStep7Drag distance for one step.
labelparameter <name>Accessible name.
readOnlyfalseShown but not draggable, editable or focusable; set it with setValue.

Matrices​

OptionDefaultMeaning
entriesrequiredRows of cells: LaTeX strings with \tangle markers, or numbers.
symbolthe nameLaTeX shown while collapsed.
brackets'bmatrix''bmatrix', 'pmatrix', 'vmatrix', 'Bmatrix' or 'Vmatrix'.
cellnoneParameter options for number cells.
expandedfalseStart expanded.
colornoneColor of the matrix glyphs (parameter colors still apply inside).
evaluatenone(values) => number[][], for getMatrix() when cells are expressions.

A number cell becomes a parameter named <matrix>_<row>_<col>, counted from 1: entries: [[1], [0.5]] on matrix X creates X_1_1 and X_2_1.

makeTangle options​

OptionDefaultMeaning
editor'popover'Click-to-type field: 'popover', 'inline' or 'none'.
revealAllOnDragfalseShow every 'symbol' parameter as a number while dragging.
expandOn'hover'How the mouse expands a matrix: 'hover', 'click' or 'none'. Touch taps.
expandDuration0.4Seconds for the expand / collapse animation; 0 switches instantly.
openDelay150Milliseconds of hovering before a matrix expands.
closeDelay400Milliseconds after leaving before a hover-opened matrix collapses.
hitPadding0.08Extra world-unit margin around glyphs that still counts as a hit.
keyboardtrueAdd hidden, focusable buttons for keyboard and screen-reader users.
autoRendertrueRender after each change. Set false if a render loop is live.
onChangenone(name, value, tex) => void, on each change.
onScrubEndnone(name, value, tex) => void, once when a drag or edit ends.
onExpandChangenone(name, occurrence, expanded) => void.
onExpandEndnone(name, occurrence, expanded) => void, once the matrix has finished opening or closing.
onBeforeRendernone(tex) => void, right before each render Tangle makes; sync things that depend on the formula's size here.

Clicking a hover-opened matrix keeps it open. While another scene.play() is running, hover expands instantly instead of animating, because a second play() would replace the running timeline.

Keyboard​

Each parameter and each matrix marker gets a visually hidden button next to the canvas. On a parameter, the arrow keys change the value by one step (ten with Shift), Home and End jump to min and max, and Enter types a value. On a matrix, Enter or Space expands or collapses it. A ring outlines the focused item, or the matrix symbol when the parameter is inside a collapsed matrix.