Skip to content

Commit

Permalink
Fix units latex format
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x committed Apr 13, 2024
1 parent 999d304 commit cd55ce3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
8 changes: 4 additions & 4 deletions fluidProperties.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {number, unit} from 'mathjs'
import { number, unit } from 'mathjs'

// List of units from coolprop
const propUnit = {
Expand Down Expand Up @@ -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])]
Expand Down
19 changes: 17 additions & 2 deletions makeDoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
})
Expand Down Expand Up @@ -97,10 +97,25 @@ const formatResult = math.typed({
'Help': result => `<pre>${math.format(result)}</pre>`,
'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.
Expand Down
7 changes: 6 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

0 comments on commit cd55ce3

Please sign in to comment.