@drghaliasri/butex 8.0.1 → 8.0.3

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.
@@ -5,7 +5,7 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
5
5
  type EditorSide = 'english' | 'arabic';
6
6
  type EditorNodeKind = 'char' | 'number' | 'operator' | 'delimiter' | 'boxed' | 'frac' | 'sqrt' | 'overset' | 'underset' | 'accent' | 'env' | 'atomicCommand' | 'atomicOperatorCommand' | 'standardCommand';
7
7
  type MatrixEnvStyle = 'matrix' | 'pmatrix' | 'bmatrix' | 'Bmatrix' | 'vmatrix' | 'Vmatrix';
8
- type GridEnvName = MatrixEnvStyle | 'array' | 'aligned';
8
+ type GridEnvName = MatrixEnvStyle | 'array' | 'aligned' | 'cases';
9
9
  type EnvColumnAlignment = 'l' | 'c' | 'r';
10
10
  type CharacterFontId = 'default' | 'takween' | 'diwani' | 'diwaniOutline' | 'maghribi';
11
11
  type DigitFormId = 'western' | 'arabicIndic' | 'persianIndic';
@@ -21,10 +21,10 @@ type EditorNode = {
21
21
  kind: EditorNodeKind;
22
22
  expr: string;
23
23
  characterFont?: CharacterFontId;
24
- /** Minimal upstream source identity used only while the node remains unchanged. */
24
+ /** Canonical source identity; atomic Arabic-label edits preserve it, canonical-side edits invalidate it. */
25
25
  sourceLatex?: string;
26
26
  /** Existing editor node reused for wrapper commands that need editable content. */
27
- wrapperCommand?: '\\unit' | '\\mathtt' | '\\mathbf' | '\\boldarabic';
27
+ wrapperCommand?: '\\unit' | '\\mathtt' | '\\operatorname' | '\\text' | '\\mathbf' | '\\boldarabic';
28
28
  superscript: EditorChain | null;
29
29
  subscript: EditorChain | null;
30
30
  leftDelimExpr: string;
@@ -5,7 +5,7 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
5
5
  type EditorSide = 'english' | 'arabic';
6
6
  type EditorNodeKind = 'char' | 'number' | 'operator' | 'delimiter' | 'boxed' | 'frac' | 'sqrt' | 'overset' | 'underset' | 'accent' | 'env' | 'atomicCommand' | 'atomicOperatorCommand' | 'standardCommand';
7
7
  type MatrixEnvStyle = 'matrix' | 'pmatrix' | 'bmatrix' | 'Bmatrix' | 'vmatrix' | 'Vmatrix';
8
- type GridEnvName = MatrixEnvStyle | 'array' | 'aligned';
8
+ type GridEnvName = MatrixEnvStyle | 'array' | 'aligned' | 'cases';
9
9
  type EnvColumnAlignment = 'l' | 'c' | 'r';
10
10
  type CharacterFontId = 'default' | 'takween' | 'diwani' | 'diwaniOutline' | 'maghribi';
11
11
  type DigitFormId = 'western' | 'arabicIndic' | 'persianIndic';
@@ -21,10 +21,10 @@ type EditorNode = {
21
21
  kind: EditorNodeKind;
22
22
  expr: string;
23
23
  characterFont?: CharacterFontId;
24
- /** Minimal upstream source identity used only while the node remains unchanged. */
24
+ /** Canonical source identity; atomic Arabic-label edits preserve it, canonical-side edits invalidate it. */
25
25
  sourceLatex?: string;
26
26
  /** Existing editor node reused for wrapper commands that need editable content. */
27
- wrapperCommand?: '\\unit' | '\\mathtt' | '\\mathbf' | '\\boldarabic';
27
+ wrapperCommand?: '\\unit' | '\\mathtt' | '\\operatorname' | '\\text' | '\\mathbf' | '\\boldarabic';
28
28
  superscript: EditorChain | null;
29
29
  subscript: EditorChain | null;
30
30
  leftDelimExpr: string;
@@ -601,7 +601,9 @@ ${this.closing}` + this.latexScripts();
601
601
  }
602
602
  const formattedLines = this.lines.map((line) => ` ${line.arabicLatex()}`);
603
603
  const content = formattedLines.join(" \\\\\n");
604
- const fullContent = `${this.opening}
604
+ const fullContent = this.opening === "\\begin{cases}" && this.closing === "\\end{cases}" ? `\\left.\\begin{array}{rl}
605
+ ${content}
606
+ \\end{array}\\right\\}` : `${this.opening}
605
607
  ${content}
606
608
  ${this.closing}`;
607
609
  return this.arabicLatexScripts(sup, sub, fullContent);
@@ -3002,7 +3004,7 @@ function createAccentNode(accentId) {
3002
3004
  }
3003
3005
  function createGridEnvNode(envName, rows, columns, columnAlignments) {
3004
3006
  const safeRows = Math.max(1, Math.min(12, Math.trunc(rows)));
3005
- const safeColumns = Math.max(1, Math.min(12, Math.trunc(columns)));
3007
+ const safeColumns = envName === "cases" ? 2 : Math.max(1, Math.min(12, Math.trunc(columns)));
3006
3008
  const matrixStyles = /* @__PURE__ */ new Set(["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"]);
3007
3009
  const isMatrix = matrixStyles.has(envName) && envName !== "array" && envName !== "aligned";
3008
3010
  return {
@@ -3327,6 +3329,9 @@ function renderMatrixRowsArabic(node, options) {
3327
3329
  const spec = (node.columnAlignments ?? []).slice(0, node.matrixRows[0]?.length ?? 0).reverse().map(reverseAlignmentForArabic).join("");
3328
3330
  return `\\begin{array}{${spec || "c"}}${rows.join(" \\\\\n")}\\end{array}`;
3329
3331
  }
3332
+ if (envName === "cases" && options?.casesPresentation) {
3333
+ return `\\left.\\begin{array}{rl}${rows.join(" \\\\\n")}\\end{array}\\right\\}`;
3334
+ }
3330
3335
  return `\\begin{${envName}}${rows.join(" \\\\\n")}\\end{${envName}}`;
3331
3336
  }
3332
3337
  function renderAccentEnglish(command, accent, base, options) {
@@ -3348,6 +3353,10 @@ function renderAccentArabic(command, accent, base, options) {
3348
3353
  function escapeText(s) {
3349
3354
  return s.replace(/\\/g, "\\textbackslash{}").replace(/[{}]/g, "\\$&");
3350
3355
  }
3356
+ function escapeAtomicLiteral(text) {
3357
+ const unescaped = text.replace(/\\([%_&#{}$])/g, "$1");
3358
+ return escapeText(unescaped).replace(/[%_&#$]/g, "\\$&");
3359
+ }
3351
3360
  function renderCharContent(node, options) {
3352
3361
  const expr = formatDigits(node.expr, selectedDigitForm(options));
3353
3362
  const font = node.characterFont ?? "default";
@@ -3394,7 +3403,7 @@ function renderNodeEnglish(node, options) {
3394
3403
  return `${body}${latinScripts(node, options)}`;
3395
3404
  }
3396
3405
  if (node.kind === "atomicOperatorCommand") {
3397
- const body = atomicOperatorCommandSpec(node.expr)?.Tex ?? node.expr;
3406
+ const body = node.sourceLatex === "\\le" || node.sourceLatex === "\\ge" ? node.sourceLatex : atomicOperatorCommandSpec(node.expr)?.Tex ?? node.expr;
3398
3407
  return `${body}${latinScripts(node, options)}`;
3399
3408
  }
3400
3409
  if (node.kind === "standardCommand") {
@@ -3411,7 +3420,8 @@ function renderNodeEnglish(node, options) {
3411
3420
  if (node.wrapperCommand) {
3412
3421
  const expr = formatDigits(node.expr, selectedDigitForm(options));
3413
3422
  const wrapper = node.wrapperCommand === "\\boldarabic" ? "\\mathbf" : node.wrapperCommand;
3414
- return `${wrapper}{${escapeText(expr)}}${latinScripts(node, options)}`;
3423
+ const escaped = wrapper === "\\operatorname" || wrapper === "\\text" ? escapeAtomicLiteral(expr) : escapeText(expr);
3424
+ return `${wrapper}{${escaped}}${latinScripts(node, options)}`;
3415
3425
  }
3416
3426
  return `${renderCharContent(node, options)}${latinScripts(node, options)}`;
3417
3427
  }
@@ -3472,7 +3482,8 @@ function renderNodeArabic(node, options) {
3472
3482
  const content = renderCharContent(node, options);
3473
3483
  if (node.wrapperCommand) {
3474
3484
  const wrapper = node.wrapperCommand === "\\mathbf" ? "\\boldarabic" : node.wrapperCommand;
3475
- return arabicScripts(node, `${wrapper}{${content}}`, options);
3485
+ const literal = wrapper === "\\operatorname" || wrapper === "\\text" ? escapeAtomicLiteral(expr) : content;
3486
+ return arabicScripts(node, `${wrapper}{${literal}}`, options);
3476
3487
  }
3477
3488
  return arabicScripts(node, (node.characterFont ?? "default") === "default" ? `\\text{${escapeText(expr)}}` : content, options);
3478
3489
  }
@@ -3501,6 +3512,9 @@ function renderEnglishLatexFromSession(session) {
3501
3512
  function renderArabicLatexFromSession(session) {
3502
3513
  return renderChainArabic(session.arabicTree, { digitForm: session.currentDigitForm ?? "western" });
3503
3514
  }
3515
+ function renderArabicPreviewLatexFromSession(session) {
3516
+ return renderChainArabic(session.arabicTree, { digitForm: session.currentDigitForm ?? "western", casesPresentation: true });
3517
+ }
3504
3518
 
3505
3519
  // src/editor/caretOrder.ts
3506
3520
  function collectCaretPositions(chain) {
@@ -3826,6 +3840,9 @@ function findNodeById(root, nodeId) {
3826
3840
  function activeTreeKey(side) {
3827
3841
  return side === "english" ? "englishTree" : "arabicTree";
3828
3842
  }
3843
+ function passiveTreeKey(side) {
3844
+ return side === "english" ? "arabicTree" : "englishTree";
3845
+ }
3829
3846
 
3830
3847
  // src/editor/selection.ts
3831
3848
  function nodeChildChains(node) {
@@ -4454,6 +4471,7 @@ var EQUATION_EDITOR_MESSAGES = {
4454
4471
  matrix: "\u0645\u0635\u0641\u0648\u0641\u0629",
4455
4472
  array: "\u062C\u062F\u0648\u0644",
4456
4473
  aligned: "\u0645\u062D\u0627\u0630\u0627\u0629",
4474
+ piecewise: "\u062F\u0627\u0644\u0629 \u0645\u062A\u0639\u062F\u062F\u0629 \u0627\u0644\u062A\u0639\u0631\u064A\u0641",
4457
4475
  matrixType: "\u0646\u0648\u0639 \u0627\u0644\u0645\u0635\u0641\u0648\u0641\u0629",
4458
4476
  chooseEnvironmentSize: "\u0627\u062E\u062A\u064A\u0627\u0631 \u062D\u062C\u0645 \u0627\u0644\u0628\u064A\u0626\u0629",
4459
4477
  rows: "\u0635\u0641\u0648\u0641",
@@ -4581,6 +4599,7 @@ var EQUATION_EDITOR_MESSAGES = {
4581
4599
  matrix: "Matrix",
4582
4600
  array: "Array",
4583
4601
  aligned: "Aligned",
4602
+ piecewise: "Piecewise (cases)",
4584
4603
  matrixType: "Matrix type",
4585
4604
  chooseEnvironmentSize: "Choose environment size",
4586
4605
  rows: "Rows",
@@ -4714,6 +4733,9 @@ function markExprSync(session, nodeId) {
4714
4733
  function clearSourceIdentity(session, nodeId) {
4715
4734
  const english = findNodeById(session.englishTree, nodeId);
4716
4735
  const arabic = findNodeById(session.arabicTree, nodeId);
4736
+ if (session.activeSide === "arabic" && english?.node.kind === "char" && ["\\mathtt", "\\operatorname", "\\text"].includes(english.node.wrapperCommand ?? "") && english.node.sourceLatex) {
4737
+ return;
4738
+ }
4717
4739
  if (english) {
4718
4740
  delete english.node.sourceLatex;
4719
4741
  }
@@ -5714,6 +5736,9 @@ function insertArrayEnv(session, rows, columns, columnAlignments) {
5714
5736
  function insertAlignedEnv(session, rows, columns) {
5715
5737
  return insertGridEnv(session, "aligned", rows, columns);
5716
5738
  }
5739
+ function insertCasesEnv(session, rows) {
5740
+ return insertGridEnv(session, "cases", rows, 2);
5741
+ }
5717
5742
  function insertGridEnv(session, envName, rows, columns, columnAlignments) {
5718
5743
  const base = replaceSelectionFirst(session);
5719
5744
  const next = cloneEditorSession(base);
@@ -5734,7 +5759,7 @@ function insertGridEnv(session, envName, rows, columns, columnAlignments) {
5734
5759
  function setMatrixEnvStyle(session, style) {
5735
5760
  const next = cloneEditorSession(session);
5736
5761
  const pair = findSelectedMatrixPair(next);
5737
- if (!pair) {
5762
+ if (!pair || pair.english.envName === "cases") {
5738
5763
  return session;
5739
5764
  }
5740
5765
  pair.english.envName = style;
@@ -5798,7 +5823,7 @@ function removeMatrixRow(session) {
5798
5823
  function addMatrixColumn(session) {
5799
5824
  const next = cloneEditorSession(session);
5800
5825
  const pair = findSelectedMatrixPair(next);
5801
- if (!pair || !pair.english.matrixRows || !pair.arabic.matrixRows) {
5826
+ if (!pair || !pair.english.matrixRows || !pair.arabic.matrixRows || pair.english.envName === "cases") {
5802
5827
  return session;
5803
5828
  }
5804
5829
  const columnCount = pair.english.matrixRows[0]?.length ?? 0;
@@ -5820,7 +5845,7 @@ function addMatrixColumn(session) {
5820
5845
  function removeMatrixColumn(session) {
5821
5846
  const next = cloneEditorSession(session);
5822
5847
  const pair = findSelectedMatrixPair(next);
5823
- if (!pair || !pair.english.matrixRows || !pair.arabic.matrixRows) {
5848
+ if (!pair || !pair.english.matrixRows || !pair.arabic.matrixRows || pair.english.envName === "cases") {
5824
5849
  return session;
5825
5850
  }
5826
5851
  const columnCount = pair.english.matrixRows[0]?.length ?? 0;
@@ -6037,6 +6062,24 @@ function deleteForward(session) {
6037
6062
  appendDebug(next, "deleteForward", "removed node");
6038
6063
  return next;
6039
6064
  }
6065
+ function setExpr(session, nodeId, expr) {
6066
+ const next = cloneEditorSession(session);
6067
+ const activeKey = activeTreeKey(next.activeSide);
6068
+ const passiveKey = passiveTreeKey(next.activeSide);
6069
+ const activeNode = findNodeById(next[activeKey], nodeId);
6070
+ const passiveNode = findNodeById(next[passiveKey], nodeId);
6071
+ if (!activeNode || !passiveNode) {
6072
+ return session;
6073
+ }
6074
+ activeNode.node.expr = expr;
6075
+ if (["\\mathtt", "\\operatorname", "\\text"].includes(activeNode.node.wrapperCommand ?? "")) {
6076
+ passiveNode.node.expr = expr;
6077
+ }
6078
+ clearSourceIdentity(next, nodeId);
6079
+ markExprSync(next, nodeId);
6080
+ appendDebug(next, "setExpr", nodeId);
6081
+ return next;
6082
+ }
6040
6083
  function createStarterSession() {
6041
6084
  const englishRoot = createEditorChain([createEditorNode("char", "x")]);
6042
6085
  const arabicRoot = createEditorChain([createEditorNode("char", "\u0633")]);
@@ -6120,6 +6163,14 @@ function pasteText(session, text) {
6120
6163
  if (!text) {
6121
6164
  return session;
6122
6165
  }
6166
+ const selection = session.selection;
6167
+ if (selection && Math.abs(selection.anchorIndex - selection.focusIndex) === 1) {
6168
+ const selectedChain = findChainById(session[activeTreeKey(session.activeSide)], selection.chainId);
6169
+ const selected = selectedChain?.chain.nodes[Math.min(selection.anchorIndex, selection.focusIndex)];
6170
+ if (selected?.kind === "char" && ["\\mathtt", "\\operatorname", "\\text"].includes(selected.wrapperCommand ?? "")) {
6171
+ return setExpr(session, selected.id, text);
6172
+ }
6173
+ }
6123
6174
  let s = session;
6124
6175
  let i = 0;
6125
6176
  while (i < text.length) {
@@ -6179,6 +6230,8 @@ function buildBaseNodeBody(node, digitForm = "western", side = "english") {
6179
6230
  if (node.wrapperCommand === "\\boldarabic" || node.wrapperCommand === "\\mathbf") {
6180
6231
  value.classList.add("node-value--bold");
6181
6232
  }
6233
+ if (node.wrapperCommand === "\\text") value.dir = "auto";
6234
+ if (node.wrapperCommand === "\\operatorname") value.classList.add("node-value--operator-name");
6182
6235
  if (node.wrapperCommand === "\\unit") {
6183
6236
  value.classList.add("node-value--unit");
6184
6237
  value.title = "\u0648\u062D\u062F\u0629 (unit)";
@@ -6685,6 +6738,11 @@ var MATRIX_ENV_CSS = `
6685
6738
  transform-origin: center;
6686
6739
  }
6687
6740
 
6741
+ .matrix-env--cases .matrix-env__wrapper:empty {
6742
+ min-width: 0;
6743
+ padding: 0;
6744
+ }
6745
+
6688
6746
  .matrix-env__grid {
6689
6747
  display: inline-grid;
6690
6748
  direction: ltr;
@@ -6729,9 +6787,10 @@ function applyMatrixDelimiterScale(wrap, grid) {
6729
6787
  }
6730
6788
  function buildMatrixEnvNodeBody(node, buildChain, side) {
6731
6789
  const wrap = document.createElement("span");
6732
- wrap.className = `matrix-env matrix-env--${side}`;
6790
+ wrap.className = `matrix-env matrix-env--${side}${node.envName === "cases" ? " matrix-env--cases" : ""}`;
6791
+ if (node.envName === "cases") wrap.dir = "ltr";
6733
6792
  const style = node.envName ?? node.matrixStyle ?? "matrix";
6734
- const glyphs = style === "array" || style === "aligned" ? void 0 : WRAPPERS[style];
6793
+ const glyphs = style === "cases" ? side === "arabic" ? { left: "", right: "}" } : { left: "{", right: "" } : style === "array" || style === "aligned" ? void 0 : WRAPPERS[style];
6735
6794
  const left = document.createElement("span");
6736
6795
  left.className = "matrix-env__wrapper matrix-env__wrapper--left";
6737
6796
  left.textContent = glyphs?.left ?? "";
@@ -8181,6 +8240,10 @@ function createEditorRuntime(options) {
8181
8240
  applyStructural((prev) => insertAlignedEnv(prev, rows, columns));
8182
8241
  focusInput();
8183
8242
  },
8243
+ insertCasesEnv: (rows) => {
8244
+ applyStructural((prev) => insertCasesEnv(prev, rows));
8245
+ focusInput();
8246
+ },
8184
8247
  insertSqrt: () => {
8185
8248
  applyStructural((prev) => insertSqrt(prev));
8186
8249
  focusInput();
@@ -8331,11 +8394,17 @@ var COMMAND_FONT_MAP = Object.fromEntries([
8331
8394
  var CHAR_FONT_EXPORT_TEX = Object.fromEntries(
8332
8395
  NON_DEFAULT_CHARACTER_FONT_SPECS.map((font) => [font.id, font.macro])
8333
8396
  );
8397
+ function hasBareBoldCharSource(node) {
8398
+ return node.kind === "char" && (node.wrapperCommand === "\\boldarabic" || node.wrapperCommand === "\\mathbf") && typeof node.sourceLatex === "string" && node.sourceLatex.length > 0 && !node.sourceLatex.startsWith("\\mathbf{");
8399
+ }
8334
8400
  function charEditorNodeToAstNode(node) {
8335
8401
  const font = node.characterFont ?? "default";
8336
8402
  const charNode = new CharNode(node.expr);
8337
8403
  if (node.wrapperCommand) {
8338
8404
  const wrapper = node.wrapperCommand === "\\boldarabic" ? "\\mathbf" : node.wrapperCommand;
8405
+ if (hasBareBoldCharSource(node)) {
8406
+ charNode.sourceLatex = node.sourceLatex;
8407
+ }
8339
8408
  return new CommandNode(wrapper, [], [new ChainNode([charNode])]);
8340
8409
  }
8341
8410
  if (font === "default") {
@@ -8353,8 +8422,9 @@ function commandIdForTex(tex) {
8353
8422
  return null;
8354
8423
  }
8355
8424
  function operatorCommandIdForTex(tex) {
8425
+ const normalized = tex === "\\le" ? "\\leq" : tex === "\\ge" ? "\\geq" : tex;
8356
8426
  for (const spec of Object.values(ATOMIC_OPERATOR_COMMANDS)) {
8357
- if (spec.Tex === tex) {
8427
+ if (spec.Tex === normalized) {
8358
8428
  return spec.id;
8359
8429
  }
8360
8430
  }
@@ -8385,7 +8455,7 @@ function attachScripts(astNode, editorNode) {
8385
8455
  }
8386
8456
  var MATRIX_ENV_NAMES = /* @__PURE__ */ new Set(["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"]);
8387
8457
  function isGridEnvName(value) {
8388
- return MATRIX_ENV_NAMES.has(value) || value === "array" || value === "aligned";
8458
+ return MATRIX_ENV_NAMES.has(value) || value === "array" || value === "aligned" || value === "cases";
8389
8459
  }
8390
8460
  function isMatrixEnvName(value) {
8391
8461
  return MATRIX_ENV_NAMES.has(value);
@@ -8432,6 +8502,9 @@ function envToEditorNode(node) {
8432
8502
  return { editable: false, reason: `\u0625\u063A\u0644\u0627\u0642 \u0628\u064A\u0626\u0629 \u0631\u064A\u0627\u0636\u064A\u0629 \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645 \u062D\u0627\u0644\u064A\u0627: ${node.closing}` };
8433
8503
  }
8434
8504
  const rowCells = node.lines.map(splitEnvLine);
8505
+ if (parsed.envName === "cases" && (rowCells.length === 0 || rowCells.length > 12 || rowCells.some((row) => row.length !== 2))) {
8506
+ return { editable: false, reason: "\u0628\u064A\u0626\u0629 cases \u062A\u062A\u0637\u0644\u0628 \u062A\u0639\u0628\u064A\u0631\u0627 \u0648\u0634\u0631\u0637\u0627 \u0641\u064A \u0643\u0644 \u0635\u0641 (\u0639\u0645\u0648\u062F\u0627\u0646)" };
8507
+ }
8435
8508
  const rows = Math.max(1, rowCells.length);
8436
8509
  const columns = Math.max(1, ...rowCells.map((row) => row.length));
8437
8510
  const env = createGridEnvNode(parsed.envName, rows, columns, parsed.alignments);
@@ -8514,14 +8587,19 @@ function wrappedAtomicCommandId(node) {
8514
8587
  return commandIdForTex(`${node.name}{${child.expr}}`);
8515
8588
  }
8516
8589
  function editableWrapperCommand(node) {
8517
- if (node.name !== "\\mathtt" && node.name !== "\\mathsf" && node.name !== "\\unit" && node.name !== "\\mathbf" && node.name !== "\\boldarabic") {
8590
+ if (node.name !== "\\mathtt" && node.name !== "\\operatorname" && node.name !== "\\text" && node.name !== "\\mathsf" && node.name !== "\\unit" && node.name !== "\\mathbf" && node.name !== "\\boldarabic") {
8591
+ return null;
8592
+ }
8593
+ if (node.name === "\\text" && node.sourceLatex && !node.sourceLatex.startsWith("\\text{")) {
8518
8594
  return null;
8519
8595
  }
8520
8596
  const child = singleCommandChar(node);
8597
+ if (node.name === "\\text" && child?.sourceLatex && !node.sourceLatex) return null;
8521
8598
  let editorNode;
8522
8599
  if (child) {
8523
8600
  const normalized = normalizeCharImportExpr(child.expr);
8524
8601
  editorNode = createEditorNode("char", normalized.expr);
8602
+ if (child.sourceLatex) editorNode.sourceLatex = child.sourceLatex;
8525
8603
  if (normalized.characterFont !== "default") {
8526
8604
  editorNode.characterFont = normalized.characterFont;
8527
8605
  }
@@ -8533,7 +8611,7 @@ function editableWrapperCommand(node) {
8533
8611
  if (!nested?.editable) return null;
8534
8612
  editorNode = nested.node;
8535
8613
  }
8536
- editorNode.wrapperCommand = node.name === "\\mathtt" ? "\\mathtt" : node.name === "\\mathbf" ? "\\mathbf" : node.name === "\\boldarabic" ? "\\boldarabic" : "\\unit";
8614
+ editorNode.wrapperCommand = node.name === "\\mathsf" ? "\\unit" : node.name;
8537
8615
  const scripts = attachScripts(node, editorNode);
8538
8616
  if (scripts) {
8539
8617
  return scripts;
@@ -8667,6 +8745,7 @@ function commandToEditorNode(node) {
8667
8745
  const operatorId = operatorCommandIdForTex(node.name);
8668
8746
  if (operatorId && node.optionalArgs.length === 0 && node.mandatoryArgs.length === 0) {
8669
8747
  const atomicOperator = createEditorNode("atomicOperatorCommand", operatorId);
8748
+ if (node.name === "\\le" || node.name === "\\ge") atomicOperator.sourceLatex = node.name;
8670
8749
  const scripts = attachScripts(node, atomicOperator);
8671
8750
  if (scripts) {
8672
8751
  return scripts;
@@ -8978,7 +9057,7 @@ function editorNodeToAstNode(node) {
8978
9057
  if (!tex) {
8979
9058
  return { ok: false, reason: `\u0631\u0645\u0632 \u0631\u064A\u0627\u0636\u064A \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645 \u062D\u0627\u0644\u064A\u0627: ${node.expr}` };
8980
9059
  }
8981
- astNode = new CommandNode(tex);
9060
+ astNode = new CommandNode(node.sourceLatex === "\\le" || node.sourceLatex === "\\ge" ? node.sourceLatex : tex);
8982
9061
  } else if (node.kind === "standardCommand") {
8983
9062
  if (!isStandardCommandTex(node.expr)) {
8984
9063
  return { ok: false, reason: `\u0623\u0645\u0631 \u0631\u064A\u0627\u0636\u064A \u0642\u064A\u0627\u0633\u064A \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645 \u062D\u0627\u0644\u064A\u0627: ${node.expr}` };
@@ -8989,7 +9068,7 @@ function editorNodeToAstNode(node) {
8989
9068
  } else {
8990
9069
  return { ok: false, reason: `\u0639\u0642\u062F\u0629 \u0631\u064A\u0627\u0636\u064A\u0629 \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645\u0629 \u062D\u0627\u0644\u064A\u0627: ${node.kind}` };
8991
9070
  }
8992
- if (node.sourceLatex) {
9071
+ if (node.sourceLatex && !hasBareBoldCharSource(node)) {
8993
9072
  astNode.sourceLatex = node.sourceLatex;
8994
9073
  }
8995
9074
  const scripts = attachAstScripts(node, astNode);
@@ -9047,8 +9126,8 @@ function mathTokenToEditorSession(math, side = "arabic") {
9047
9126
  result.session.sync = buildInitialSyncMap(result.session.englishTree, result.session.arabicTree);
9048
9127
  return result;
9049
9128
  }
9050
- function mathSourceFromSession(session, side, opening = "$", closing = "$") {
9051
- const inner = side === "english" ? renderEnglishLatexFromSession(session) : renderArabicLatexFromSession(session);
9129
+ function mathSourceFromSession(session, side, opening = "$", closing = "$", presentation = false) {
9130
+ const inner = side === "english" ? renderEnglishLatexFromSession(session) : presentation ? renderArabicPreviewLatexFromSession(session) : renderArabicLatexFromSession(session);
9052
9131
  if (opening === "$" || opening === "\\(") {
9053
9132
  return opening + inner + closing;
9054
9133
  }
@@ -9068,11 +9147,11 @@ function mathNodeFromSession(session, opening = "$", closing = "$") {
9068
9147
  }
9069
9148
  return { nodeType: "MathObject", mathMode: opening, lines: [], closing };
9070
9149
  }
9071
- function mathTokenSourceForSide(token, side, digitForm) {
9150
+ function mathTokenSourceForSide(token, side, digitForm, presentation = false) {
9072
9151
  if (!token.math || token.sourceOwner === "raw") {
9073
9152
  return token.source;
9074
9153
  }
9075
- if (token.sourceSide === side && digitForm === void 0) {
9154
+ if (!presentation && token.sourceSide === side && digitForm === void 0) {
9076
9155
  return token.source;
9077
9156
  }
9078
9157
  const result = mathTokenToEditorSession(token.math, side);
@@ -9080,7 +9159,7 @@ function mathTokenSourceForSide(token, side, digitForm) {
9080
9159
  return token.source;
9081
9160
  }
9082
9161
  const session = digitForm === void 0 ? result.session : setCurrentDigitForm(result.session, digitForm);
9083
- return mathSourceFromSession(session, side, token.opening, token.closing);
9162
+ return mathSourceFromSession(session, side, token.opening, token.closing, presentation);
9084
9163
  }
9085
9164
 
9086
9165
  // src/document/commands.ts
@@ -10332,9 +10411,13 @@ function digitsMappingFromDigitForm(digitForm) {
10332
10411
  }
10333
10412
  return "arabicdigits";
10334
10413
  }
10414
+ function editorMathLatexForCompile(token) {
10415
+ const source = token.math && token.sourceSide === "arabic" && token.source.includes("\\begin{cases}") ? mathTokenSourceForSide(token, "arabic", void 0, true) : token.source;
10416
+ return mirrorBuTeXOperatorsInTex(source, { target: "compile" });
10417
+ }
10335
10418
  function mathBodyLatex(token) {
10336
10419
  if (token.sourceOwner === "editor") {
10337
- return stripDisplayMathDelimiters(mirrorBuTeXOperatorsInTex(token.source, { target: "compile" }));
10420
+ return stripDisplayMathDelimiters(editorMathLatexForCompile(token));
10338
10421
  }
10339
10422
  if (!token.math || token.sourceOwner === "raw") {
10340
10423
  return stripDisplayMathDelimiters(token.source);
@@ -10409,7 +10492,7 @@ ${body}
10409
10492
  \\end{equation}`;
10410
10493
  }
10411
10494
  if (token.sourceOwner === "editor") {
10412
- return mirrorBuTeXOperatorsInTex(token.source, { target: "compile" });
10495
+ return editorMathLatexForCompile(token);
10413
10496
  }
10414
10497
  if (!token.math || token.sourceOwner === "raw") {
10415
10498
  return token.source;
@@ -10985,7 +11068,7 @@ function remapSelectionAfterRangeMove(selection, direction) {
10985
11068
 
10986
11069
  // src/document/previewModel.ts
10987
11070
  function mathTex(token, equationSide, digitForm) {
10988
- return mathTokenSourceForSide(token, equationSide, digitForm);
11071
+ return mathTokenSourceForSide(token, equationSide, digitForm, true);
10989
11072
  }
10990
11073
  function previewInlines(field, islands, output, equationSide, document2, previewOptions) {
10991
11074
  return field.tokens.map((token) => {
@@ -12329,7 +12412,7 @@ function MathChip({
12329
12412
  onTextInputFromFocus
12330
12413
  }) {
12331
12414
  const messages = documentMessages(uiLocale);
12332
- const tex = mathTokenSourceForSide(token, equationSide, digitForm);
12415
+ const tex = mathTokenSourceForSide(token, equationSide, digitForm, true);
12333
12416
  const forwardArrowKey = documentDirection === "rtl" ? "ArrowLeft" : "ArrowRight";
12334
12417
  const backwardArrowKey = documentDirection === "rtl" ? "ArrowRight" : "ArrowLeft";
12335
12418
  const island = /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(MathIsland, { id: token.id, tex, display: token.display, output });
@@ -16109,7 +16192,7 @@ function EquationMobileToolbar({
16109
16192
  if (kind === "rows") {
16110
16193
  setMatrixRows(next);
16111
16194
  } else {
16112
- setMatrixColumns(envMode === "aligned" ? 2 : next);
16195
+ setMatrixColumns(envMode === "aligned" || envMode === "cases" ? 2 : next);
16113
16196
  }
16114
16197
  }
16115
16198
  function insertEnvironment() {
@@ -16118,6 +16201,8 @@ function EquationMobileToolbar({
16118
16201
  runtime.insertArrayEnv(matrixRows, matrixColumns);
16119
16202
  } else if (envMode === "aligned") {
16120
16203
  runtime.insertAlignedEnv(matrixRows, 2);
16204
+ } else if (envMode === "cases") {
16205
+ runtime.insertCasesEnv(matrixRows);
16121
16206
  } else {
16122
16207
  runtime.insertMatrixEnv(matrixStyle, matrixRows, matrixColumns);
16123
16208
  }
@@ -16285,7 +16370,8 @@ function EquationMobileToolbar({
16285
16370
  /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("div", { className: "butex-widget__sheet-choice-row", role: "group", "aria-label": messages.environmentType, children: [
16286
16371
  { id: "matrix", label: messages.matrix },
16287
16372
  { id: "array", label: messages.array },
16288
- { id: "aligned", label: messages.aligned }
16373
+ { id: "aligned", label: messages.aligned },
16374
+ { id: "cases", label: messages.piecewise }
16289
16375
  ].map((option) => /* @__PURE__ */ (0, import_jsx_runtime13.jsx)(
16290
16376
  "button",
16291
16377
  {
@@ -16293,7 +16379,7 @@ function EquationMobileToolbar({
16293
16379
  "aria-pressed": envMode === option.id,
16294
16380
  onClick: () => {
16295
16381
  setEnvMode(option.id);
16296
- if (option.id === "aligned") setMatrixColumns(2);
16382
+ if (option.id === "aligned" || option.id === "cases") setMatrixColumns(2);
16297
16383
  },
16298
16384
  children: option.label
16299
16385
  },
@@ -16302,19 +16388,19 @@ function EquationMobileToolbar({
16302
16388
  envMode === "matrix" ? /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("div", { className: "butex-widget__sheet-choice-row", role: "group", "aria-label": messages.matrixType, children: MATRIX_STYLES.map((style) => /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("button", { type: "button", title: matrixStyleTitle(style.id, messages), "aria-label": matrixStyleTitle(style.id, messages), "aria-pressed": matrixStyle === style.id, onClick: () => setMatrixStyle(style.id), children: style.label }, style.id)) }) : null,
16303
16389
  /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)("section", { className: "butex-widget__sheet-section", children: [
16304
16390
  /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("h4", { children: messages.commonMatrixSizes }),
16305
- /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("div", { className: "butex-widget__matrix-presets", children: (envMode === "aligned" ? MATRIX_PRESETS.filter((preset) => preset.columns === 2) : MATRIX_PRESETS).map((preset) => /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)(
16391
+ /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("div", { className: "butex-widget__matrix-presets", children: (envMode === "aligned" || envMode === "cases" ? MATRIX_PRESETS.filter((preset) => preset.columns === 2) : MATRIX_PRESETS).map((preset) => /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)(
16306
16392
  "button",
16307
16393
  {
16308
16394
  type: "button",
16309
16395
  "aria-pressed": matrixRows === preset.rows && matrixColumns === preset.columns,
16310
16396
  onClick: () => {
16311
16397
  setMatrixRows(preset.rows);
16312
- setMatrixColumns(envMode === "aligned" ? 2 : preset.columns);
16398
+ setMatrixColumns(envMode === "aligned" || envMode === "cases" ? 2 : preset.columns);
16313
16399
  },
16314
16400
  children: [
16315
16401
  preset.rows,
16316
16402
  "\xD7",
16317
- envMode === "aligned" ? 2 : preset.columns
16403
+ envMode === "aligned" || envMode === "cases" ? 2 : preset.columns
16318
16404
  ]
16319
16405
  },
16320
16406
  `${String(preset.rows)}-${String(preset.columns)}`
@@ -16331,12 +16417,12 @@ function EquationMobileToolbar({
16331
16417
  /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)("div", { className: "butex-widget__matrix-stepper-row", children: [
16332
16418
  /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("span", { children: messages.columns }),
16333
16419
  /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)("div", { className: "butex-widget__matrix-stepper", children: [
16334
- /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("button", { type: "button", disabled: matrixColumns <= 1 || envMode === "aligned", "aria-label": messages.decreaseColumns, onClick: () => setMatrixDimension("columns", matrixColumns - 1), children: "\u2212" }),
16335
- /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("output", { "aria-live": "polite", children: envMode === "aligned" ? 2 : matrixColumns }),
16336
- /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("button", { type: "button", disabled: matrixColumns >= 12 || envMode === "aligned", "aria-label": messages.increaseColumns, onClick: () => setMatrixDimension("columns", matrixColumns + 1), children: "+" })
16420
+ /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("button", { type: "button", disabled: matrixColumns <= 1 || envMode === "aligned" || envMode === "cases", "aria-label": messages.decreaseColumns, onClick: () => setMatrixDimension("columns", matrixColumns - 1), children: "\u2212" }),
16421
+ /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("output", { "aria-live": "polite", children: envMode === "aligned" || envMode === "cases" ? 2 : matrixColumns }),
16422
+ /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("button", { type: "button", disabled: matrixColumns >= 12 || envMode === "aligned" || envMode === "cases", "aria-label": messages.increaseColumns, onClick: () => setMatrixDimension("columns", matrixColumns + 1), children: "+" })
16337
16423
  ] })
16338
16424
  ] }),
16339
- /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("p", { className: "butex-widget__matrix-size-summary", "aria-live": "polite", children: matrixSizeLabel(matrixRows, envMode === "aligned" ? 2 : matrixColumns, messages) }),
16425
+ /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("p", { className: "butex-widget__matrix-size-summary", "aria-live": "polite", children: matrixSizeLabel(matrixRows, envMode === "aligned" || envMode === "cases" ? 2 : matrixColumns, messages) }),
16340
16426
  /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("button", { type: "button", className: "butex-widget__matrix-insert", onClick: insertEnvironment, children: messages.insert }),
16341
16427
  /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)("section", { className: "butex-widget__sheet-section", children: [
16342
16428
  /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("h4", { children: messages.editSelectedEnvironment }),
@@ -16357,11 +16443,11 @@ function EquationMobileToolbar({
16357
16443
  "\u2212",
16358
16444
  messages.row
16359
16445
  ] }),
16360
- /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)("button", { type: "button", onClick: () => getRuntime()?.addMatrixColumn(), children: [
16446
+ /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)("button", { type: "button", disabled: envMode === "cases", onClick: () => getRuntime()?.addMatrixColumn(), children: [
16361
16447
  "+",
16362
16448
  messages.column
16363
16449
  ] }),
16364
- /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)("button", { type: "button", onClick: () => getRuntime()?.removeMatrixColumn(), children: [
16450
+ /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)("button", { type: "button", disabled: envMode === "cases", onClick: () => getRuntime()?.removeMatrixColumn(), children: [
16365
16451
  "\u2212",
16366
16452
  messages.column
16367
16453
  ] })
@@ -16892,7 +16978,7 @@ var ButexEditor = (0, import_react12.forwardRef)(
16892
16978
  const debugLogEl = debugLogRef.current;
16893
16979
  const english = renderEnglishLatexFromSession(session);
16894
16980
  const arabic = renderArabicLatexFromSession(session);
16895
- const activeLatex = session.activeSide === "arabic" ? arabic : english;
16981
+ const activeLatex = session.activeSide === "arabic" ? renderArabicPreviewLatexFromSession(session) : english;
16896
16982
  if (debugRef.current && latexLinesEl) {
16897
16983
  const currentMessages = equationEditorMessages(uiLocaleRef.current);
16898
16984
  latexLinesEl.textContent = `${currentMessages.englishLatex}:
@@ -17092,6 +17178,8 @@ ${arabic || currentMessages.empty}`;
17092
17178
  runtimeRef.current?.insertArrayEnv(rows, columns);
17093
17179
  } else if (envPaletteMode === "aligned") {
17094
17180
  runtimeRef.current?.insertAlignedEnv(rows, columns);
17181
+ } else if (envPaletteMode === "cases") {
17182
+ runtimeRef.current?.insertCasesEnv(rows);
17095
17183
  } else {
17096
17184
  runtimeRef.current?.insertMatrixEnv(matrixStyle, rows, columns);
17097
17185
  }
@@ -17387,7 +17475,8 @@ ${arabic || currentMessages.empty}`;
17387
17475
  /* @__PURE__ */ (0, import_jsx_runtime14.jsx)("div", { className: "matrix-style-options", role: "listbox", "aria-label": messages.environmentType, children: [
17388
17476
  { id: "matrix", label: messages.matrix },
17389
17477
  { id: "array", label: messages.array },
17390
- { id: "aligned", label: messages.aligned }
17478
+ { id: "aligned", label: messages.aligned },
17479
+ { id: "cases", label: messages.piecewise }
17391
17480
  ].map((option) => /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(
17392
17481
  "button",
17393
17482
  {
@@ -17398,7 +17487,7 @@ ${arabic || currentMessages.empty}`;
17398
17487
  onMouseDown: (event) => event.preventDefault(),
17399
17488
  onClick: () => {
17400
17489
  setEnvPaletteMode(option.id);
17401
- if (option.id === "aligned") {
17490
+ if (option.id === "aligned" || option.id === "cases") {
17402
17491
  setMatrixColumns(2);
17403
17492
  }
17404
17493
  },
@@ -17423,7 +17512,7 @@ ${arabic || currentMessages.empty}`;
17423
17512
  style.id
17424
17513
  )) }) : null,
17425
17514
  /* @__PURE__ */ (0, import_jsx_runtime14.jsx)("div", { className: "matrix-size-readout", children: matrixHover ? matrixSizeLabel2(matrixHover.rows, matrixHover.columns, messages) : matrixSizeLabel2(matrixRows, matrixColumns, messages) }),
17426
- /* @__PURE__ */ (0, import_jsx_runtime14.jsx)("div", { className: "matrix-grid-picker", "aria-label": messages.chooseEnvironmentSize, children: Array.from(
17515
+ envPaletteMode !== "cases" ? /* @__PURE__ */ (0, import_jsx_runtime14.jsx)("div", { className: "matrix-grid-picker", "aria-label": messages.chooseEnvironmentSize, children: Array.from(
17427
17516
  { length: 8 },
17428
17517
  (_, rowIndex) => Array.from({ length: 8 }, (_2, columnIndex) => {
17429
17518
  const rows = rowIndex + 1;
@@ -17451,7 +17540,7 @@ ${arabic || currentMessages.empty}`;
17451
17540
  `${rows}-${columns}`
17452
17541
  );
17453
17542
  })
17454
- ) }),
17543
+ ) }) : null,
17455
17544
  /* @__PURE__ */ (0, import_jsx_runtime14.jsxs)("div", { className: "matrix-custom-size", children: [
17456
17545
  /* @__PURE__ */ (0, import_jsx_runtime14.jsxs)("label", { children: [
17457
17546
  messages.row,
@@ -17474,7 +17563,8 @@ ${arabic || currentMessages.empty}`;
17474
17563
  type: "number",
17475
17564
  min: 1,
17476
17565
  max: 12,
17477
- value: matrixColumns,
17566
+ value: envPaletteMode === "cases" || envPaletteMode === "aligned" ? 2 : matrixColumns,
17567
+ disabled: envPaletteMode === "cases" || envPaletteMode === "aligned",
17478
17568
  onChange: (event) => setMatrixColumns(clampMatrixSize2(Number(event.currentTarget.value) || 1))
17479
17569
  }
17480
17570
  )
@@ -17487,7 +17577,7 @@ ${arabic || currentMessages.empty}`;
17487
17577
  onMouseDown: (event) => event.preventDefault(),
17488
17578
  onClick: () => {
17489
17579
  const rows = clampMatrixSize2(matrixRows);
17490
- const columns = clampMatrixSize2(matrixColumns);
17580
+ const columns = envPaletteMode === "cases" || envPaletteMode === "aligned" ? 2 : clampMatrixSize2(matrixColumns);
17491
17581
  insertSelectedEnvironment(rows, columns);
17492
17582
  setMatrixMenuOpen(false);
17493
17583
  setMatrixHover(null);
@@ -17533,11 +17623,11 @@ ${arabic || currentMessages.empty}`;
17533
17623
  "-",
17534
17624
  messages.row
17535
17625
  ] }),
17536
- /* @__PURE__ */ (0, import_jsx_runtime14.jsxs)("button", { type: "button", title: messages.addColumn, onMouseDown: (event) => event.preventDefault(), onClick: () => runtimeRef.current?.addMatrixColumn(), children: [
17626
+ /* @__PURE__ */ (0, import_jsx_runtime14.jsxs)("button", { type: "button", title: messages.addColumn, disabled: envPaletteMode === "cases", onMouseDown: (event) => event.preventDefault(), onClick: () => runtimeRef.current?.addMatrixColumn(), children: [
17537
17627
  "+",
17538
17628
  messages.column
17539
17629
  ] }),
17540
- /* @__PURE__ */ (0, import_jsx_runtime14.jsxs)("button", { type: "button", title: messages.removeColumn, onMouseDown: (event) => event.preventDefault(), onClick: () => runtimeRef.current?.removeMatrixColumn(), children: [
17630
+ /* @__PURE__ */ (0, import_jsx_runtime14.jsxs)("button", { type: "button", title: messages.removeColumn, disabled: envPaletteMode === "cases", onMouseDown: (event) => event.preventDefault(), onClick: () => runtimeRef.current?.removeMatrixColumn(), children: [
17541
17631
  "-",
17542
17632
  messages.column
17543
17633
  ] })