Files
docx/src/math/math-phantom.ts
T
DolanandClaude Opus 5.5 eb206519ae feat: math from LaTeX in docx/math, with accents, bars, braces, boxes, phantoms and large operators (#3570)
* feat: math from LaTeX in docx/math, with accents, bars, braces, boxes, phantoms and large operators

latexToMath turns LaTeX math into docx's math components, to go in a Math.
It reads what LaTeX, MathJax and KaTeX write: fractions, roots, scripts,
large operators, functions, \left and \right, accents, braces, boxes,
fonts, \text, and the environments for matrices, cases and aligned
equations, with \tag. It throws for what it can't read, saying where.

The parts LaTeX needs are public components too: MathLargeOperator,
MathAccent, MathBar, MathBrace, MathBox and MathPhantom in docx/math, and
in docx, MathRun's style, script and literal, MathFraction's type, and
limits on MathSum and MathIntegral.

Closes #2994

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: draw MathRun alphabets upright unless given a style, and escape the \middle| table cell

A run with a script but no style wrote no m:sty, which Word draws as italic, so sansSerif came out italic
sans-serif rather than the upright 𝖠 its docs show. A script other than roman now defaults to a plain style,
as LaTeX draws its alphabets; a given style is kept. latexToMath already gave each alphabet its style.

The | after \middle in the LaTeX docs' table ended the cell, splitting the row into three.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 17:07:59 +01:00

75 lines
2.4 KiB
TypeScript

/**
* Phantoms: math that takes up room without being seen, or is seen without taking up room, such as LaTeX's
* `\phantom` and `\smash`.
*
* @module
*/
import { BuilderElement, type MathComponent } from "docx";
import { checkArgument, createArgument, createValueElement } from "./math-elements";
/**
* Options for {@link MathPhantom}.
*/
export type MathPhantomOptions = {
/** The math */
readonly children: readonly MathComponent[];
/**
* Whether the math is seen (`m:show`).
* @default false
*/
readonly visible?: boolean;
/**
* Whether it takes up its width. False gives it none (`m:zeroWid`), as LaTeX's `\vphantom`.
* @default true
*/
readonly width?: boolean;
/**
* Whether it takes up its height above the line. False gives it none (`m:zeroAsc`), as LaTeX's `\hphantom` and
* `\smash`.
* @default true
*/
readonly height?: boolean;
/**
* Whether it takes up its depth below the line. False gives it none (`m:zeroDesc`), as LaTeX's `\hphantom` and
* `\smash`.
* @default true
*/
readonly depth?: boolean;
};
/**
* A phantom (`m:phant`): math that takes up room without being seen, as LaTeX's `\phantom`, to leave space for it or
* line things up with it. Or math that is seen but takes up no room, or less, as `\smash`.
*
* @example
* ```typescript
* // As much space as "x+y" takes up
* new MathPhantom({ children: [new MathRun("x+y")] });
*
* // A tall fraction that doesn't push its line apart
* new MathPhantom({ visible: true, height: false, depth: false, children: [fraction] });
* ```
*/
export class MathPhantom extends BuilderElement {
public constructor({ children, visible = false, width = true, height = true, depth = true }: MathPhantomOptions) {
checkArgument("MathPhantom", "children", children);
super({
name: "m:phant",
children: [
new BuilderElement({
name: "m:phantPr",
children: [
createValueElement("m:show", visible ? 1 : 0),
...(width ? [] : [createValueElement("m:zeroWid", 1)]),
...(height ? [] : [createValueElement("m:zeroAsc", 1)]),
...(depth ? [] : [createValueElement("m:zeroDesc", 1)]),
],
}),
createArgument(children),
],
});
}
}