TiddlyWiki / TiddlyWiki5

A self-contained JavaScript wiki for the browser, Node.js, AWS Lambda etc.
https://tiddlywiki.com/
Other
8.07k stars 1.19k forks source link

[IDEA] Single dollar delimiter for KaTeX plugin #5932

Open 8d1h opened 3 years ago

8d1h commented 3 years ago

Is your feature request related to a problem? Please describe. Inline math in the KaTeX plugin uses two dollar signs as delimiter. This is not very convenient to type and makes it difficult to copy from / to other Markdown / LaTeX files. I think for people who do need to type math equations, this is something worth fixing (there is this discussion).

Describe the solution you'd like I modified the KaTeX parser to parse single dollar as delimiter for inline math (and \$ if you want a dollar sign). I kept the two dollars syntax so existing tiddlers won't mess up. And dollar signs in templates all seem to work fine (maybe need some more testing).

It's a bit hacky but could be useful for some :) We could make some interface so that people can configure it by themselves (also, an interface for custom KaTeX macros would be nice). If no one is working on this now, maybe I can look into it :)

dollar

code Copy the following code to `$:/plugins/tiddlywiki/katex/latex-parser.js` ````javascript /*\ title: $:/plugins/tiddlywiki/katex/latex-parser.js type: application/javascript module-type: wikirule Wiki text inline rule for LaTeX. For example: ``` $$latex-goes-here$$ ``` This wikiparser can be modified using the rules eg: ``` \rules except latex-parser \rules only latex-parser ``` \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; exports.name = "latex-parser"; exports.types = {inline: true}; exports.init = function(parser) { this.parser = parser; // Regexp to match this.matchRegExp = /(\\\$)|(?
pmario commented 3 years ago

Hi, Have a close look at: #5907 and the related GG thread. ...

(also, an interface for custom KaTeX macros would be nice). If no one is working on this now, maybe I can look into it :)

I think the latest KaTeX library is needed here. (but I'm not sure, since I don't use KaTeX)


The main problem I see with your "hack" is that it will force users to escape the default $ with \$. If they did use a dollar sign in "default" tiddlers, the plugin will cause severe problems in existing content without a warning. ... There is a reason, why the default is $$.

So by installing the plugin, there should be a check or a warning somewhere.

It should be possible to include a special "wikirule" as you did, that is disabled by default. So the user has to actively "opt in". After the "opt in" the wiki has to be saved and reloaded! So the docs would need to mention that.


For testing you may try content which reference TW system tiddlers eg: $:/ControlPanel and others

8d1h commented 3 years ago

Thanks!

I think the latest KaTeX library is needed here. (but I'm not sure, since I don't use KaTeX)

In fact I think this is independent. Although it would always be nice to update to the latest version.

The main problem I see with your "hack" is that it will force users to escape the default $ with \$. If they did use a dollar sign in "default" tiddlers, the plugin will cause severe problems in existing content without a warning. ... There is a reason, why the default is $$.

So by installing the plugin, there should be a check or a warning somewhere.

Yes that's why I said that this is a hack. To make it official into the plugin we need such an interface for user to choose.

I played around a bit and put up together a config tab for the plugin.

katex

It also has an editor for macros as requested in https://github.com/Jermolene/TiddlyWiki5/issues/4375 (note that macros with arguments can also be added in the same way)

pmario commented 3 years ago

The TW parser will allow subclassing with v5.2.0.

@Jermolene should we create a new wikitext type with this plugin eg: text/vnd.tiddlywiki+katex that will automatically add this rule to the parser, so other tiddlers are not affected by the new rules.

This would mean, that the TW core needs to understand, that it should fall back to text/vnd.tiddlywiki if it sees a text/vnd.tiddlywiki+something and the +something parser is missing.

@8d1h ... Is it possible to implement the KaTeX macro definition into the core plugin and the $ handling into a second may be 3rd party plugin?

8d1h commented 3 years ago

The TW parser will allow subclassing with v5.2.0.

That looks interesting!

@8d1h ... Is it possible to implement the KaTeX macro definition into the core plugin and the $ handling into a second may be 3rd party plugin?

Sure I can submit a PR for the macro editor. The $ parsing does need some more considerations, but I hope it can be implemented into the official plugin (for 3rd party plugins there is already the MathJax one, which also allows parsing \(math\) and other custom delimiters.

The PR for the macro editor is https://github.com/Jermolene/TiddlyWiki5/pull/5933