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

🧮 Trim all math to ensure no new lines in latex output #1728

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all 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/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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dellaert can you confirm that this is the output that you would expect (see the outcome right below).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep!

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the result in latex (no more new line inside the equation).

Paragraph
\begin{equation}
Ax=b
Expand Down
Loading