diff --git a/plugins/tiddlywiki/katex/config.tid b/plugins/tiddlywiki/katex/config.tid new file mode 100644 index 000000000..9e48c1fd8 --- /dev/null +++ b/plugins/tiddlywiki/katex/config.tid @@ -0,0 +1,17 @@ +title: $:/plugins/tiddlywiki/katex/config + +
+ +<$list filter="[all[shadows+tiddlers]tag[$:/tags/KaTeX/Config]!has[draft.of]]"> + +
+ +!! <$link><$transclude field="caption"/> + +<$transclude> + +
+ + + +
diff --git a/plugins/tiddlywiki/katex/plugin.info b/plugins/tiddlywiki/katex/plugin.info index 830abf934..a2b0f37c7 100644 --- a/plugins/tiddlywiki/katex/plugin.info +++ b/plugins/tiddlywiki/katex/plugin.info @@ -2,6 +2,6 @@ "title": "$:/plugins/tiddlywiki/katex", "name": "KaTeX", "description": "KaTeX library for mathematical typography", - "list": "readme usage", + "list": "readme usage config", "library-version": "v0.12.0" } diff --git a/plugins/tiddlywiki/katex/ui/config/macro.tid b/plugins/tiddlywiki/katex/ui/config/macro.tid new file mode 100644 index 000000000..d1737d8d0 --- /dev/null +++ b/plugins/tiddlywiki/katex/ui/config/macro.tid @@ -0,0 +1,137 @@ +title: $:/plugins/tiddlywiki/katex/ui/config/macro +tags: $:/tags/KaTeX/Config +caption: Custom macro editor + +\define katex-escape(text) +<$vars t=$text$> +<$latex text={{{[search-replace:g:regexp[(? + +\end + +\define katex-create-macro() +<$vars loc={{$:/temp/katex/new-macro-name}}> +<$action-createtiddler $basetitle={{{[addprefix[$:/plugins/tiddlywiki/katex/macros/]]}}} tags="$:/tags/KaTeX/Macro" type="text/plain" caption={{$:/temp/katex/new-macro-name}} text={{$:/temp/katex/new-macro-command}} $overwrite=yes/> + +\end + +\define katex-edit-cell() +<$reveal state="$:/temp/katex/edit-macro" type=match text={{!!title}}> +<$edit-text tiddler=<> tag="input" default=""/> + +\end + +\define katex-view-cell() +<$reveal state="$:/temp/katex/edit-macro" type=nomatch text={{!!title}}> +<$view field=text/> + +\end + +\define katex-edit-macro-button() +<$reveal state="$:/temp/katex/edit-macro" type=nomatch text={{!!title}}> +<$button class="tc-btn-invisible tc-btn-dropdown"> +<$action-setfield $tiddler="$:/temp/katex/edit-macro" text={{!!title}}/> +{{$:/core/images/edit-button}} + + +\end + +\define katex-save-macro-button() +<$reveal state="$:/temp/katex/edit-macro" type=match text={{!!title}}> +<$button class="tc-btn-invisible tc-btn-dropdown"> +<$action-deletetiddler $tiddler="$:/temp/katex/edit-macro"/> +{{$:/core/images/done-button}} + + +\end + + + + + + + + + +<$list filter="[all[shadows+tiddlers]tag[$:/tags/KaTeX/Macro]!has[draft.of]sort[caption]]"> + + + + + + + + + + + + + + + + + + + + + + + +
MacroCommandPreview
<$text text={{!!caption}}/><><><> +<> +<> +<$button class="tc-btn-invisible tc-btn-dropdown"> +<$action-deletetiddler $tiddler={{!!title}}/> +{{$:/core/images/delete-button}} + +
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}} +
+
+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/> + + +Import + +<$button> +<$action-setfield $tiddler="$:/temp/katex/import-macro" text=""/> +Clear + + +''Preview'' + +<$list filter="[splitregexpregexp]" variable=line> +<$vars m={{{[search-replace:g:regexp,[$1]]}}} c={{{[search-replace:g:regexp,[$2]]}}}> + + + + + + +
<><><$macrocall $name=katex-escape text="<>"/>
+ + +
+
+
+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);