Tiddlywiki5: KaTeX global macros do not work as expected

Created on 15 Nov 2019  路  4Comments  路  Source: Jermolene/TiddlyWiki5

There seem to be a couple of distinct but possibly related issues with defining macros that persist across math expressions with \gdef or \global\def using the KaTeX plugin (I tested mostly with \gdef).

Issue 1:

\gdef only seems to work at all if the macros list is present in the options being passed to katex.render.

Steps to reproduce

If this option is not present in the options list in $:/plugins/tiddlywiki/katex/wrapper.js, \gdef just seems to behave like \def, i.e. if we have a tiddler containing

$$
\gdef\RP{\R\mathrm P}
\RP
$$

$$
\RP
$$

only the first instance of \RP would render correctly and the second one would show as the red error text.

Workaround

If we add macros: {} to the end of optionsin $:/plugins/tiddlywiki/katex/wrapper.js, it works, at least within the same tiddler. See Issue 2.

Issue 2:

Macros defined with \gdef (using the workaround in Issue 1) only work with tiddlers that are in the story river at the same time.

Steps to reproduce

Say we have Tiddler1 containing

$$\gdef\RP{\R\mathrm P}$$

and Tiddler2 containing

$$\RP$$

then, if we reload the page, Tiddler2 will only show the red error text.

Workaround(s)

Tiddler2 will render correctly if we also open Tiddler1, click the edit button of Tiddler2 and then click the done button.

A workaround for this is to transclude Tiddler1 at the start of Tiddler2, but doing this for every Tiddler seems less than ideal.

Conclusion

Having a mechanism to define macros globally would be extremely useful.

A first feature request would be to make it possible to define a macro list to be passed to katex.render without having to edit $:/plugins/tiddlywiki/katex/wrapper.js. An issue with this is that I don't think it's possible to define macros that take arguments in this way.

Secondly, it would be great if one could simply create a Tiddler containing all macros that one wants to use across the Wiki defined with \gdef and use them in all other Tiddlers without having to transclude.

Lastly, I apologize if these should be separate issues, but they seem connected. It also seems possible that at least Issue 1 is an issue with KaTeX itself and not TiddlyWiki.

Most helpful comment

Hi @KendrickLamarck thanks for the clear report. I think both issues are limitations of the integration with TiddlyWiki, rather than bugs with KaTeX.

It should indeed be possible to extend things such that tiddlers tagged (say) $:/tags/{K|L}aTeX/Global could be used to define global LaTeX macros available throughout the wiki. We might have to use a TiddlyWiki-like \define syntax to mark up the macros.

@BurningTreeC, one of our core contributors, has been working on KaTeX recently -- Simon is this something you might be able to pick up?

All 4 comments

Hi @KendrickLamarck thanks for the clear report. I think both issues are limitations of the integration with TiddlyWiki, rather than bugs with KaTeX.

It should indeed be possible to extend things such that tiddlers tagged (say) $:/tags/{K|L}aTeX/Global could be used to define global LaTeX macros available throughout the wiki. We might have to use a TiddlyWiki-like \define syntax to mark up the macros.

@BurningTreeC, one of our core contributors, has been working on KaTeX recently -- Simon is this something you might be able to pick up?

Hi! Such a feature would also be very desirable for me :) In the meanwhile, the fix for Issue 1 only seems to work for me when I add katex.macros={}; above the line

var KaTeXWidget = function(parseTreeNode,options) {
    this.initialise(parseTreeNode,options);

in $:/plugins/tiddlywiki/katex/wrapper.js and add macros:katex.macros to the options.

As an additional workaround for Issue 2 I write my KaTeX definitions into a tiddler that I tag with $:/tags/AboveStory - In that way the global definitons always get processed.

@KendrickLamarck @southwester @Jermolene you all might have more knowledge about how to implement this but I'd also like to see it realized so with a little bit of help I can give it a shot. There are many things though I still have to figure out first how they're done like creating a \define - like syntax for defining katex macros in tiddlers tagged $:/tags/Katex/Global

Thank you @BurningTreeC ! Here are my thoughts:

The $:/tags/AboveStory solution already works great for my needs. I would be satisfied with something like that with a less "workaroundy" feel to it. So have tiddlers tagged $:/tags/KaTeX/Global (or $:/tags/KaTeX/Macros?) that contain macro definitions:

  • They should probably contain just the code without the $$, so it doesn't just render empty
  • Using a new \define syntax instead of the present \def, \gdef, \newcommand and so on could have benefits and drawbacks:

    • It's consistent with wikitext macro definitions.

    • It won't work the same as wikitext macros, for example LaTeX macros only allow up to 9 arguments.

    • It already looks a bit like LaTeX, which could be either nice or confusing.

We could also support the other way of defining macros, by passing them as a list to katex.render as described here. Maybe use the same tag, but use Data- or JSONTiddlers and just distinguish by type?

Finally, In the docs I just linked it actually says

This object will be modified if the LaTeX code defines its own macros via \gdef

so maybe this is the way to implement it? Could be completely wrong. Again, I don't know how or if it's possible to define Macros that take arguments using this method.

Was this page helpful?
0 / 5 - 0 ratings