From 6fa758f8648eb06ec451bc374bf13ab530ed2991 Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Fri, 13 Oct 2023 16:56:46 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9C=20Improvements=20to=20LaTeX=20pars?= =?UTF-8?q?ing=20(#672)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 📜 Improvements to LaTeX parsing * 🏷 SI units in JATS --- .changeset/gentle-beans-sip.md | 5 +++++ .changeset/silly-jars-deliver.md | 5 +++++ packages/myst-to-jats/src/index.ts | 2 +- packages/tex-to-myst/src/misc.ts | 4 ++++ packages/tex-to-myst/src/siunitx.ts | 15 ++++++++++++++- packages/tex-to-myst/src/tables.ts | 7 +++++++ packages/tex-to-myst/src/tex.ts | 2 ++ packages/tex-to-myst/tests/siunitx.yml | 3 +++ 8 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 .changeset/gentle-beans-sip.md create mode 100644 .changeset/silly-jars-deliver.md diff --git a/.changeset/gentle-beans-sip.md b/.changeset/gentle-beans-sip.md new file mode 100644 index 000000000..17f845ee4 --- /dev/null +++ b/.changeset/gentle-beans-sip.md @@ -0,0 +1,5 @@ +--- +'myst-to-jats': patch +--- + +Improve SI units for JATS export diff --git a/.changeset/silly-jars-deliver.md b/.changeset/silly-jars-deliver.md new file mode 100644 index 000000000..6e1d81bfd --- /dev/null +++ b/.changeset/silly-jars-deliver.md @@ -0,0 +1,5 @@ +--- +'tex-to-myst': patch +--- + +Updates to various latex parsing functions diff --git a/packages/myst-to-jats/src/index.ts b/packages/myst-to-jats/src/index.ts index 2a9724bae..af98212e2 100644 --- a/packages/myst-to-jats/src/index.ts +++ b/packages/myst-to-jats/src/index.ts @@ -438,7 +438,7 @@ const handlers: Record = { si(node, state) { // 5 mm state.openNode('named-content', { 'content-type': 'quantity' }); - state.text(`${node.number} `); + if (node.number != null) state.text(`${node.number} `); state.openNode('abbrev', { 'content-type': 'unit', alt: node.alt }); state.text(node.unit); state.closeNode(); diff --git a/packages/tex-to-myst/src/misc.ts b/packages/tex-to-myst/src/misc.ts index c7d5d53d7..6749ea090 100644 --- a/packages/tex-to-myst/src/misc.ts +++ b/packages/tex-to-myst/src/misc.ts @@ -61,6 +61,7 @@ export const MISC_HANDLERS: Record = { // Some size options, not respecting at the moment macro_vspace: pass, macro_hfill: pass, + macro_tiny: pass, macro_small: pass, macro_footnotesize: pass, macro_normalsize: pass, @@ -75,4 +76,7 @@ export const MISC_HANDLERS: Record = { macro_textwidth: pass, macro_onecolumn: pass, macro_linewidth: pass, + // line numbers + macro_linenumbers: pass, + macro_nolinenumbers: pass, }; diff --git a/packages/tex-to-myst/src/siunitx.ts b/packages/tex-to-myst/src/siunitx.ts index 4016b387a..0a42df110 100644 --- a/packages/tex-to-myst/src/siunitx.ts +++ b/packages/tex-to-myst/src/siunitx.ts @@ -17,7 +17,7 @@ const UNITS: Record = { mole: 'mol', second: 's', becquerel: 'Bq', - degreeCelsius: '°C', + degreeCelsius: '℃', coulomb: 'C', farad: 'F', gray: 'Gy', @@ -54,6 +54,7 @@ const UNITS: Record = { arcsecond: '″', // second (plane angle) U+2033 neper: 'Np', tonne: 't', + celsius: '℃', }; // SI prefixes @@ -327,6 +328,18 @@ const SIUNITX_HANDLERS: Record = { }); return; }, + macro_si(node, state) { + state.openParagraph(); + const { units, alt, organized } = createSiUnitNode(state.file, node, 0); + const translated = organized.map(unitToString).join(NARROW_NO_BREAK_SPACE); + state.addLeaf('si', { + unit: translated, + alt, + units: units.map((n: GenericNode) => n.content), + value: translated, + }); + return; + }, macro_qty(node, state) { state.openParagraph(); const { units, alt, organized } = createSiUnitNode(state.file, node, 1); diff --git a/packages/tex-to-myst/src/tables.ts b/packages/tex-to-myst/src/tables.ts index 55e4c89b5..fd13a6772 100644 --- a/packages/tex-to-myst/src/tables.ts +++ b/packages/tex-to-myst/src/tables.ts @@ -59,6 +59,13 @@ export const TABLE_HANDLERS: Record = { state.closeParagraph(); state.closeNode(); }, + ['env_table*'](node, state) { + state.closeParagraph(); + state.openNode('container', { kind: 'table' }); + state.renderChildren(node); + state.closeParagraph(); + state.closeNode(); + }, env_tabular: createTable, env_tabularx: createTable, env_supertabular: createTable, diff --git a/packages/tex-to-myst/src/tex.ts b/packages/tex-to-myst/src/tex.ts index 4225ade4b..914042abe 100644 --- a/packages/tex-to-myst/src/tex.ts +++ b/packages/tex-to-myst/src/tex.ts @@ -72,6 +72,7 @@ const macros: Record = { affiliation: 1, framebox: 1, tnote: 1, + ['table*']: 1, arraystretch: 1, multirow: 5, multicolumn: 3, @@ -83,6 +84,7 @@ const macros: Record = { captionof: 2, // SI Units: https://texdoc.org/serve/siunitx/0 SI: 2, + si: 1, qty: 2, tothe: 1, raiseto: 1, diff --git a/packages/tex-to-myst/tests/siunitx.yml b/packages/tex-to-myst/tests/siunitx.yml index 830b507d3..8689b1c82 100644 --- a/packages/tex-to-myst/tests/siunitx.yml +++ b/packages/tex-to-myst/tests/siunitx.yml @@ -87,3 +87,6 @@ cases: - title: kg⁻¹ tex: \qty[per-mode = symbol]{1.99}{ \per \kilogram } text: 1.99 kg⁻¹ + - title: 90 ms + tex: 90~\si{\milli \second} + text: 90 ms