Ability to include KATEX packages such as katex-physics

Hi everyone, I have just switched from org-roam to logseq in the past couple of days and I am very happy about it.
The one thing I miss is the ability to load latex packages in the preamble. I understand that logseq uses Katex, which is different from classic org-mode.

Recently, someone asked to include the package mhchem in this post and it was included (apparently) straightforwardly in this pull request.

As for myself, I would have liked to include the package katex-physics in all of my notes. But I guess everyone has his/her favorite package.

Is it easy possible to give the user the possibility to include his/her favorite package ? Or could someone include katex-physics by default for me ?

Thank you in advance

I made the mhchem package request and never even realized it was integrated.
So thanks :slight_smile:

1 Like

Well, for anyone who NEEDS katex-physics or any other package, I have a dirty workaround. I insist, it is very dirty, but at least it kind of works.

  1. First, find the logseq installation repository. On my linux machine, it is located at /opt/logseq-desktop
  2. Copy your package in folder resources/app/js. You should find katex.min.js and mhchem.min.js already there
  3. For the specific case of katex-physics, I need to register each macro to katex at the end of the file. I simply add
for (var key in macros_physics) {
    katex.__defineMacro(key, macros_physics[key]);
}
  1. You need to add this script at the end of the <header> section. My dirty workaround is to add this piece of code at the end of mhchem.min.js, since it is the last piece of javascript that is executed (to my knowledge) and that I don’t want to recompile logseq’s source code
function includeJs(jsFilePath) {
    var js = document.createElement("script");

    js.type = "text/javascript";
    js.src = jsFilePath;
    js.charset = "UTF-8"

    document.head.appendChild(js);
}

includeJs("./js/katex-physics.js");
  1. Relaunch logseq. If I have a formula using katex-physics on my first page, it does not render well at first. I just re-render it once.

I am sharing this for impatients (like me), but I am genuinely asking for better/simpler/cleaner ways to do the same thing :slight_smile:

1 Like

It is probably just my ignorance, but I can’t seem to find a file katex-physics.js to download. How do I produce such a file from the repository named katex-physics here on GitHub? Please excuse my dumb question, I just started with logseq and katex as an absolute novice.

Hi, in principle one needs to compile the Github repository named katex-physics. The documentation is lacking but you can infer the procedure from the .travis.yml file.

But you can also directly use the version on npm. To do so install it with npm i katex-physics. The file you are interested in is node_modules/katex-physics/dist/main.js. I renamed this file katex-physics out of convenience. Hope this helps :slight_smile:. In my opinion, the use of katex is really a big limitation of logseq and I personally need these dirty tricks to be able to use it…

1 Like

Thanks for the explanation, that helped a lot.
Would be great if logseq would offer MathJax as well, so that the user could configure what they prefer.

1 Like

KaTeX-physics is now provided in Logseq by the Extensions plus plugin, by xyhp915