Skip to content

Commit

Permalink
🧮 Trim all math to ensure no new lines in latex output (#1728)
Browse files Browse the repository at this point in the history
Fixes #1704
  • Loading branch information
rowanc1 authored Jan 6, 2025
1 parent 7afc691 commit 3b05051
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-moles-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-parser": patch
---

Explicitly trim math strings
12 changes: 8 additions & 4 deletions packages/myst-parser/src/tokensToMyst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,20 +280,22 @@ const defaultMdast: Record<string, TokenHandlerSpec> = {
math_inline_double: {
type: 'math',
noCloseToken: true,
isText: true,
isLeaf: true,
getAttrs(t) {
return {
value: t.content.trim(),
enumerated: t.meta?.enumerated,
};
},
},
math_block: {
type: 'math',
noCloseToken: true,
isText: true,
isLeaf: true,
getAttrs(t) {
const name = t.info || undefined;
return {
value: t.content.trim(),
...normalizeLabel(name),
enumerated: t.meta?.enumerated,
};
Expand All @@ -302,10 +304,11 @@ const defaultMdast: Record<string, TokenHandlerSpec> = {
math_block_label: {
type: 'math',
noCloseToken: true,
isText: true,
isLeaf: true,
getAttrs(t) {
const name = t.info || undefined;
return {
value: t.content.trim(),
...normalizeLabel(name),
enumerated: t.meta?.enumerated,
};
Expand All @@ -314,10 +317,11 @@ const defaultMdast: Record<string, TokenHandlerSpec> = {
amsmath: {
type: 'math',
noCloseToken: true,
isText: true,
isLeaf: true,
getAttrs(t, tokens, index, state) {
const tight = computeAmsmathTightness(state.src, t.map);
const attrs = {
value: t.content.trim(),
enumerated: t.meta?.enumerated,
} as Record<string, any>;
if (tight) attrs.tight = tight;
Expand Down
2 changes: 2 additions & 0 deletions packages/myst-parser/tests/math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Math } from 'myst-spec-ext';
describe('Test math tightness', () => {
test.each([
[true, 'p\n$$Ax=b$$\np'],
[true, 'p\n$$\nAx=b\n$$\np'],
[undefined, 'p\n\n$$Ax=b$$\n\np'],
['before', 'p\n$$Ax=b$$\n\np'],
// ['after', 'p\n\n$$Ax=b$$\np'], // TODO: this is (maybe) a bug in the dollar-math parser
Expand All @@ -18,6 +19,7 @@ describe('Test math tightness', () => {
expect((select('math', tree) as Math).tight).toBe(undefined);
mathNestingTransform(tree, file);
expect((select('math', tree) as Math).tight).toBe(tight);
expect((select('math', tree) as Math).value).toBe('Ax=b');
});
test.each([
[true, 'p\n:::{math}\nAx=b\n:::\np'],
Expand Down
6 changes: 6 additions & 0 deletions packages/mystmd/tests/basic-tex-math/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Paragraph
$$Ax=b$$
Paragraph

Paragraph around dollar math
$$
Ax=b
$$
Paragraph

Paragraph
\begin{equation}
Ax=b
Expand Down
6 changes: 6 additions & 0 deletions packages/mystmd/tests/outputs/basic-tex-math.tex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
\end{equation}
Paragraph

Paragraph around dollar math
\begin{equation}
Ax=b
\end{equation}
Paragraph

Paragraph
\begin{equation}
Ax=b
Expand Down

0 comments on commit 3b05051

Please sign in to comment.