Mathematical expressions

Update: the link is now https://wentaoli.xyz/second_quantization.

1 Like

I was using @MrMugame 's script a lot. Recently updated and it looks like it broke due to using ā€œ+ā€ to concat strings rather than ā€œ..ā€. I provided my updated code below for anyone else who may have broken functions now.

latex = {
  header = [[<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css">]],
  katex = js.import("https://cdn.jsdelivr.net/npm/[email protected]/+esm")
}

function latex.inline(expression)  
  local html = latex.katex.renderToString(expression, {
    trust = true,
    throwOnError = false,
    displayMode = false
  })
  
  return widget.new {
    html = "<span>" .. latex.header .. html .. "</span>"
  }
end

function latex.block(expression)
  local html = latex.katex.renderToString(expression, {
    trust = true,
    throwOnError = false,
    displayMode = true
  })
  
  return widget.new {
    html = "<span>" .. latex.header .. html .. "</span>"
  }
end 

slashcommand.define {
  name = "math",
  run = function()
    editor.insertAtCursor("${latex.inline[[]]}", false, true)
    editor.moveCursor(editor.getCursor() - 3)
  end
}

slashcommand.define {
  name = "equation",
  run = function()
    editor.insertAtCursor("${latex.block[[]]}", false, true)
    editor.moveCursor(editor.getCursor() - 3)
  end
}
2 Likes