@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.
@@ -564,7 +564,9 @@ ${this.closing}` + this.latexScripts();
564
564
  }
565
565
  const formattedLines = this.lines.map((line) => ` ${line.arabicLatex()}`);
566
566
  const content = formattedLines.join(" \\\\\n");
567
- const fullContent = `${this.opening}
567
+ const fullContent = this.opening === "\\begin{cases}" && this.closing === "\\end{cases}" ? `\\left.\\begin{array}{rl}
568
+ ${content}
569
+ \\end{array}\\right\\}` : `${this.opening}
568
570
  ${content}
569
571
  ${this.closing}`;
570
572
  return this.arabicLatexScripts(sup, sub, fullContent);
@@ -2965,7 +2967,7 @@ function createAccentNode(accentId) {
2965
2967
  }
2966
2968
  function createGridEnvNode(envName, rows, columns, columnAlignments) {
2967
2969
  const safeRows = Math.max(1, Math.min(12, Math.trunc(rows)));
2968
- const safeColumns = Math.max(1, Math.min(12, Math.trunc(columns)));
2970
+ const safeColumns = envName === "cases" ? 2 : Math.max(1, Math.min(12, Math.trunc(columns)));
2969
2971
  const matrixStyles = /* @__PURE__ */ new Set(["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"]);
2970
2972
  const isMatrix = matrixStyles.has(envName) && envName !== "array" && envName !== "aligned";
2971
2973
  return {
@@ -3290,6 +3292,9 @@ function renderMatrixRowsArabic(node, options) {
3290
3292
  const spec = (node.columnAlignments ?? []).slice(0, node.matrixRows[0]?.length ?? 0).reverse().map(reverseAlignmentForArabic).join("");
3291
3293
  return `\\begin{array}{${spec || "c"}}${rows.join(" \\\\\n")}\\end{array}`;
3292
3294
  }
3295
+ if (envName === "cases" && options?.casesPresentation) {
3296
+ return `\\left.\\begin{array}{rl}${rows.join(" \\\\\n")}\\end{array}\\right\\}`;
3297
+ }
3293
3298
  return `\\begin{${envName}}${rows.join(" \\\\\n")}\\end{${envName}}`;
3294
3299
  }
3295
3300
  function renderAccentEnglish(command, accent, base, options) {
@@ -3311,6 +3316,10 @@ function renderAccentArabic(command, accent, base, options) {
3311
3316
  function escapeText(s) {
3312
3317
  return s.replace(/\\/g, "\\textbackslash{}").replace(/[{}]/g, "\\$&");
3313
3318
  }
3319
+ function escapeAtomicLiteral(text) {
3320
+ const unescaped = text.replace(/\\([%_&#{}$])/g, "$1");
3321
+ return escapeText(unescaped).replace(/[%_&#$]/g, "\\$&");
3322
+ }
3314
3323
  function renderCharContent(node, options) {
3315
3324
  const expr = formatDigits(node.expr, selectedDigitForm(options));
3316
3325
  const font = node.characterFont ?? "default";
@@ -3357,7 +3366,7 @@ function renderNodeEnglish(node, options) {
3357
3366
  return `${body}${latinScripts(node, options)}`;
3358
3367
  }
3359
3368
  if (node.kind === "atomicOperatorCommand") {
3360
- const body = atomicOperatorCommandSpec(node.expr)?.Tex ?? node.expr;
3369
+ const body = node.sourceLatex === "\\le" || node.sourceLatex === "\\ge" ? node.sourceLatex : atomicOperatorCommandSpec(node.expr)?.Tex ?? node.expr;
3361
3370
  return `${body}${latinScripts(node, options)}`;
3362
3371
  }
3363
3372
  if (node.kind === "standardCommand") {
@@ -3374,7 +3383,8 @@ function renderNodeEnglish(node, options) {
3374
3383
  if (node.wrapperCommand) {
3375
3384
  const expr = formatDigits(node.expr, selectedDigitForm(options));
3376
3385
  const wrapper = node.wrapperCommand === "\\boldarabic" ? "\\mathbf" : node.wrapperCommand;
3377
- return `${wrapper}{${escapeText(expr)}}${latinScripts(node, options)}`;
3386
+ const escaped = wrapper === "\\operatorname" || wrapper === "\\text" ? escapeAtomicLiteral(expr) : escapeText(expr);
3387
+ return `${wrapper}{${escaped}}${latinScripts(node, options)}`;
3378
3388
  }
3379
3389
  return `${renderCharContent(node, options)}${latinScripts(node, options)}`;
3380
3390
  }
@@ -3435,7 +3445,8 @@ function renderNodeArabic(node, options) {
3435
3445
  const content = renderCharContent(node, options);
3436
3446
  if (node.wrapperCommand) {
3437
3447
  const wrapper = node.wrapperCommand === "\\mathbf" ? "\\boldarabic" : node.wrapperCommand;
3438
- return arabicScripts(node, `${wrapper}{${content}}`, options);
3448
+ const literal = wrapper === "\\operatorname" || wrapper === "\\text" ? escapeAtomicLiteral(expr) : content;
3449
+ return arabicScripts(node, `${wrapper}{${literal}}`, options);
3439
3450
  }
3440
3451
  return arabicScripts(node, (node.characterFont ?? "default") === "default" ? `\\text{${escapeText(expr)}}` : content, options);
3441
3452
  }
@@ -3464,6 +3475,9 @@ function renderEnglishLatexFromSession(session) {
3464
3475
  function renderArabicLatexFromSession(session) {
3465
3476
  return renderChainArabic(session.arabicTree, { digitForm: session.currentDigitForm ?? "western" });
3466
3477
  }
3478
+ function renderArabicPreviewLatexFromSession(session) {
3479
+ return renderChainArabic(session.arabicTree, { digitForm: session.currentDigitForm ?? "western", casesPresentation: true });
3480
+ }
3467
3481
 
3468
3482
  // src/editor/caretOrder.ts
3469
3483
  function collectCaretPositions(chain) {
@@ -3789,6 +3803,9 @@ function findNodeById(root, nodeId) {
3789
3803
  function activeTreeKey(side) {
3790
3804
  return side === "english" ? "englishTree" : "arabicTree";
3791
3805
  }
3806
+ function passiveTreeKey(side) {
3807
+ return side === "english" ? "arabicTree" : "englishTree";
3808
+ }
3792
3809
 
3793
3810
  // src/editor/selection.ts
3794
3811
  function nodeChildChains(node) {
@@ -4417,6 +4434,7 @@ var EQUATION_EDITOR_MESSAGES = {
4417
4434
  matrix: "\u0645\u0635\u0641\u0648\u0641\u0629",
4418
4435
  array: "\u062C\u062F\u0648\u0644",
4419
4436
  aligned: "\u0645\u062D\u0627\u0630\u0627\u0629",
4437
+ piecewise: "\u062F\u0627\u0644\u0629 \u0645\u062A\u0639\u062F\u062F\u0629 \u0627\u0644\u062A\u0639\u0631\u064A\u0641",
4420
4438
  matrixType: "\u0646\u0648\u0639 \u0627\u0644\u0645\u0635\u0641\u0648\u0641\u0629",
4421
4439
  chooseEnvironmentSize: "\u0627\u062E\u062A\u064A\u0627\u0631 \u062D\u062C\u0645 \u0627\u0644\u0628\u064A\u0626\u0629",
4422
4440
  rows: "\u0635\u0641\u0648\u0641",
@@ -4544,6 +4562,7 @@ var EQUATION_EDITOR_MESSAGES = {
4544
4562
  matrix: "Matrix",
4545
4563
  array: "Array",
4546
4564
  aligned: "Aligned",
4565
+ piecewise: "Piecewise (cases)",
4547
4566
  matrixType: "Matrix type",
4548
4567
  chooseEnvironmentSize: "Choose environment size",
4549
4568
  rows: "Rows",
@@ -4677,6 +4696,9 @@ function markExprSync(session, nodeId) {
4677
4696
  function clearSourceIdentity(session, nodeId) {
4678
4697
  const english = findNodeById(session.englishTree, nodeId);
4679
4698
  const arabic = findNodeById(session.arabicTree, nodeId);
4699
+ if (session.activeSide === "arabic" && english?.node.kind === "char" && ["\\mathtt", "\\operatorname", "\\text"].includes(english.node.wrapperCommand ?? "") && english.node.sourceLatex) {
4700
+ return;
4701
+ }
4680
4702
  if (english) {
4681
4703
  delete english.node.sourceLatex;
4682
4704
  }
@@ -5677,6 +5699,9 @@ function insertArrayEnv(session, rows, columns, columnAlignments) {
5677
5699
  function insertAlignedEnv(session, rows, columns) {
5678
5700
  return insertGridEnv(session, "aligned", rows, columns);
5679
5701
  }
5702
+ function insertCasesEnv(session, rows) {
5703
+ return insertGridEnv(session, "cases", rows, 2);
5704
+ }
5680
5705
  function insertGridEnv(session, envName, rows, columns, columnAlignments) {
5681
5706
  const base = replaceSelectionFirst(session);
5682
5707
  const next = cloneEditorSession(base);
@@ -5697,7 +5722,7 @@ function insertGridEnv(session, envName, rows, columns, columnAlignments) {
5697
5722
  function setMatrixEnvStyle(session, style) {
5698
5723
  const next = cloneEditorSession(session);
5699
5724
  const pair = findSelectedMatrixPair(next);
5700
- if (!pair) {
5725
+ if (!pair || pair.english.envName === "cases") {
5701
5726
  return session;
5702
5727
  }
5703
5728
  pair.english.envName = style;
@@ -5761,7 +5786,7 @@ function removeMatrixRow(session) {
5761
5786
  function addMatrixColumn(session) {
5762
5787
  const next = cloneEditorSession(session);
5763
5788
  const pair = findSelectedMatrixPair(next);
5764
- if (!pair || !pair.english.matrixRows || !pair.arabic.matrixRows) {
5789
+ if (!pair || !pair.english.matrixRows || !pair.arabic.matrixRows || pair.english.envName === "cases") {
5765
5790
  return session;
5766
5791
  }
5767
5792
  const columnCount = pair.english.matrixRows[0]?.length ?? 0;
@@ -5783,7 +5808,7 @@ function addMatrixColumn(session) {
5783
5808
  function removeMatrixColumn(session) {
5784
5809
  const next = cloneEditorSession(session);
5785
5810
  const pair = findSelectedMatrixPair(next);
5786
- if (!pair || !pair.english.matrixRows || !pair.arabic.matrixRows) {
5811
+ if (!pair || !pair.english.matrixRows || !pair.arabic.matrixRows || pair.english.envName === "cases") {
5787
5812
  return session;
5788
5813
  }
5789
5814
  const columnCount = pair.english.matrixRows[0]?.length ?? 0;
@@ -6000,6 +6025,24 @@ function deleteForward(session) {
6000
6025
  appendDebug(next, "deleteForward", "removed node");
6001
6026
  return next;
6002
6027
  }
6028
+ function setExpr(session, nodeId, expr) {
6029
+ const next = cloneEditorSession(session);
6030
+ const activeKey = activeTreeKey(next.activeSide);
6031
+ const passiveKey = passiveTreeKey(next.activeSide);
6032
+ const activeNode = findNodeById(next[activeKey], nodeId);
6033
+ const passiveNode = findNodeById(next[passiveKey], nodeId);
6034
+ if (!activeNode || !passiveNode) {
6035
+ return session;
6036
+ }
6037
+ activeNode.node.expr = expr;
6038
+ if (["\\mathtt", "\\operatorname", "\\text"].includes(activeNode.node.wrapperCommand ?? "")) {
6039
+ passiveNode.node.expr = expr;
6040
+ }
6041
+ clearSourceIdentity(next, nodeId);
6042
+ markExprSync(next, nodeId);
6043
+ appendDebug(next, "setExpr", nodeId);
6044
+ return next;
6045
+ }
6003
6046
  function createStarterSession() {
6004
6047
  const englishRoot = createEditorChain([createEditorNode("char", "x")]);
6005
6048
  const arabicRoot = createEditorChain([createEditorNode("char", "\u0633")]);
@@ -6083,6 +6126,14 @@ function pasteText(session, text) {
6083
6126
  if (!text) {
6084
6127
  return session;
6085
6128
  }
6129
+ const selection = session.selection;
6130
+ if (selection && Math.abs(selection.anchorIndex - selection.focusIndex) === 1) {
6131
+ const selectedChain = findChainById(session[activeTreeKey(session.activeSide)], selection.chainId);
6132
+ const selected = selectedChain?.chain.nodes[Math.min(selection.anchorIndex, selection.focusIndex)];
6133
+ if (selected?.kind === "char" && ["\\mathtt", "\\operatorname", "\\text"].includes(selected.wrapperCommand ?? "")) {
6134
+ return setExpr(session, selected.id, text);
6135
+ }
6136
+ }
6086
6137
  let s = session;
6087
6138
  let i = 0;
6088
6139
  while (i < text.length) {
@@ -6142,6 +6193,8 @@ function buildBaseNodeBody(node, digitForm = "western", side = "english") {
6142
6193
  if (node.wrapperCommand === "\\boldarabic" || node.wrapperCommand === "\\mathbf") {
6143
6194
  value.classList.add("node-value--bold");
6144
6195
  }
6196
+ if (node.wrapperCommand === "\\text") value.dir = "auto";
6197
+ if (node.wrapperCommand === "\\operatorname") value.classList.add("node-value--operator-name");
6145
6198
  if (node.wrapperCommand === "\\unit") {
6146
6199
  value.classList.add("node-value--unit");
6147
6200
  value.title = "\u0648\u062D\u062F\u0629 (unit)";
@@ -6648,6 +6701,11 @@ var MATRIX_ENV_CSS = `
6648
6701
  transform-origin: center;
6649
6702
  }
6650
6703
 
6704
+ .matrix-env--cases .matrix-env__wrapper:empty {
6705
+ min-width: 0;
6706
+ padding: 0;
6707
+ }
6708
+
6651
6709
  .matrix-env__grid {
6652
6710
  display: inline-grid;
6653
6711
  direction: ltr;
@@ -6692,9 +6750,10 @@ function applyMatrixDelimiterScale(wrap, grid) {
6692
6750
  }
6693
6751
  function buildMatrixEnvNodeBody(node, buildChain, side) {
6694
6752
  const wrap = document.createElement("span");
6695
- wrap.className = `matrix-env matrix-env--${side}`;
6753
+ wrap.className = `matrix-env matrix-env--${side}${node.envName === "cases" ? " matrix-env--cases" : ""}`;
6754
+ if (node.envName === "cases") wrap.dir = "ltr";
6696
6755
  const style = node.envName ?? node.matrixStyle ?? "matrix";
6697
- const glyphs = style === "array" || style === "aligned" ? void 0 : WRAPPERS[style];
6756
+ const glyphs = style === "cases" ? side === "arabic" ? { left: "", right: "}" } : { left: "{", right: "" } : style === "array" || style === "aligned" ? void 0 : WRAPPERS[style];
6698
6757
  const left = document.createElement("span");
6699
6758
  left.className = "matrix-env__wrapper matrix-env__wrapper--left";
6700
6759
  left.textContent = glyphs?.left ?? "";
@@ -8144,6 +8203,10 @@ function createEditorRuntime(options) {
8144
8203
  applyStructural((prev) => insertAlignedEnv(prev, rows, columns));
8145
8204
  focusInput();
8146
8205
  },
8206
+ insertCasesEnv: (rows) => {
8207
+ applyStructural((prev) => insertCasesEnv(prev, rows));
8208
+ focusInput();
8209
+ },
8147
8210
  insertSqrt: () => {
8148
8211
  applyStructural((prev) => insertSqrt(prev));
8149
8212
  focusInput();
@@ -8294,11 +8357,17 @@ var COMMAND_FONT_MAP = Object.fromEntries([
8294
8357
  var CHAR_FONT_EXPORT_TEX = Object.fromEntries(
8295
8358
  NON_DEFAULT_CHARACTER_FONT_SPECS.map((font) => [font.id, font.macro])
8296
8359
  );
8360
+ function hasBareBoldCharSource(node) {
8361
+ return node.kind === "char" && (node.wrapperCommand === "\\boldarabic" || node.wrapperCommand === "\\mathbf") && typeof node.sourceLatex === "string" && node.sourceLatex.length > 0 && !node.sourceLatex.startsWith("\\mathbf{");
8362
+ }
8297
8363
  function charEditorNodeToAstNode(node) {
8298
8364
  const font = node.characterFont ?? "default";
8299
8365
  const charNode = new CharNode(node.expr);
8300
8366
  if (node.wrapperCommand) {
8301
8367
  const wrapper = node.wrapperCommand === "\\boldarabic" ? "\\mathbf" : node.wrapperCommand;
8368
+ if (hasBareBoldCharSource(node)) {
8369
+ charNode.sourceLatex = node.sourceLatex;
8370
+ }
8302
8371
  return new CommandNode(wrapper, [], [new ChainNode([charNode])]);
8303
8372
  }
8304
8373
  if (font === "default") {
@@ -8316,8 +8385,9 @@ function commandIdForTex(tex) {
8316
8385
  return null;
8317
8386
  }
8318
8387
  function operatorCommandIdForTex(tex) {
8388
+ const normalized = tex === "\\le" ? "\\leq" : tex === "\\ge" ? "\\geq" : tex;
8319
8389
  for (const spec of Object.values(ATOMIC_OPERATOR_COMMANDS)) {
8320
- if (spec.Tex === tex) {
8390
+ if (spec.Tex === normalized) {
8321
8391
  return spec.id;
8322
8392
  }
8323
8393
  }
@@ -8348,7 +8418,7 @@ function attachScripts(astNode, editorNode) {
8348
8418
  }
8349
8419
  var MATRIX_ENV_NAMES = /* @__PURE__ */ new Set(["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"]);
8350
8420
  function isGridEnvName(value) {
8351
- return MATRIX_ENV_NAMES.has(value) || value === "array" || value === "aligned";
8421
+ return MATRIX_ENV_NAMES.has(value) || value === "array" || value === "aligned" || value === "cases";
8352
8422
  }
8353
8423
  function isMatrixEnvName(value) {
8354
8424
  return MATRIX_ENV_NAMES.has(value);
@@ -8395,6 +8465,9 @@ function envToEditorNode(node) {
8395
8465
  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}` };
8396
8466
  }
8397
8467
  const rowCells = node.lines.map(splitEnvLine);
8468
+ if (parsed.envName === "cases" && (rowCells.length === 0 || rowCells.length > 12 || rowCells.some((row) => row.length !== 2))) {
8469
+ 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)" };
8470
+ }
8398
8471
  const rows = Math.max(1, rowCells.length);
8399
8472
  const columns = Math.max(1, ...rowCells.map((row) => row.length));
8400
8473
  const env = createGridEnvNode(parsed.envName, rows, columns, parsed.alignments);
@@ -8477,14 +8550,19 @@ function wrappedAtomicCommandId(node) {
8477
8550
  return commandIdForTex(`${node.name}{${child.expr}}`);
8478
8551
  }
8479
8552
  function editableWrapperCommand(node) {
8480
- if (node.name !== "\\mathtt" && node.name !== "\\mathsf" && node.name !== "\\unit" && node.name !== "\\mathbf" && node.name !== "\\boldarabic") {
8553
+ if (node.name !== "\\mathtt" && node.name !== "\\operatorname" && node.name !== "\\text" && node.name !== "\\mathsf" && node.name !== "\\unit" && node.name !== "\\mathbf" && node.name !== "\\boldarabic") {
8554
+ return null;
8555
+ }
8556
+ if (node.name === "\\text" && node.sourceLatex && !node.sourceLatex.startsWith("\\text{")) {
8481
8557
  return null;
8482
8558
  }
8483
8559
  const child = singleCommandChar(node);
8560
+ if (node.name === "\\text" && child?.sourceLatex && !node.sourceLatex) return null;
8484
8561
  let editorNode;
8485
8562
  if (child) {
8486
8563
  const normalized = normalizeCharImportExpr(child.expr);
8487
8564
  editorNode = createEditorNode("char", normalized.expr);
8565
+ if (child.sourceLatex) editorNode.sourceLatex = child.sourceLatex;
8488
8566
  if (normalized.characterFont !== "default") {
8489
8567
  editorNode.characterFont = normalized.characterFont;
8490
8568
  }
@@ -8496,7 +8574,7 @@ function editableWrapperCommand(node) {
8496
8574
  if (!nested?.editable) return null;
8497
8575
  editorNode = nested.node;
8498
8576
  }
8499
- editorNode.wrapperCommand = node.name === "\\mathtt" ? "\\mathtt" : node.name === "\\mathbf" ? "\\mathbf" : node.name === "\\boldarabic" ? "\\boldarabic" : "\\unit";
8577
+ editorNode.wrapperCommand = node.name === "\\mathsf" ? "\\unit" : node.name;
8500
8578
  const scripts = attachScripts(node, editorNode);
8501
8579
  if (scripts) {
8502
8580
  return scripts;
@@ -8630,6 +8708,7 @@ function commandToEditorNode(node) {
8630
8708
  const operatorId = operatorCommandIdForTex(node.name);
8631
8709
  if (operatorId && node.optionalArgs.length === 0 && node.mandatoryArgs.length === 0) {
8632
8710
  const atomicOperator = createEditorNode("atomicOperatorCommand", operatorId);
8711
+ if (node.name === "\\le" || node.name === "\\ge") atomicOperator.sourceLatex = node.name;
8633
8712
  const scripts = attachScripts(node, atomicOperator);
8634
8713
  if (scripts) {
8635
8714
  return scripts;
@@ -8941,7 +9020,7 @@ function editorNodeToAstNode(node) {
8941
9020
  if (!tex) {
8942
9021
  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}` };
8943
9022
  }
8944
- astNode = new CommandNode(tex);
9023
+ astNode = new CommandNode(node.sourceLatex === "\\le" || node.sourceLatex === "\\ge" ? node.sourceLatex : tex);
8945
9024
  } else if (node.kind === "standardCommand") {
8946
9025
  if (!isStandardCommandTex(node.expr)) {
8947
9026
  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}` };
@@ -8952,7 +9031,7 @@ function editorNodeToAstNode(node) {
8952
9031
  } else {
8953
9032
  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}` };
8954
9033
  }
8955
- if (node.sourceLatex) {
9034
+ if (node.sourceLatex && !hasBareBoldCharSource(node)) {
8956
9035
  astNode.sourceLatex = node.sourceLatex;
8957
9036
  }
8958
9037
  const scripts = attachAstScripts(node, astNode);
@@ -9010,8 +9089,8 @@ function mathTokenToEditorSession(math, side = "arabic") {
9010
9089
  result.session.sync = buildInitialSyncMap(result.session.englishTree, result.session.arabicTree);
9011
9090
  return result;
9012
9091
  }
9013
- function mathSourceFromSession(session, side, opening = "$", closing = "$") {
9014
- const inner = side === "english" ? renderEnglishLatexFromSession(session) : renderArabicLatexFromSession(session);
9092
+ function mathSourceFromSession(session, side, opening = "$", closing = "$", presentation = false) {
9093
+ const inner = side === "english" ? renderEnglishLatexFromSession(session) : presentation ? renderArabicPreviewLatexFromSession(session) : renderArabicLatexFromSession(session);
9015
9094
  if (opening === "$" || opening === "\\(") {
9016
9095
  return opening + inner + closing;
9017
9096
  }
@@ -9031,11 +9110,11 @@ function mathNodeFromSession(session, opening = "$", closing = "$") {
9031
9110
  }
9032
9111
  return { nodeType: "MathObject", mathMode: opening, lines: [], closing };
9033
9112
  }
9034
- function mathTokenSourceForSide(token, side, digitForm) {
9113
+ function mathTokenSourceForSide(token, side, digitForm, presentation = false) {
9035
9114
  if (!token.math || token.sourceOwner === "raw") {
9036
9115
  return token.source;
9037
9116
  }
9038
- if (token.sourceSide === side && digitForm === void 0) {
9117
+ if (!presentation && token.sourceSide === side && digitForm === void 0) {
9039
9118
  return token.source;
9040
9119
  }
9041
9120
  const result = mathTokenToEditorSession(token.math, side);
@@ -9043,7 +9122,7 @@ function mathTokenSourceForSide(token, side, digitForm) {
9043
9122
  return token.source;
9044
9123
  }
9045
9124
  const session = digitForm === void 0 ? result.session : setCurrentDigitForm(result.session, digitForm);
9046
- return mathSourceFromSession(session, side, token.opening, token.closing);
9125
+ return mathSourceFromSession(session, side, token.opening, token.closing, presentation);
9047
9126
  }
9048
9127
 
9049
9128
  // src/document/commands.ts
@@ -10295,9 +10374,13 @@ function digitsMappingFromDigitForm(digitForm) {
10295
10374
  }
10296
10375
  return "arabicdigits";
10297
10376
  }
10377
+ function editorMathLatexForCompile(token) {
10378
+ const source = token.math && token.sourceSide === "arabic" && token.source.includes("\\begin{cases}") ? mathTokenSourceForSide(token, "arabic", void 0, true) : token.source;
10379
+ return mirrorBuTeXOperatorsInTex(source, { target: "compile" });
10380
+ }
10298
10381
  function mathBodyLatex(token) {
10299
10382
  if (token.sourceOwner === "editor") {
10300
- return stripDisplayMathDelimiters(mirrorBuTeXOperatorsInTex(token.source, { target: "compile" }));
10383
+ return stripDisplayMathDelimiters(editorMathLatexForCompile(token));
10301
10384
  }
10302
10385
  if (!token.math || token.sourceOwner === "raw") {
10303
10386
  return stripDisplayMathDelimiters(token.source);
@@ -10372,7 +10455,7 @@ ${body}
10372
10455
  \\end{equation}`;
10373
10456
  }
10374
10457
  if (token.sourceOwner === "editor") {
10375
- return mirrorBuTeXOperatorsInTex(token.source, { target: "compile" });
10458
+ return editorMathLatexForCompile(token);
10376
10459
  }
10377
10460
  if (!token.math || token.sourceOwner === "raw") {
10378
10461
  return token.source;
@@ -10948,7 +11031,7 @@ function remapSelectionAfterRangeMove(selection, direction) {
10948
11031
 
10949
11032
  // src/document/previewModel.ts
10950
11033
  function mathTex(token, equationSide, digitForm) {
10951
- return mathTokenSourceForSide(token, equationSide, digitForm);
11034
+ return mathTokenSourceForSide(token, equationSide, digitForm, true);
10952
11035
  }
10953
11036
  function previewInlines(field, islands, output, equationSide, document2, previewOptions) {
10954
11037
  return field.tokens.map((token) => {
@@ -12292,7 +12375,7 @@ function MathChip({
12292
12375
  onTextInputFromFocus
12293
12376
  }) {
12294
12377
  const messages = documentMessages(uiLocale);
12295
- const tex = mathTokenSourceForSide(token, equationSide, digitForm);
12378
+ const tex = mathTokenSourceForSide(token, equationSide, digitForm, true);
12296
12379
  const forwardArrowKey = documentDirection === "rtl" ? "ArrowLeft" : "ArrowRight";
12297
12380
  const backwardArrowKey = documentDirection === "rtl" ? "ArrowRight" : "ArrowLeft";
12298
12381
  const island = /* @__PURE__ */ jsx4(MathIsland, { id: token.id, tex, display: token.display, output });
@@ -16080,7 +16163,7 @@ function EquationMobileToolbar({
16080
16163
  if (kind === "rows") {
16081
16164
  setMatrixRows(next);
16082
16165
  } else {
16083
- setMatrixColumns(envMode === "aligned" ? 2 : next);
16166
+ setMatrixColumns(envMode === "aligned" || envMode === "cases" ? 2 : next);
16084
16167
  }
16085
16168
  }
16086
16169
  function insertEnvironment() {
@@ -16089,6 +16172,8 @@ function EquationMobileToolbar({
16089
16172
  runtime.insertArrayEnv(matrixRows, matrixColumns);
16090
16173
  } else if (envMode === "aligned") {
16091
16174
  runtime.insertAlignedEnv(matrixRows, 2);
16175
+ } else if (envMode === "cases") {
16176
+ runtime.insertCasesEnv(matrixRows);
16092
16177
  } else {
16093
16178
  runtime.insertMatrixEnv(matrixStyle, matrixRows, matrixColumns);
16094
16179
  }
@@ -16256,7 +16341,8 @@ function EquationMobileToolbar({
16256
16341
  /* @__PURE__ */ jsx13("div", { className: "butex-widget__sheet-choice-row", role: "group", "aria-label": messages.environmentType, children: [
16257
16342
  { id: "matrix", label: messages.matrix },
16258
16343
  { id: "array", label: messages.array },
16259
- { id: "aligned", label: messages.aligned }
16344
+ { id: "aligned", label: messages.aligned },
16345
+ { id: "cases", label: messages.piecewise }
16260
16346
  ].map((option) => /* @__PURE__ */ jsx13(
16261
16347
  "button",
16262
16348
  {
@@ -16264,7 +16350,7 @@ function EquationMobileToolbar({
16264
16350
  "aria-pressed": envMode === option.id,
16265
16351
  onClick: () => {
16266
16352
  setEnvMode(option.id);
16267
- if (option.id === "aligned") setMatrixColumns(2);
16353
+ if (option.id === "aligned" || option.id === "cases") setMatrixColumns(2);
16268
16354
  },
16269
16355
  children: option.label
16270
16356
  },
@@ -16273,19 +16359,19 @@ function EquationMobileToolbar({
16273
16359
  envMode === "matrix" ? /* @__PURE__ */ jsx13("div", { className: "butex-widget__sheet-choice-row", role: "group", "aria-label": messages.matrixType, children: MATRIX_STYLES.map((style) => /* @__PURE__ */ jsx13("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,
16274
16360
  /* @__PURE__ */ jsxs12("section", { className: "butex-widget__sheet-section", children: [
16275
16361
  /* @__PURE__ */ jsx13("h4", { children: messages.commonMatrixSizes }),
16276
- /* @__PURE__ */ jsx13("div", { className: "butex-widget__matrix-presets", children: (envMode === "aligned" ? MATRIX_PRESETS.filter((preset) => preset.columns === 2) : MATRIX_PRESETS).map((preset) => /* @__PURE__ */ jsxs12(
16362
+ /* @__PURE__ */ jsx13("div", { className: "butex-widget__matrix-presets", children: (envMode === "aligned" || envMode === "cases" ? MATRIX_PRESETS.filter((preset) => preset.columns === 2) : MATRIX_PRESETS).map((preset) => /* @__PURE__ */ jsxs12(
16277
16363
  "button",
16278
16364
  {
16279
16365
  type: "button",
16280
16366
  "aria-pressed": matrixRows === preset.rows && matrixColumns === preset.columns,
16281
16367
  onClick: () => {
16282
16368
  setMatrixRows(preset.rows);
16283
- setMatrixColumns(envMode === "aligned" ? 2 : preset.columns);
16369
+ setMatrixColumns(envMode === "aligned" || envMode === "cases" ? 2 : preset.columns);
16284
16370
  },
16285
16371
  children: [
16286
16372
  preset.rows,
16287
16373
  "\xD7",
16288
- envMode === "aligned" ? 2 : preset.columns
16374
+ envMode === "aligned" || envMode === "cases" ? 2 : preset.columns
16289
16375
  ]
16290
16376
  },
16291
16377
  `${String(preset.rows)}-${String(preset.columns)}`
@@ -16302,12 +16388,12 @@ function EquationMobileToolbar({
16302
16388
  /* @__PURE__ */ jsxs12("div", { className: "butex-widget__matrix-stepper-row", children: [
16303
16389
  /* @__PURE__ */ jsx13("span", { children: messages.columns }),
16304
16390
  /* @__PURE__ */ jsxs12("div", { className: "butex-widget__matrix-stepper", children: [
16305
- /* @__PURE__ */ jsx13("button", { type: "button", disabled: matrixColumns <= 1 || envMode === "aligned", "aria-label": messages.decreaseColumns, onClick: () => setMatrixDimension("columns", matrixColumns - 1), children: "\u2212" }),
16306
- /* @__PURE__ */ jsx13("output", { "aria-live": "polite", children: envMode === "aligned" ? 2 : matrixColumns }),
16307
- /* @__PURE__ */ jsx13("button", { type: "button", disabled: matrixColumns >= 12 || envMode === "aligned", "aria-label": messages.increaseColumns, onClick: () => setMatrixDimension("columns", matrixColumns + 1), children: "+" })
16391
+ /* @__PURE__ */ jsx13("button", { type: "button", disabled: matrixColumns <= 1 || envMode === "aligned" || envMode === "cases", "aria-label": messages.decreaseColumns, onClick: () => setMatrixDimension("columns", matrixColumns - 1), children: "\u2212" }),
16392
+ /* @__PURE__ */ jsx13("output", { "aria-live": "polite", children: envMode === "aligned" || envMode === "cases" ? 2 : matrixColumns }),
16393
+ /* @__PURE__ */ jsx13("button", { type: "button", disabled: matrixColumns >= 12 || envMode === "aligned" || envMode === "cases", "aria-label": messages.increaseColumns, onClick: () => setMatrixDimension("columns", matrixColumns + 1), children: "+" })
16308
16394
  ] })
16309
16395
  ] }),
16310
- /* @__PURE__ */ jsx13("p", { className: "butex-widget__matrix-size-summary", "aria-live": "polite", children: matrixSizeLabel(matrixRows, envMode === "aligned" ? 2 : matrixColumns, messages) }),
16396
+ /* @__PURE__ */ jsx13("p", { className: "butex-widget__matrix-size-summary", "aria-live": "polite", children: matrixSizeLabel(matrixRows, envMode === "aligned" || envMode === "cases" ? 2 : matrixColumns, messages) }),
16311
16397
  /* @__PURE__ */ jsx13("button", { type: "button", className: "butex-widget__matrix-insert", onClick: insertEnvironment, children: messages.insert }),
16312
16398
  /* @__PURE__ */ jsxs12("section", { className: "butex-widget__sheet-section", children: [
16313
16399
  /* @__PURE__ */ jsx13("h4", { children: messages.editSelectedEnvironment }),
@@ -16328,11 +16414,11 @@ function EquationMobileToolbar({
16328
16414
  "\u2212",
16329
16415
  messages.row
16330
16416
  ] }),
16331
- /* @__PURE__ */ jsxs12("button", { type: "button", onClick: () => getRuntime()?.addMatrixColumn(), children: [
16417
+ /* @__PURE__ */ jsxs12("button", { type: "button", disabled: envMode === "cases", onClick: () => getRuntime()?.addMatrixColumn(), children: [
16332
16418
  "+",
16333
16419
  messages.column
16334
16420
  ] }),
16335
- /* @__PURE__ */ jsxs12("button", { type: "button", onClick: () => getRuntime()?.removeMatrixColumn(), children: [
16421
+ /* @__PURE__ */ jsxs12("button", { type: "button", disabled: envMode === "cases", onClick: () => getRuntime()?.removeMatrixColumn(), children: [
16336
16422
  "\u2212",
16337
16423
  messages.column
16338
16424
  ] })
@@ -16863,7 +16949,7 @@ var ButexEditor = forwardRef(
16863
16949
  const debugLogEl = debugLogRef.current;
16864
16950
  const english = renderEnglishLatexFromSession(session);
16865
16951
  const arabic = renderArabicLatexFromSession(session);
16866
- const activeLatex = session.activeSide === "arabic" ? arabic : english;
16952
+ const activeLatex = session.activeSide === "arabic" ? renderArabicPreviewLatexFromSession(session) : english;
16867
16953
  if (debugRef.current && latexLinesEl) {
16868
16954
  const currentMessages = equationEditorMessages(uiLocaleRef.current);
16869
16955
  latexLinesEl.textContent = `${currentMessages.englishLatex}:
@@ -17063,6 +17149,8 @@ ${arabic || currentMessages.empty}`;
17063
17149
  runtimeRef.current?.insertArrayEnv(rows, columns);
17064
17150
  } else if (envPaletteMode === "aligned") {
17065
17151
  runtimeRef.current?.insertAlignedEnv(rows, columns);
17152
+ } else if (envPaletteMode === "cases") {
17153
+ runtimeRef.current?.insertCasesEnv(rows);
17066
17154
  } else {
17067
17155
  runtimeRef.current?.insertMatrixEnv(matrixStyle, rows, columns);
17068
17156
  }
@@ -17358,7 +17446,8 @@ ${arabic || currentMessages.empty}`;
17358
17446
  /* @__PURE__ */ jsx14("div", { className: "matrix-style-options", role: "listbox", "aria-label": messages.environmentType, children: [
17359
17447
  { id: "matrix", label: messages.matrix },
17360
17448
  { id: "array", label: messages.array },
17361
- { id: "aligned", label: messages.aligned }
17449
+ { id: "aligned", label: messages.aligned },
17450
+ { id: "cases", label: messages.piecewise }
17362
17451
  ].map((option) => /* @__PURE__ */ jsx14(
17363
17452
  "button",
17364
17453
  {
@@ -17369,7 +17458,7 @@ ${arabic || currentMessages.empty}`;
17369
17458
  onMouseDown: (event) => event.preventDefault(),
17370
17459
  onClick: () => {
17371
17460
  setEnvPaletteMode(option.id);
17372
- if (option.id === "aligned") {
17461
+ if (option.id === "aligned" || option.id === "cases") {
17373
17462
  setMatrixColumns(2);
17374
17463
  }
17375
17464
  },
@@ -17394,7 +17483,7 @@ ${arabic || currentMessages.empty}`;
17394
17483
  style.id
17395
17484
  )) }) : null,
17396
17485
  /* @__PURE__ */ jsx14("div", { className: "matrix-size-readout", children: matrixHover ? matrixSizeLabel2(matrixHover.rows, matrixHover.columns, messages) : matrixSizeLabel2(matrixRows, matrixColumns, messages) }),
17397
- /* @__PURE__ */ jsx14("div", { className: "matrix-grid-picker", "aria-label": messages.chooseEnvironmentSize, children: Array.from(
17486
+ envPaletteMode !== "cases" ? /* @__PURE__ */ jsx14("div", { className: "matrix-grid-picker", "aria-label": messages.chooseEnvironmentSize, children: Array.from(
17398
17487
  { length: 8 },
17399
17488
  (_, rowIndex) => Array.from({ length: 8 }, (_2, columnIndex) => {
17400
17489
  const rows = rowIndex + 1;
@@ -17422,7 +17511,7 @@ ${arabic || currentMessages.empty}`;
17422
17511
  `${rows}-${columns}`
17423
17512
  );
17424
17513
  })
17425
- ) }),
17514
+ ) }) : null,
17426
17515
  /* @__PURE__ */ jsxs13("div", { className: "matrix-custom-size", children: [
17427
17516
  /* @__PURE__ */ jsxs13("label", { children: [
17428
17517
  messages.row,
@@ -17445,7 +17534,8 @@ ${arabic || currentMessages.empty}`;
17445
17534
  type: "number",
17446
17535
  min: 1,
17447
17536
  max: 12,
17448
- value: matrixColumns,
17537
+ value: envPaletteMode === "cases" || envPaletteMode === "aligned" ? 2 : matrixColumns,
17538
+ disabled: envPaletteMode === "cases" || envPaletteMode === "aligned",
17449
17539
  onChange: (event) => setMatrixColumns(clampMatrixSize2(Number(event.currentTarget.value) || 1))
17450
17540
  }
17451
17541
  )
@@ -17458,7 +17548,7 @@ ${arabic || currentMessages.empty}`;
17458
17548
  onMouseDown: (event) => event.preventDefault(),
17459
17549
  onClick: () => {
17460
17550
  const rows = clampMatrixSize2(matrixRows);
17461
- const columns = clampMatrixSize2(matrixColumns);
17551
+ const columns = envPaletteMode === "cases" || envPaletteMode === "aligned" ? 2 : clampMatrixSize2(matrixColumns);
17462
17552
  insertSelectedEnvironment(rows, columns);
17463
17553
  setMatrixMenuOpen(false);
17464
17554
  setMatrixHover(null);
@@ -17504,11 +17594,11 @@ ${arabic || currentMessages.empty}`;
17504
17594
  "-",
17505
17595
  messages.row
17506
17596
  ] }),
17507
- /* @__PURE__ */ jsxs13("button", { type: "button", title: messages.addColumn, onMouseDown: (event) => event.preventDefault(), onClick: () => runtimeRef.current?.addMatrixColumn(), children: [
17597
+ /* @__PURE__ */ jsxs13("button", { type: "button", title: messages.addColumn, disabled: envPaletteMode === "cases", onMouseDown: (event) => event.preventDefault(), onClick: () => runtimeRef.current?.addMatrixColumn(), children: [
17508
17598
  "+",
17509
17599
  messages.column
17510
17600
  ] }),
17511
- /* @__PURE__ */ jsxs13("button", { type: "button", title: messages.removeColumn, onMouseDown: (event) => event.preventDefault(), onClick: () => runtimeRef.current?.removeMatrixColumn(), children: [
17601
+ /* @__PURE__ */ jsxs13("button", { type: "button", title: messages.removeColumn, disabled: envPaletteMode === "cases", onMouseDown: (event) => event.preventDefault(), onClick: () => runtimeRef.current?.removeMatrixColumn(), children: [
17512
17602
  "-",
17513
17603
  messages.column
17514
17604
  ] })