+
+Macro |
+Command |
+Preview |
+ |
+
+
+<$list filter="[all[shadows+tiddlers]tag[$:/tags/KaTeX/Macro]!has[draft.of]sort[caption]]">
+
+<$text text={{!!caption}}/> |
+<><> |
+<> |
+
+<>
+<>
+<$button class="tc-btn-invisible tc-btn-dropdown">
+<$action-deletetiddler $tiddler={{!!title}}/>
+{{$:/core/images/delete-button}}
+$button>
+ |
+
+$list>
+
+
+Add a new macro |
+
+
+<$edit-text tiddler="$:/temp/katex/new-macro-name" tag="input" default=""/> |
+<$edit-text tiddler="$:/temp/katex/new-macro-command" tag="input" default=""/> |
+<> |
+<$button actions=<>>
+{{$:/language/EditTemplate/Fields/Add/Button}}
+$button> |
+
+
+
+
+Import
+<$edit-text tiddler="$:/temp/katex/import-macro" tag="textarea" default="" class="tc-edit-texteditor" placeholder="You can type commands like \def\ZZ{\mathbb{Z}} and import them automatically."/>
+
+<$vars macros={{$:/temp/katex/import-macro}} sep="%.*\n|\n+" re="^\\g?def([^{]*){(.*)}.*">
+
+<$button>
+<$list filter="[splitregexpregexp]" variable=line>
+<$vars m={{{[search-replace:g:regexp,[$1]]}}} c={{{[search-replace:g:regexp,[$2]]}}}>
+<$action-createtiddler $basetitle={{{[addprefix[$:/plugins/tiddlywiki/katex/macros/]]}}} tags="$:/tags/KaTeX/Macro" type="text/plain" caption=<> text=<> $overwrite=yes/>
+$vars>
+$list>
+Import
+$button>
+<$button>
+<$action-setfield $tiddler="$:/temp/katex/import-macro" text=""/>
+Clear
+$button>
+
+''Preview''
+
+<$list filter="[splitregexpregexp]" variable=line>
+<$vars m={{{[search-replace:g:regexp,[$1]]}}} c={{{[search-replace:g:regexp,[$2]]}}}>
+
+<> |
+<> |
+<$macrocall $name=katex-escape text="<>"/> |
+$vars>
+$list>
+
+
+$vars>
+
+ |
+
+
+
+
+Usage
+
+-
+You can add entries like `\ZZ`, `\mathbb{Z}`, which will render as <$latex text="\mathbb{Z}"/>.
+
+-
+An entry mapping `\dd#1#2` to `\frac{d#1}{d#2}` will create a macro with two arguments; `\dd{f}{x}` will then render as <$latex text="\frac{df}{dx}"/>.
+
+-
+Note that the macros defined here have higher priority than those defined using `\gdef`. Also, deleted macros will continue to exist until a full refresh.
+
+
+
+ |
+
+
diff --git a/plugins/tiddlywiki/katex/wrapper.js b/plugins/tiddlywiki/katex/wrapper.js
index 983392802..ceda68c84 100644
--- a/plugins/tiddlywiki/katex/wrapper.js
+++ b/plugins/tiddlywiki/katex/wrapper.js
@@ -16,6 +16,23 @@ var katex = require("$:/plugins/tiddlywiki/katex/katex.min.js"),
chemParse = require("$:/plugins/tiddlywiki/katex/mhchem.min.js"),
Widget = require("$:/core/modules/widgets/widget.js").widget;
+katex.macros = {};
+katex.updateMacros = function() {
+ var tiddlers = $tw.wiki.getTiddlersWithTag("$:/tags/KaTeX/Macro"),
+ regex = /#\d/g, // Remove the arguments like #1#2
+ tid, macro, cmd;
+ for (var i=0; i < tiddlers.length; i++) {
+ tid = $tw.wiki.getTiddler(tiddlers[i]);
+ try {
+ macro = tid.fields["caption"];
+ macro = macro.replace(regex, "");
+ cmd = tid.fields["text"];
+ katex.macros[macro] = cmd;
+ } catch(ex) {// Catch the bad ones
+ };
+ };
+};
+
var KaTeXWidget = function(parseTreeNode,options) {
this.initialise(parseTreeNode,options);
};
@@ -36,9 +53,10 @@ KaTeXWidget.prototype.render = function(parent,nextSibling) {
// Get the source text
var text = this.getAttribute("text",this.parseTreeNode.text || "");
var displayMode = this.getAttribute("displayMode",this.parseTreeNode.displayMode || "false") === "true";
+ katex.updateMacros();
// Render it into a span
var span = this.document.createElement("span"),
- options = {throwOnError: false, displayMode: displayMode};
+ options = {throwOnError: false, displayMode: displayMode, macros: katex.macros};
try {
if(!this.document.isTiddlyWikiFakeDom) {
katex.render(text,span,options);