Skip to content

Commit

Permalink
🐛 Top level AMS Math environments are not wrapped. (#1526)
Browse files Browse the repository at this point in the history
Fixes #1519
  • Loading branch information
rowanc1 authored Sep 10, 2024
1 parent adb3c75 commit 594a0f8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/chilly-eyes-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"myst-to-tex": patch
"mystmd": patch
---

Top level AMS environments are not wrapped
21 changes: 6 additions & 15 deletions packages/myst-to-tex/src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
36 changes: 36 additions & 0 deletions packages/myst-to-tex/tests/amsmath.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit 594a0f8

Please sign in to comment.