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
}
5 Likes

Recently updated and it looks like it broke due to using “+” to concat strings rather than “..”.

Yeah, that’s expected. The Space Lua is (much more) aligned with the standard Lua 5.4 behaviour now.

Here’s my version with a really terrible trick, based on the code by @MrMugame:

Space Lua
local module = js.import("https://cdn.jsdelivr.net/npm/[email protected]/+esm")

local function render(expr, display)
  return widget.new({
    html = module.renderToString(expr, {
      displayMode = display,
      trust = true,
      throwOnError = false
    })
  })
end

katex = {}
setmetatable(katex, {
  __call = function (_, expr)
    return render(expr, false)
  end,
  __mul = function (_, expr)
    return render(expr, true)
  end
})

slashCommand.define({
  name = "katex",
  description = "Insert inline LaTeX equation",
  run = function()
    editor.insertAtCursor("${katex[[|^|]]}", false, true)
  end
})
slashCommand.define({
  name = "katex*",
  description = "Insert LaTeX equation in display style",
  run = function()
    editor.insertAtCursor("${katex*[[|^|]]}", false, true)
  end
})
Space Style
@import url("https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css");

.sb-lua-directive-inline:has(.katex) {
  border: none !important;
}

Also note that it is technically possible to store everything locally.
To do this you have to replace the urls with their corresponding base 64 version, i.e. data:text/css;base64,... and data:text/javascript;base64,....

The only remaining issue is fonts. I haven’t tried this, but if you don’t like them missing, you could modify the @font-face definitions within katex.css to replace every url(...) with e.g. url("data:font/woff2;base64,...").

Nasty… but I guess it could work.