diff --git a/.changeset/chilly-eyes-smash.md b/.changeset/chilly-eyes-smash.md new file mode 100644 index 000000000..199171b20 --- /dev/null +++ b/.changeset/chilly-eyes-smash.md @@ -0,0 +1,6 @@ +--- +"myst-to-tex": patch +"mystmd": patch +--- + +Top level AMS environments are not wrapped diff --git a/packages/myst-to-tex/src/math.ts b/packages/myst-to-tex/src/math.ts index 5ac1701c7..74804cc12 100644 --- a/packages/myst-to-tex/src/math.ts +++ b/packages/myst-to-tex/src/math.ts @@ -3,31 +3,22 @@ import { addIndexEntries } from './utils.js'; // Top level environments in amsmath version 2.1 (and eqnarray), see: // http://anorien.csc.warwick.ac.uk/mirrors/CTAN/macros/latex/required/amsmath/amsldoc.pdf -const ENVIRONMENTS = [ +const TOP_LEVEL_ENVIRONMENTS = [ 'equation', 'multline', 'gather', 'align', 'alignat', 'flalign', - 'matrix', - 'pmatrix', - 'bmatrix', - 'Bmatrix', - 'vmatrix', - 'Vmatrix', 'eqnarray', ]; +// The other environments can be inside of an equation +// const MATRIX_ENVIRONMENTS = ['matrix', 'pmatrix', 'bmatrix', 'Bmatrix', 'vmatrix', 'Vmatrix']; -const RE_OPEN = new RegExp(`^\\\\begin{(${ENVIRONMENTS.join('|')})([*]?)}`); +const RE_OPEN = new RegExp(`^\\\\begin{(${TOP_LEVEL_ENVIRONMENTS.join('|')})([*]?)}`); -function isAmsmathEnvironment(value: string): boolean { +function isTopLevelAmsmathEnvironment(value: string): boolean { // First test if there are multiple environments in this equation - const matches = value.trim().matchAll(new RegExp(`\\\\begin{(${ENVIRONMENTS.join('|')})}`, 'g')); - if ([...matches].length > 1) { - // If there are multiple amsmath environments, ensure we always return something with the equation wrappers - return false; - } const matchOpen = value.trim().match(RE_OPEN); if (!matchOpen) return false; const [, environment, star] = matchOpen; @@ -90,7 +81,7 @@ const math: Handler = (node, state) => { state.write(' \\)'); } else { // Check if the node is an AMSMath environment, if so, render it directly - const isAmsMath = isAmsmathEnvironment(node.value); + const isAmsMath = isTopLevelAmsmathEnvironment(node.value); if (isAmsMath) { // TODO: labels may be stripped previously in the transform, we may need to back that out state.ensureNewLine(); diff --git a/packages/myst-to-tex/tests/amsmath.yml b/packages/myst-to-tex/tests/amsmath.yml index aabcd0085..e575b0e40 100644 --- a/packages/myst-to-tex/tests/amsmath.yml +++ b/packages/myst-to-tex/tests/amsmath.yml @@ -47,3 +47,39 @@ cases: = \frac{1}{r} \begin{pmatrix}- \sin\theta^1 & \cos\theta^1 \\ - \sin\theta^2 & \cos\theta^2 \\ - \sin\theta^3 & \cos\theta^3 \end{pmatrix} \begin{pmatrix}v_x \\ v_y \end{pmatrix} \end{equation} + - title: bmatrix environment + mdast: + type: root + children: + - type: math + value: |- + \begin{bmatrix} + 3 & \textbf{3} & \textbf{3} & \textbf{5} & 5 & 5 & 5 & 2 & 2 & 2 \\ + 3 & 0 & \textbf{2} & 2 & 0 & 0 & -3 & -3 & 0 & -2 + \end{bmatrix} + latex: |- + \begin{equation} + \begin{bmatrix} + 3 & \textbf{3} & \textbf{3} & \textbf{5} & 5 & 5 & 5 & 2 & 2 & 2 \\ + 3 & 0 & \textbf{2} & 2 & 0 & 0 & -3 & -3 & 0 & -2 + \end{bmatrix} + \end{equation} + - title: align environment + mdast: + type: root + children: + - type: math + value: |- + \begin{align} + \begin{bmatrix} + 3 & \textbf{3} & \textbf{3} & \textbf{5} & 5 & 5 & 5 & 2 & 2 & 2 \\ + 3 & 0 & \textbf{2} & 2 & 0 & 0 & -3 & -3 & 0 & -2 + \end{bmatrix} + \end{align} + latex: |- + \begin{align} + \begin{bmatrix} + 3 & \textbf{3} & \textbf{3} & \textbf{5} & 5 & 5 & 5 & 2 & 2 & 2 \\ + 3 & 0 & \textbf{2} & 2 & 0 & 0 & -3 & -3 & 0 & -2 + \end{bmatrix} + \end{align}