From cd55ce3164b25f9a3c9d1626f731fc4c1f717e48 Mon Sep 17 00:00:00 2001 From: David Contreras Date: Fri, 12 Apr 2024 22:28:01 -0600 Subject: [PATCH] Fix units latex format --- fluidProperties.js | 8 ++++---- makeDoc.js | 19 +++++++++++++++++-- todo.md | 7 ++++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/fluidProperties.js b/fluidProperties.js index 0dc8fdb..eb90dc9 100644 --- a/fluidProperties.js +++ b/fluidProperties.js @@ -1,4 +1,4 @@ -import {number, unit} from 'mathjs' +import { number, unit } from 'mathjs' // List of units from coolprop const propUnit = { @@ -262,9 +262,9 @@ const toUnit = (v, u) => u ? unit(v, u) : unit(v) export function props(desiredProperty, fluidName, fluidProperties) { const calcPropUnit = calcPropUnits(desiredProperty) - let prop = Object.keys(fluidProperties).slice(0,2) - let value = Object.values(fluidProperties).slice(0,2) - + let prop = Object.keys(fluidProperties).slice(0, 2) + let value = Object.values(fluidProperties).slice(0, 2) + if (prop.length > 0) { const units = [propUnit[subProp(prop[0])], propUnit[subProp(prop[1])]] value = [toValue(value[0], units[0]), toValue(value[1], units[1])] diff --git a/makeDoc.js b/makeDoc.js index e774d3f..922377e 100644 --- a/makeDoc.js +++ b/makeDoc.js @@ -42,7 +42,7 @@ function makeDoc(code) { cells[thisCell].to = lineNum } else { - cells.push({ from: lineNum, to:lineNum, cell_type: lineTypes[lineNum], source: [line] }) + cells.push({ from: lineNum, to: lineNum, cell_type: lineTypes[lineNum], source: [line] }) } lastType = lineTypes[lineNum] }) @@ -97,10 +97,25 @@ const formatResult = math.typed({ 'Help': result => `
${math.format(result)}
`, 'any': math.typed.referTo( 'number', - fnumber => result => katex.renderToString(math.parse(fnumber(result)).toTex()) + fnumber => result => { + if (result.isUnit) { + let rString = result.toString() + if (rString.includes('/')) { + let rJSON = result.toJSON() + return katex.renderToString( + `${parseToTex(fnumber(rJSON.value).toString())}\ ${parseToTex(rJSON.unit)}`) + } + } + return katex.renderToString(parseToTex(fnumber(result))) + } ) }) + +function parseToTex(expression) { + return math.parse(expression).toTex() +} + /** * Processes an array of expressions by evaluating them, formatting the results, * and determining their visibility. diff --git a/todo.md b/todo.md index 547a641..1edf8c0 100644 --- a/todo.md +++ b/todo.md @@ -1,6 +1,6 @@ # To-Do -- [x] Scroll outputs into view and highlight them +- [ ] Plots - [ ] Migrate the language definition to CM6 - [ ] Dynamic autocomplete - [ ] More efficient math evaluation (only update state when inputs change) @@ -9,8 +9,13 @@ # Maybe - [ ] Saves state - [ ] Webworkers +- [ ] Share URL +- [ ] Load doc +- [ ] Open doc (drag and drop) # Done [x] +- [x] Scroll outputs into view and highlight them +- [x] Fix nits "J/kg" - [x] Migrate to codemirror 6 with a build tool like Vite - [x] Not only text outputs (allow for latex) - [x] Use Alpinejs to reduce code