Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧮 Fix AMSMath environment for LaTeX #977

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-books-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-to-tex": patch
---

Properly handle AMS environments, and no longer wrap with equation environment.
55 changes: 47 additions & 8 deletions packages/myst-to-tex/src/math.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import type { Handler, ITexSerializer } from './types.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 = [
'equation',
'multline',
'gather',
'align',
'alignat',
'flalign',
'matrix',
'pmatrix',
'bmatrix',
'Bmatrix',
'vmatrix',
'Vmatrix',
'eqnarray',
];

const RE_OPEN = new RegExp(`^\\\\begin{(${ENVIRONMENTS.join('|')})([*]?)}`);

function isAmsmathEnvironment(value: string) {
const matchOpen = value.trim().match(RE_OPEN);
if (!matchOpen) return false;
const [, environment, numbered] = matchOpen;
rowanc1 marked this conversation as resolved.
Show resolved Hide resolved
const end = `\\end{${environment}${numbered}}`;
const matchClose = value.trim().endsWith(end);
if (!matchClose) return false;
LecrisUT marked this conversation as resolved.
Show resolved Hide resolved
return { environment, numbered };
}

function addMacrosToState(value: string, state: ITexSerializer) {
if (!state.options.math) return;
Object.entries(state.options.math).forEach(([k, v]) => {
Expand Down Expand Up @@ -47,15 +77,24 @@ const math: Handler = (node, state) => {
state.write(node.value);
state.write(' \\)');
} else {
// TODO: AMS math
state.write(`\\begin{equation${enumerated === false ? '*' : ''}}\n`);
if (label) {
state.write(`\\label{${label}}`);
// Check if the node is an AMSMath environment, if so, render it directly
const isAmsMath = isAmsmathEnvironment(node.value);
if (isAmsMath) {
// TODO: labels may be stripped previously in the transform, we may need to back that out
state.ensureNewLine();
state.write(node.value);
state.ensureNewLine(true);
} else {
// Otherwise enclose the math environment by equation + label
state.write(`\\begin{equation${enumerated === false ? '*' : ''}}\n`);
if (label) {
state.write(`\\label{${label}}`);
}
state.ensureNewLine();
state.write(node.value);
state.ensureNewLine(true);
state.write(`\\end{equation${enumerated === false ? '*' : ''}}`);
}
state.ensureNewLine();
state.write(node.value);
state.ensureNewLine(true);
state.write(`\\end{equation${enumerated === false ? '*' : ''}}`);
}
if (!state.data.isInTable) state.closeBlock(node);
};
Expand Down
34 changes: 34 additions & 0 deletions packages/myst-to-tex/tests/amsmath.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
title: myst-to-tex amsmath tests
cases:
- title: gather
mdast:
type: root
children:
- type: math
value: |-
\begin{gather}
E=mc^2
\end{gather}
latex: |-
\begin{gather}
E=mc^2
\end{gather}
- title: alignat*
mdast:
type: root
children:
- type: math
value: |-
\begin{alignat*}{3}
& m \quad && \text{módulo} \quad && m>0\\
& a \quad && \text{multiplicador} \quad && 0<a<m\\
& c \quad && \text{constante aditiva} \quad && 0\leq c<m\\
& x_0 \quad && \text{valor inicial} \quad && 0\leq x_0 <m
\end{alignat*}
latex: |-
\begin{alignat*}{3}
& m \quad && \text{módulo} \quad && m>0\\
& a \quad && \text{multiplicador} \quad && 0<a<m\\
& c \quad && \text{constante aditiva} \quad && 0\leq c<m\\
& x_0 \quad && \text{valor inicial} \quad && 0\leq x_0 <m
\end{alignat*}
Loading