Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preamble option for KaTex #2784

Closed
Edenharder opened this issue Sep 7, 2021 · 9 comments
Closed

Preamble option for KaTex #2784

Edenharder opened this issue Sep 7, 2021 · 9 comments
Labels
help-wanted Need help from people with specific environment or konwledge :type/feature-request Closed at will. Feature requests are in Logseq forum https://discuss.logseq.com/

Comments

@Edenharder
Copy link

I would be nice if we can define extra commands for KaTex, then we do not need to type the complicated expression each time. It is indeed possible in KaTex, for example this issue. There is also a similar plugin for Obsidian.

@MinusGix
Copy link

MinusGix commented Oct 22, 2021

I implemented a basic version (on local copy of Logseq) which just allows the user to set the macros field for katex.render:

(defn render!
  [state]
  (let [[id s display?] (:rum/args state)]
    (let [macros (clj->js (get (state/get-config) :katex-macros {}))]
      (try
        (js/katex.render s (gdom/getElement id)
                         #js {:displayMode display?
                              :throwOnError false
                              :strict false
                              :macros macros})
        (catch js/Error e
          (js/console.error e))))))

but this requires the usage to be like (in their config.edn:

:katex-macros { 
    "\\if" "\\ \\text{if}\\ "
}

but that is somewhat unpleasant.
One issue with this approach is that simply setting the macros field to an object (even an empty object) makes so it is modified whenever Katex renders \gdef\somecommand{2 + 2} (see: https://katex.org/docs/options.html). This would actually be nice by itself but it persists across different pages, and all pages aren't rendered at the same time so you can run into situations where a macro is \gdef'd in a file you haven't viewed yet and so it doesn't render in another file. I don't think this is too major, but it could be a source of confusion.

The option that is closer to the issue linked is to have some special file (ex: katex-macros.tex) that is read and has the contents inserted at the top of the 'root' page (not sure what the terminology is in this project, but it could probably be 'displayed' once by putting it outside the main editing area). This would require globalGroup: true, but I think that could be potentially be avoided in-general by only using globalGroup on that render specifically.

@llcc llcc added :type/feature-request Closed at will. Feature requests are in Logseq forum https://discuss.logseq.com/ latex help-wanted Need help from people with specific environment or konwledge labels Oct 28, 2021
@tiensonqin tiensonqin removed the latex label Nov 3, 2021
@MinusGix
Copy link

I'm curious as to why this was closed? It doesn't appear to have been added or an alternative created, unless I'm missing something.

@cnut1648
Copy link

cnut1648 commented Feb 3, 2022

Yeah I am also pretty interested in this feature, and I wonder if there is a more elegant solution.

@Drekin
Copy link

Drekin commented Mar 11, 2022

+1 for such feature. It would be much nice to write e.g. \rng(f ∘ g) instead of \operatorname{rng}(f \circ g).

@kalaspa
Copy link

kalaspa commented Mar 29, 2022

You can add Katex macros directly in your logseq/custom.js, with the following code:

function whenAvailable(name, callback) {
    var interval = 10; // ms
    window.setTimeout(function() {
        if (window[name]) {
            callback(window[name]);
        } else {
            whenAvailable(name, callback);
        }
    }, interval);
}


whenAvailable("katex", function(t) {
    // Put your macros below, key will be replaced by the corresponding macro
    katex.__defineMacro(key, macro);
});

@cnut1648
Copy link

Thanks @kalaspa ! That works. Just posting some examples of how to use it

whenAvailable("katex", function(t) {
    // Put your macros below, key will be replaced by the corresponding macro
    katex.__defineMacro("\\E", "\\mathbb{E}");
    katex.__defineMacro("\\pdv", "\\dfrac{\\partial #1}{\\partial #2}");
    katex.__defineMacro("\\dv", "\\dfrac{d #1}{d #2}");
});

@Drekin
Copy link

Drekin commented Apr 2, 2022

Thanks! This is much cleaner that hacking directly into katex.min.js.

@honzaprevratil
Copy link

@cnut1648 Hi, I tried the putting your snippet combined with the function whenAvailable posted by @kalaspa but it's not working for me. Do you have any ideas, what to do to fix it?

@cnut1648
Copy link

Hi @honzaprevratil I still use the same code and the macro works for me. When I open the logseq sometimes there is a popup asking if I allow logseq to run custom.js. Only when I click yes will logseq use the macro. The only issue is that I do not know how to trigger the popup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help-wanted Need help from people with specific environment or konwledge :type/feature-request Closed at will. Feature requests are in Logseq forum https://discuss.logseq.com/
Projects
None yet
Development

No branches or pull requests

8 participants