Skip to content

Commit

Permalink
🎇 Update tex image width (#1612)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Oct 30, 2024
1 parent 1837772 commit 3ce5404
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-kings-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tex-to-myst": patch
---

Update width of image
15 changes: 13 additions & 2 deletions packages/tex-to-myst/src/figures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@ const FIGURE_HANDLERS: Record<string, Handler> = {
macro_includegraphics(node, state) {
state.closeParagraph();
const url = texToText(getArguments(node, 'group'));
// TODO: width, placement, etc.
state.pushNode(u('image', { url }));
const args = getArguments(node, 'argument')?.[0].content;
// TODO: better width, placement, etc.
if (
args.length === 4 &&
args[0].content === 'width' &&
args[1].content === '=' &&
Number.isFinite(Number.parseFloat(args[2].content))
) {
const width = `${Math.round(Number.parseFloat(args[2].content) * 100)}%`;
state.pushNode(u('image', { url, width }));
} else {
state.pushNode(u('image', { url }));
}
},
macro_caption: renderCaption,
macro_captionof: renderCaption,
Expand Down
5 changes: 5 additions & 0 deletions packages/tex-to-myst/tests/figures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cases:
children:
- type: image
url: figures/my_pic.png
width: 100%
- type: caption
children:
- type: paragraph
Expand Down Expand Up @@ -50,6 +51,7 @@ cases:
children:
- type: image
url: fig.jpg
width: 95%
- type: paragraph
children:
- type: text
Expand Down Expand Up @@ -78,6 +80,7 @@ cases:
children:
- type: image
url: img/Figure 4.png
width: 80%
- type: caption
children:
- type: paragraph
Expand All @@ -98,6 +101,7 @@ cases:
children:
- type: image
url: figures/wiki-complex.png
width: 70%
- title: subfigure
tex: |-
\begin{figure}
Expand Down Expand Up @@ -207,6 +211,7 @@ cases:
children:
- type: image
url: figures/my_pic.png
width: 100%
- type: caption
children:
- type: paragraph
Expand Down

0 comments on commit 3ce5404

Please sign in to comment.