@mathzetta/inlinemath 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +37 -33
  2. package/dist/cas.external.js +2 -2
  3. package/dist/cas.js +2 -2
  4. package/dist/chunks/calculate-BGMEvf3f.js +2 -0
  5. package/dist/chunks/draw-q86mDDWm.js +2 -0
  6. package/dist/chunks/edit-CTMRUElt.js +2 -0
  7. package/dist/chunks/edit-D2RO7fg0.js +2 -0
  8. package/dist/chunks/html-1wCO6EpZ.js +2 -0
  9. package/dist/chunks/html-D5QiXGXD.js +2 -0
  10. package/dist/chunks/lite-Bo8THzyq.js +2 -0
  11. package/dist/fonts/README-inlinemath-greek.txt +15 -0
  12. package/dist/fonts/fonts.css +19 -1
  13. package/dist/fonts/inlinemath-greek-italic.woff2 +0 -0
  14. package/dist/fonts/inlinemath-greek-regular.woff2 +0 -0
  15. package/dist/inlinemath-cas.external.global.min.js +2 -2
  16. package/dist/inlinemath-cas.global.min.js +2 -2
  17. package/dist/inlinemath-lite-calc.global.min.js +2 -2
  18. package/dist/inlinemath-lite.external.global.min.js +2 -2
  19. package/dist/inlinemath-lite.global.min.js +2 -2
  20. package/dist/inlinemath.css +17 -7
  21. package/dist/inlinemath.external.global.min.js +2 -2
  22. package/dist/inlinemath.external.js +2 -2
  23. package/dist/inlinemath.global.min.js +2 -2
  24. package/dist/inlinemath.js +2 -2
  25. package/dist/lite-calc.js +2 -2
  26. package/dist/lite.external.js +2 -2
  27. package/dist/lite.js +2 -2
  28. package/dist/markdown.external.js +2 -2
  29. package/dist/markdown.js +2 -2
  30. package/fonts/README-inlinemath-greek.txt +15 -0
  31. package/fonts/fonts.css +19 -1
  32. package/fonts/inlinemath-greek-italic.woff2 +0 -0
  33. package/fonts/inlinemath-greek-regular.woff2 +0 -0
  34. package/inlinemath.css +17 -7
  35. package/package.json +6 -6
  36. package/src/cas.d.ts +1 -1
  37. package/src/cas.js +15 -4
  38. package/src/draw.js +13 -9
  39. package/src/edit.js +11 -4
  40. package/src/html.js +1 -1
  41. package/src/index.js +1 -1
  42. package/src/layout.js +89 -32
  43. package/src/lite.js +1 -1
  44. package/src/markdown.d.ts +1 -1
  45. package/src/markdown.js +139 -57
  46. package/src/mathzetta.js +2 -2
  47. package/src/select.js +7 -0
  48. package/src/types.d.ts +12 -10
  49. package/src/variables.js +44 -10
  50. package/src/view.js +69 -18
  51. package/dist/chunks/calculate-Cz87Lt0N.js +0 -2
  52. package/dist/chunks/draw-Bev17BJJ.js +0 -2
  53. package/dist/chunks/edit-C65xXzo8.js +0 -2
  54. package/dist/chunks/edit-M68aRA5o.js +0 -2
  55. package/dist/chunks/html-BimAqn7x.js +0 -2
  56. package/dist/chunks/html-CupluJSh.js +0 -2
  57. package/dist/chunks/lite-3Fb-OHbt.js +0 -2
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Inline Math
2
2
 
