From 3ce5404c0e34f20493598b053b096ecb1986b608 Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Tue, 29 Oct 2024 22:51:44 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=87=20Update=20tex=20image=20width=20(?= =?UTF-8?q?#1612)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/eleven-kings-repeat.md | 5 +++++ packages/tex-to-myst/src/figures.ts | 15 +++++++++++++-- packages/tex-to-myst/tests/figures.yml | 5 +++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 .changeset/eleven-kings-repeat.md diff --git a/.changeset/eleven-kings-repeat.md b/.changeset/eleven-kings-repeat.md new file mode 100644 index 000000000..c85fb563f --- /dev/null +++ b/.changeset/eleven-kings-repeat.md @@ -0,0 +1,5 @@ +--- +"tex-to-myst": patch +--- + +Update width of image diff --git a/packages/tex-to-myst/src/figures.ts b/packages/tex-to-myst/src/figures.ts index 92906cd43..31a1f5b25 100644 --- a/packages/tex-to-myst/src/figures.ts +++ b/packages/tex-to-myst/src/figures.ts @@ -44,8 +44,19 @@ const FIGURE_HANDLERS: Record = { 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, diff --git a/packages/tex-to-myst/tests/figures.yml b/packages/tex-to-myst/tests/figures.yml index c7c2eeb93..3a2851b86 100644 --- a/packages/tex-to-myst/tests/figures.yml +++ b/packages/tex-to-myst/tests/figures.yml @@ -19,6 +19,7 @@ cases: children: - type: image url: figures/my_pic.png + width: 100% - type: caption children: - type: paragraph @@ -50,6 +51,7 @@ cases: children: - type: image url: fig.jpg + width: 95% - type: paragraph children: - type: text @@ -78,6 +80,7 @@ cases: children: - type: image url: img/Figure 4.png + width: 80% - type: caption children: - type: paragraph @@ -98,6 +101,7 @@ cases: children: - type: image url: figures/wiki-complex.png + width: 70% - title: subfigure tex: |- \begin{figure} @@ -207,6 +211,7 @@ cases: children: - type: image url: figures/my_pic.png + width: 100% - type: caption children: - type: paragraph