mathjax/MathJax-demos-node

Unable to add contextual menus when using component/tex2svg-page

Opened this issue · 3 comments

This is more of a question than a bug, but I am trying to add contextual menus when using component/tex2svg-page, but it is not working. From what I can tell, it should be using the combined component, tex-svg.js and this should "just work", but I don't see the contextual menu. I've tried manually loading ui/menu and setting enableMenu: true, as per the docs:

https://docs.mathjax.org/en/latest/web/components/misc.html#menu-component

diff --git a/component/tex2svg-page b/component/tex2svg-page
index ef17c7d..1667847 100755
--- a/component/tex2svg-page
+++ b/component/tex2svg-page
@@ -68,14 +68,18 @@ const htmlfile = require('fs').readFileSync(argv._[0], 'utf8');
 // Configure MathJax
 //
 MathJax = {
+    options: {
+        enableMenu: true
+    },
     loader: {
         paths: {mathjax: 'mathjax-full/es5'},
         source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source),
         require: require,
-        load: ['adaptors/liteDOM']
+        load: ['adaptors/liteDOM', 'ui/menu']
     },
     tex: {
-        packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/)
+        packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/),
+        inlineMath: [['\\$', '\\$'], ['\\(', '\\)']]
     },
     svg: {
         fontCache: argv.fontCache,

But then I get:

> node ./tex2svg-page --dist /tmp/mathjax.html > /tmp/mathjax-svg.html
MathJax: Invalid option "enableMenu" (no default value).
dpvc commented

I'm sorry to have to tell you that you will not be successful at adding the menu to a pre-processed page, for several reasons.

First, the menu is attached to the math via javascript calls to addEventListener(), and that is something that can't be recorded in a serialized DOM (without having additional javascript that would do it after the page is loaded in a browser). Although there are oncontextmenu and other similar attributes, MathJax sometimes uses multiple listeners for the same event, and that would be harder to handle through those mechanisms.

Second, even if you used oncontextmenu and so on, there would have to be javascript code for those event handlers to call, so you would either have to load that or include it in each page. That would have to be a stand-alone menu system that doesn't rely on the rest of MathJax, and there isn't currently such a package. This would be a useful thing to have, but it doesn't currently exist, and it is not the highest thing on our to-do list.

Third, many of the items in the menu rely on data that is not in the page when you pre-process it with tex2svg-page. For example, the "Show Math As" and "Copy to Clipboard" menus, most of the the "Math Settings" items, like the ability to change renderers, and the "Accessibility" menu items all rely on having access to the MathJax internal format for the expressions, which is not part of the DOM, and so not available in the output of tex2svg-page.

So for now, you can't get the MathJax menu in pre-processed pages.

Thank for the confirmation. That's an interesting tradeoff of pre-rendering. Feel free to close this, if this is something that is likely to never happen.

dpvc commented

A couple years ago, I did some preliminary work on this, so it is somewhat that certainly could be done. I'll leave the issue open to remind me to get back to it in the future.