Skip to content

Commit

Permalink
📜 Improvements to LaTeX parsing (#672)
Browse files Browse the repository at this point in the history
* 📜 Improvements to LaTeX parsing
* 🏷 SI units in JATS
  • Loading branch information
rowanc1 authored Oct 13, 2023
1 parent 0b090ca commit 6fa758f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-beans-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-jats': patch
---

Improve SI units for JATS export
5 changes: 5 additions & 0 deletions .changeset/silly-jars-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tex-to-myst': patch
---

Updates to various latex parsing functions
2 changes: 1 addition & 1 deletion packages/myst-to-jats/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ const handlers: Record<string, Handler> = {
si(node, state) {
// <named-content content-type="quantity">5 <abbrev content-type="unit" alt="milli meter">mm</abbrev></named-content>
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();
Expand Down
4 changes: 4 additions & 0 deletions packages/tex-to-myst/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const MISC_HANDLERS: Record<string, Handler> = {
// 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,
Expand All @@ -75,4 +76,7 @@ export const MISC_HANDLERS: Record<string, Handler> = {
macro_textwidth: pass,
macro_onecolumn: pass,
macro_linewidth: pass,
// line numbers
macro_linenumbers: pass,
macro_nolinenumbers: pass,
};
15 changes: 14 additions & 1 deletion packages/tex-to-myst/src/siunitx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const UNITS: Record<string, string> = {
mole: 'mol',
second: 's',
becquerel: 'Bq',
degreeCelsius: '°C',
degreeCelsius: '',
coulomb: 'C',
farad: 'F',
gray: 'Gy',
Expand Down Expand Up @@ -54,6 +54,7 @@ const UNITS: Record<string, string> = {
arcsecond: '″', // second (plane angle) U+2033
neper: 'Np',
tonne: 't',
celsius: '℃',
};

// SI prefixes
Expand Down Expand Up @@ -327,6 +328,18 @@ const SIUNITX_HANDLERS: Record<string, Handler> = {
});
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<SiUnit>('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);
Expand Down
7 changes: 7 additions & 0 deletions packages/tex-to-myst/src/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export const TABLE_HANDLERS: Record<string, Handler> = {
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,
Expand Down
2 changes: 2 additions & 0 deletions packages/tex-to-myst/src/tex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const macros: Record<string, number> = {
affiliation: 1,
framebox: 1,
tnote: 1,
['table*']: 1,
arraystretch: 1,
multirow: 5,
multicolumn: 3,
Expand All @@ -83,6 +84,7 @@ const macros: Record<string, number> = {
captionof: 2,
// SI Units: https://texdoc.org/serve/siunitx/0
SI: 2,
si: 1,
qty: 2,
tothe: 1,
raiseto: 1,
Expand Down
3 changes: 3 additions & 0 deletions packages/tex-to-myst/tests/siunitx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6fa758f

Please sign in to comment.