Public
Edited
Mar 6, 2023
1 fork
1 star
Insert cell
Insert cell
math = (await import('https://cdn.skypack.dev/mathjs'))
Insert cell
tex`${math.parse('(b <= -8.4e-48) ? (a * ((((2.0 * (a * (c / b))) - b) - b) / 2.0)) : ((b <= 2.3e-107) ? (a * ((sqrt(c * (a * -4.0)) - b) / 2.0)) : (a / ((1.0 / b) - ((b / a) / c))))').toTex({handler: branchConditionalHandler})}` // would like to deparenthesize!
Insert cell
tex`${math.parse('(b <= -8.4e-48) ? hypot(x, 1) : ((b <= 2.3e-107) ? (a * ((log1p(c * (a * -4.0)) - asinh(b)) / 2.0)) : (a / ((1.0 / b) - ((b / a) / c))))').toTex({handler: branchConditionalHandler})}`
Insert cell
function branchConditionalHandler(node, options) {
options = {handler: cleanupTex}
if (node.type !== 'ConditionalNode') {
return node.toTex(options)
}
const deparen = node => node.type === 'ParenthesisNode' ? node.content : node
const conditions = [node]
let curr = node
while (deparen(curr.falseExpr).type === 'ConditionalNode') {
conditions.push(deparen(curr.falseExpr))
curr = deparen(curr.falseExpr)
}
conditions.push(conditions[conditions.length - 1]) // duplicate the final condition
const deparenCondition = c => ({...c, condition: deparen(c.condition), trueExpr: deparen(c.trueExpr), falseExpr: deparen(c.falseExpr)})
return conditions.map(deparenCondition).map((c, i) =>
i === 0 ? `\\mathbf{if} \\> ${c.condition.toTex(options)}: \\\\ \\quad ${c.trueExpr.toTex(options)}`
: i !== conditions.length - 1 ? `\\mathbf{elif} \\> ${c.condition.toTex(options)}: \\\\ \\quad ${c.trueExpr.toTex(options)}`
: `\\mathbf{else :} \\\\ \\quad ${c.falseExpr.toTex(options)}`).join('\\\\')
}
Insert cell
tex`${math.parse('hypot(x, 1)').toTex({handler: cleanupTex})}`
Insert cell
tex`${math.parse('sqrt(x + 1) - hypot(x)').toTex({handler: cleanupTex})}`
Insert cell
math.parse('(a/c)')
Insert cell
tex`\mathbf{hypot}`
Insert cell
function cleanupTex(node, options) {
return node.fn?.name == 'hypot' ? `\\mathbf{hypot}(${node.args[0].toTex(options)}, ${node.args[1].toTex(options)})`
: node.fn?.name == 'log1p' ? `\\mathbf{log1p}(${node.args[0].toTex(options)})`
//: node.isSymbolNode || node.isConstantNode ? node.toTex()
: node._toTex(options)
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more