3
- Draws calculus-level maths as plain HTML and CSS (the `@mathzetta/inlinemath` package, at inlinemath.com). The input is anything
3
+ Draws calculus-level math as plain HTML and CSS (the `@mathzetta/inlinemath` package, at inlinemath.com). The input is anything
4
4
  [MathZetta](https://github.com/triplelog/mathzetta/tree/main/packages/mathzetta) reads (`sin^2 x`, `\frac{d}{dx}(x^2)`, `x**2/2`, `√x`).
5
- Every part of the drawing can be found, coloured, clicked and animated.
5
+ Every part of the drawing can be found, colored, clicked and animated.
6
6
 
7
7
  Inline Math is not a LaTeX engine. It covers what a Calculus course writes:
8
8
  - fractions, exponents and subscripts, radicals, brackets that grow;
@@ -35,6 +35,7 @@ render(div, 'x^2', { announce: true }); // read each new step to a screen re
35
35
  view.announce('Step 2: expand'); // your own words, through the same region
36
36
  view.segments(); // [{ text: 'x', id: 3 }, { text: 'squared', id: 5 }, …]
37
37
  view.editable({ toText, onEdit: ({ text }) => … }); // double-click a part (or F2 on it) to edit it in place
38
+ view.destroy(); // stop listening (handlers, keys, editing) before drawing a new view into el
38
39
  ```
39
40
 
40
41
  **Editing a part.** `view.editable({ toText })` (`import { toText } from 'mathzetta/text'`; in the
@@ -46,7 +47,7 @@ as text, the edited part's path and what was typed; `view.edit(target)` opens th
46
47
 
47
48
  **Keyboard and screen readers.** Every drawing has an accessible name (`role="math"`, its spoken form for a listener
48
49
  who can't see it). Once a view listens for `'click'`, the drawing is one tab stop: the arrow keys walk its parts (Down
49
- into a part, Up out, Left/Right between neighbours, Home or Escape back to the whole), each part is spoken through a
50
+ into a part, Up out, Left/Right between neighbors, Home or Escape back to the whole), each part is spoken through a
50
51
  polite live region and ringed, and Enter or Space calls the click listener for the current part (`event` is then the
51
52
  KeyboardEvent). `view.focusPart(node)` moves there from code.
52
53
 
@@ -65,7 +66,7 @@ import '@mathzetta/inlinemath/fonts/fonts.css'; // optional: Latin Modern, th
65
66
 
66
67
  ### Lite: drawing only
67
68
 
68
- `@mathzetta/inlinemath/lite` is `toHTML()` and nothing live: the same layout, glyphs, colours, highlights and
69
+ `@mathzetta/inlinemath/lite` is `toHTML()` and nothing live: the same layout, glyphs, colors, highlights and
69
70
  spoken labels for screen readers, without the live view (`render`, step animation, events), Markdown
70
71
  or the CAS — about a third smaller. `{{…}}` blocks are worked out only if MathZetta's calculator is
71
72
  loaded too (`import 'mathzetta/calculate'`, or `@mathzetta/inlinemath/lite-calc`, which brings it); otherwise
@@ -94,14 +95,14 @@ npm test # 253 checks; every input in mathzetta/test/cases/calculus.tsv
94
95
  | `src/layout.js` | Turns a tree into HTML. All notation choices live here. |
95
96
  | `src/glyphs.js` | SVG paths for `( ) { } √ ∫ ∑ ∏`. Change a shape here. |
96
97
  | `src/select.js` | Picks parts by id, expression, path or test. Also `numberTree`. |
97
- | `src/view.js` | The live view: highlight, colour, events, speech segments, step animation. |
98
+ | `src/view.js` | The live view: highlight, color, events, speech segments, step animation. |
98
99
  | `src/html.js` | `toHTML`, `treeOf`, and the `colors` / `highlight` options. |
99
100
  | `inlinemath.css` | All the styling. The numbers marked *metric* must match the constants in layout.js. |
100
101
 
101
- ## How it lays maths out
102
+ ## How it lays math out
102
103
 
103
104
  - **Estimated, not measured.** Each piece knows roughly how far it reaches above and below the baseline, in em.
104
- - From that, `layout.js` writes `vertical-align` values that put fraction bars on the maths axis, lift exponents above tall bases, and size `∫` to its integrand.
105
+ - From that, `layout.js` writes `vertical-align` values that put fraction bars on the math axis, lift exponents above tall bases, and size `∫` to its integrand.
105
106
  - The same string comes out in Node, in a browser or in a video frame, with no layout pass after rendering.
106
107
  - **Self-stretching delimiters.** Brackets, bars, floors and braces are drawn at `height: 100%` of what they enclose (SVG or CSS borders), so they need no estimate at all.
107
108
  - This is the best idea from inline-math's imcss renderer. Inline Math fixes its aspect-ratio bug: shapes use `preserveAspectRatio="none"` with a fixed width.
@@ -121,7 +122,8 @@ These go to `toHTML(input, options)` and `render(el, input, options)`.
121
122
  | `derivativeBrackets` | `'['` | `'('` for d/dx (x² + 1) |
122
123
  | `scriptFractions` | `'slash'` | x^{3/2} written with a slash. `'stack'` stacks it. |
123
124
  | `cases` | `true` | `c ? a : b` drawn as a piecewise brace |
124
- | `colors` | | `{ 'x^2': 'red', 5: 'blue' }` (number keys are ids) or `[[target, colour]]` |
125
+ | | | A product typed with `·` (`\cdot`) or `×` (`\times`) is drawn with that sign (MathZetta's `node.written`); other products get a sign only where one is needed. Postfix input has no such marks. |
126
+ | `colors` | | `{ 'x^2': 'red', 5: 'blue' }` (number keys are ids) or `[[target, color]]` |
125
127
  | `highlight` | | target(s) that get the `mv-hl` class |
126
128
  | `attributes`, `style` | | `(node) => ({…})` / `(node) => 'css'` for each node's element |
127
129
  | `mathml` | | MathZetta's `toMathML` (`import { toMathML } from 'mathzetta/mathml'`): also write the expression as visually hidden MathML (no ids), which screen readers can step through part by part and show in math braille; the drawing stays hidden from them and the label is dropped (MathML carries the meaning). A keyboard-enabled view keeps its label on the focusable element |
@@ -144,8 +146,8 @@ npm run build # dist/ (a few seconds)
144
146
 
145
147
  - **`dist/`** — Inline Math with everything inside, no import map or `node_modules` needed:
146
148
  - `inlinemath.js`, `markdown.js`, `cas.js` — ES modules (shared code in `dist/chunks/`);
147
- - `inlinemath.global.min.js` (136 KB, 46 KB gzipped) — a classic `<script>`: `window.InlineMath`, with Inline Math and the Markdown;
148
- - `inlinemath-cas.global.min.js` (270 KB, 87 KB gzipped) — the same plus the CAS (`InlineMath.mathCAS`, `casBlocks`, `stepsHTML`);
149
+ - `inlinemath.global.min.js` (183 KB, 62 KB gzipped) — a classic `<script>`: `window.InlineMath`, with Inline Math and the Markdown;
150
+ - `inlinemath-cas.global.min.js` (323 KB, 104 KB gzipped) — the same plus the CAS (`InlineMath.mathCAS`, `casBlocks`, `stepsHTML`);
149
151
  - `inlinemath.css` and `fonts/` — put both on the page.
150
152
 
151
153
  ```html
@@ -183,32 +185,34 @@ page needs nothing else:
183
185
 
184
186
  `@0.2` follows the latest 0.2.x; `@0.2.0` pins a release.
185
187
 
186
- **The stylesheet is required** — the drawing is HTML laid out by `inlinemath.css` (6.9 KB, 2.1 KB
188
+ **The stylesheet is required** — the drawing is HTML laid out by `inlinemath.css` (10.4 KB, 3.2 KB
187
189
  gzipped): without it fractions, radicals and brackets don't line up. The classic builds
188
190
  (`*.global.min.js`) carry it and add it to the page themselves (`<style id="inlinemath-css">`; a
189
191
  `<link>` to `dist/inlinemath.css` is still fine, it is only added when absent); the ES modules
190
192
  don't, so a page using them links `dist/inlinemath.css`. **The fonts are optional**: `fonts/fonts.css`
191
- declares Latin Modern Roman (the TeX look) in two files, regular 45 KB and italic 47 KB (woff2), each
192
- downloaded only when text uses it; without the link the maths is drawn in the system serif.
193
+ declares Latin Modern Roman (the TeX look) in two files, regular 45 KB and italic 47 KB (woff2), and
194
+ its Greek letters, which Latin Modern lacks, from New Computer Modern in the same design (10 KB each,
195
+ `fonts/README-inlinemath-greek.txt`). Each file is downloaded only when text uses it, so a page without
196
+ Greek never fetches those two; without the link the math is drawn in the system serif.
193
197
 
194
198
  Every build comes **with MathZetta inside, or without it** (`*.external.*`), with or without the
195
199
  CAS (MathZetta CAS). Sizes, gzipped:
196
200
 
197
201
  | | ES module | classic `<script>` (`window.InlineMath`) |
198
202
  |---|---|---|
199
- | Lite (drawing only), MathZetta inside | `lite.js` 32 KB | `inlinemath-lite.global.min.js` 33 KB |
200
- | Lite with the calculator (`{{…}}` worked out) | `lite-calc.js` 41 KB | `inlinemath-lite-calc.global.min.js` 43 KB |
201
- | Lite without MathZetta | `lite.external.js` 7 KB | `inlinemath-lite.external.global.min.js` 9 KB |
202
- | Inline Math (+ Markdown in the classic build), MathZetta inside | `inlinemath.js` 42 KB, `markdown.js` | `inlinemath.global.min.js` 46 KB |
203
- | …without MathZetta | `inlinemath.external.js` **8 KB**, `markdown.external.js` | `inlinemath.external.global.min.js` 13 KB |
204
- | with the CAS, MathZetta inside | `cas.js` 82 KB | `inlinemath-cas.global.min.js` 87 KB |
205
- | with the CAS, without MathZetta | `cas.external.js` 40 KB | `inlinemath-cas.external.global.min.js` 52 KB |
203
+ | Lite (drawing only), MathZetta inside | `lite.js` 35 KB | `inlinemath-lite.global.min.js` 38 KB |
204
+ | Lite with the calculator (`{{…}}` worked out) | `lite-calc.js` 48 KB | `inlinemath-lite-calc.global.min.js` 52 KB |
205
+ | Lite without MathZetta | `lite.external.js` 8 KB | `inlinemath-lite.external.global.min.js` 11 KB |
206
+ | Inline Math (+ Markdown in the classic build), MathZetta inside | `inlinemath.js` 51 KB, `markdown.js` 55 KB | `inlinemath.global.min.js` 62 KB |
207
+ | …without MathZetta | `inlinemath.external.js` **12 KB**, `markdown.external.js` 15 KB | `inlinemath.external.global.min.js` 21 KB |
208
+ | with the CAS, MathZetta inside | `cas.js` 90 KB | `inlinemath-cas.global.min.js` 104 KB |
209
+ | with the CAS, without MathZetta | `cas.external.js` 50 KB | `inlinemath-cas.external.global.min.js` 63 KB |
206
210
 
207
211
  Without MathZetta, the page loads it once itself: a classic page puts a MathZetta `<script>` first —
208
- `mathzetta.min.js`, or the `maths-display` flavour, since speech, text and calculate are used (for
212
+ `mathzetta.min.js`, or the `maths-display` flavor, since speech, text and calculate are used (for
209
213
  lite, `display`, or `maths-display` to work out `{{…}}`) — and
210
214
  the external classic build reads `window.MathZetta`; a module page maps the names with an import
211
- map (every name can point at the one MathZetta bundle). The fonts are 94 KB more, only if linked.
215
+ map (every name can point at the one MathZetta bundle). The fonts are 94 KB more (and 20 KB for Greek, only where it appears), only if linked.
212
216
 
213
217
  ```html
214
218
  <script type="importmap">
@@ -223,7 +227,7 @@ map (every name can point at the one MathZetta bundle). The fonts are 94 KB more
223
227
 
224
228
  Publishing is `npm publish` (`prepublishOnly` runs the tests and the build).
225
229
 
226
- ## Markdown with maths: `@mathzetta/inlinemath/markdown`
230
+ ## Markdown with math: `@mathzetta/inlinemath/markdown`
227
231
 
228
232
  ```js
229
233
  import { markdownToHTML } from '@mathzetta/inlinemath/markdown';
@@ -231,14 +235,14 @@ el.innerHTML = markdownToHTML(text); // needs inlinemat
231
235
  el.innerHTML = markdownToHTML(text, { blocks: casBlocks() }); // {{solve(…)}}, {{diff(…)}} too
232
236
  ```
233
237
 
234
- - **Maths:** `$…$` inline, `$$…$$` or a ` ```math ` fence on its own (centred), and
238
+ - **Math:** `$…$` inline, `$$…$$` or a ` ```math ` fence on its own (centered), and
235
239
  `\(…\)` / `\[…\]`. MathZetta reads it, so LaTeX, plain text (`$sqrt(x)/2$`) and
236
240
  code all work.
237
- - **Working out:** `{{…}}` inside maths is worked out (the calculator, or the CAS with
241
+ - **Working out:** `{{…}}` inside math is worked out (the calculator, or the CAS with
238
242
  `blocks`). A definition like `$r = 3$` is remembered, exactly, for the `{{…}}` after it
239
243
  (`define: false` turns that off; `scope: { r: 3 }` gives values).
240
244
  - **Saved variables:** `$A := 2+2$` saves A (drawn A ≔ 2 + 2), and `$_A := 2+2$` saves it
241
- without drawing anything. After that, A in any maths is drawn as its expression and `{{A}}`
245
+ without drawing anything. After that, A in any math is drawn as its expression and `{{A}}`
242
246
  is its value, so `$A = {{A}}$` draws 2 + 2 = 4 and `$3A$` draws 3(2 + 2). A name is a letter
243
247
  (Greek too), optionally with a subscript (`A_1`); a variable may use the ones before it
244
248
  (`$_B := 2A$`), and saving it again replaces it from there on. A plain `$r = 3$` still only
@@ -262,18 +266,18 @@ el.innerHTML = markdownToHTML(text, { blocks: casBlocks() }); // {{solve(…)}},
262
266
  - **Editing by double-click:** in `renderMarkdownInto`, a part drawn from a textbox variable is
263
267
  edited where it is drawn with `{ editable: { toText } }` (the whole variable is written back
264
268
  into its textbox, so everything using it redraws); without `toText` the textbox is focused.
265
- Double-clicking any other part fires `inlinemath:select` on the element with where that maths
269
+ Double-clicking any other part fires `inlinemath:select` on the element with where that math
266
270
  is in the Markdown (`detail.start`, `detail.end`, `detail.raw`; for a part drawn from a
267
271
  variable saved in the same text, where it was saved), so an editor beside it can select it.
268
272
  `extractMath` gives every piece's `start` and `end` too.
269
- - **Dollars:** a `$` is maths only when it looks like it (Pandoc's rule): "$5 and $10"
273
+ - **Dollars:** a `$` is math only when it looks like it (Pandoc's rule): "$5 and $10"
270
274
  stays text, and `\$` is a dollar sign. Code spans and fences are left alone.
271
- - **Bad maths:** maths that doesn't read is shown as written, marked, with the reason as
275
+ - **Bad math:** math that doesn't read is shown as written, marked, with the reason as
272
276
  its tooltip.
273
277
  - **The Markdown:** a small built-in renderer — headings, emphasis, links, images, lists,
274
278
  quotes, code, tables, rules. Raw HTML is shown as text unless `html: true`, and
275
279
  `javascript:` links are dropped. `render: (md) => html` uses another renderer
276
- (marked, markdown-it); the maths is taken out first and put back into its output.
280
+ (marked, markdown-it); the math is taken out first and put back into its output.
277
281
  - **Options:** `math` passes options to every drawing (`fnParens`, `colors` …);
278
282
  `delimiters` picks which marks count.
279
283
  - **Playground:** [inlinemath.com/markdown.html](https://inlinemath.com/markdown.html), an editor with a live preview.
@@ -281,14 +285,14 @@ el.innerHTML = markdownToHTML(text, { blocks: casBlocks() }); // {{solve(…)}},
281
285
  ## The CAS: `@mathzetta/inlinemath/cas`
282
286
 
283
287
  MathZetta CAS (exact simplify, factor, solve, d/dx, ∫ …; @mathzetta/cas's docs/cas.md)
284
- in drawn maths:
288
+ in drawn math:
285
289
 
286
290
  ```js
287
291
  import { toHTML } from '@mathzetta/inlinemath';
288
292
  import { casBlocks, stepsHTML, mathCAS } from '@mathzetta/inlinemath/cas';
289
293
 
290
294
  toHTML("f'(x) = {{diff(x^3 sin(x), x)}}", { blocks: casBlocks() })
291
- toHTML('{{solve(x^2 - 5x + 6 = 0, x)}}', { blocks: casBlocks() }) // x = 2 ∨ x = 3
295
+ toHTML('{{solve(x^2 - 5x + 6 = 0, x)}}', { blocks: casBlocks() }) // x = 2 or x = 3
292
296
  el.innerHTML = stepsHTML(mathCAS().solve('sqrt(x + 1) = x - 1', 'x', { steps: 'brief' }).steps)
293
297
  ```
294
298
 
@@ -323,5 +327,5 @@ playground: any input, any operation, detail level, and a live rule editor).
323
327
  ## Known rough edges
324
328
 
325
329
  - **Fonts.** The placement estimates are tuned for Latin Modern and Times-like serifs. Fonts with unusual ascent and descent will sit a little off.
326
- - **Accents.** `\vec` and `\hat` use font glyphs and are only approximately centred.
330
+ - **Accents.** `\vec`, `\hat`, `\tilde`, `\dot` and the overline are placed for Latin Modern: lower over a letter with no ascender (x, u, α), and leaning right over an italic letter. Other fonts can leave them a little high or low.
327
331
  - **Unbalanced fractions.** Taller-than-usual numerators on fractions are placed from estimates. Very deep nesting can drift by a few hundredths of an em.