Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MathJax 3.x compatibility issue #2559

Closed
wirepatch opened this issue Jan 4, 2020 · 36 comments
Closed

MathJax 3.x compatibility issue #2559

wirepatch opened this issue Jan 4, 2020 · 36 comments

Comments

@wirepatch
Copy link

wirepatch commented Jan 4, 2020

MathJax 2.x uses URL configuration parameters e.g.:

https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_CHTML

In current 3.x this dispatch mechanism has been superseeded using e.g.:

https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js

MathJax.js is no longer present in 3.x. This works using the reveal MathJax plugin e.g.:

math: {
    mathjax: 'mathjax/tex-mml-svg.js',
    config: 'x' // Obsolete dummy value.
}

However using »Inspect« Chromium shows the following JavaScript error:

math.js:71 Uncaught TypeError: Cannot read property 'Config' of undefined
    at math.js:71
    at HTMLScriptElement.finish (math.js:43)

Omitting config: 'x' completely does not help either.

Technically it's a mere nuisance. But if you are chasing for errors this one is confusing. Making »config« an option rather than a mandatory property would help.

@burgerga
Copy link

burgerga commented May 15, 2020

MathJax 3 support would be nice, but it would require almost a complete rewrite of https://github.com/hakimel/reveal.js/blob/master/plugin/math/math.js

(based on your comment I hoped there would be an easy workaround, but unfortunately not 😞)

Trying to use Mathjax 3 (also with the config: 'x' trick) breaks most (but not all) of my equations.
The reason is that Mathjax v3 is loaded and configured differently than v2 (compare http://docs.mathjax.org/en/v2.7-latest/configuration.html and http://docs.mathjax.org/en/latest/web/start.html), that is why you get this

math.js:71 Uncaught TypeError: Cannot read property 'Config' of undefined
    at math.js:71
    at HTMLScriptElement.finish (math.js:43)

error, which means that most of the plugin is not working (i.e. none of the options are applied and the reveal event listener is not added). Additionally, v3 doesn't use queues so that part needs to be rewritten as well.

EDIT: rewrote the plugin, will prepare a pull request, but I'll also paste it below tomorrow

@burgerga
Copy link

Instead of overwriting the current plugin, I made a new one, because at the moment there are still some issues with MathJax 3 (I encountered newlines not working and under/over braces not working), so if you want to change back to MathJax 2 just enable the other plugin again.

Save the following as plugins/math3/math3.js, then change your index.html to load this plugin. You need math3 in your options to customize it.

/**
 * A plugin which enables rendering of math equations inside
 * of reveal.js slides. Essentially a thin wrapper for MathJax 3
 *
 * @author Hakim El Hattab
 * @author Gerhard Burger
 */
var RevealMath3 = window.RevealMath3 || (function(){

	var options = Reveal.getConfig().math3 || {};
	var mathjax = options.mathjax || 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js';
	var url = mathjax;

	var defaultOptions = {
		tex: {
			inlineMath: [ [ '$', '$' ], [ '\\(', '\\)' ]  ]
		},
		options: {
			skipHtmlTags: [ 'script', 'noscript', 'style', 'textarea', 'pre' ]
		},
		startup: {
			ready: () => {
      			MathJax.startup.defaultReady();
      			MathJax.startup.promise.then(() => {
      				Reveal.layout();
      			});
    		}
		}
	};

	function defaults( options, defaultOptions ) {

		for ( var i in defaultOptions ) {
			if ( !options.hasOwnProperty( i ) ) {
				options[i] = defaultOptions[i];
			}
		}

	}

	function loadScript( url, callback ) {

		var script = document.createElement( 'script' );
		script.type = "text/javascript"
		script.id = "MathJax-script"
		script.src = url;
		script.async = true

		// Wrapper for callback to make sure it only fires once
		var finish = function() {
			if( typeof callback === 'function' ) {
				callback.call();
				callback = null;
			}
		}
		script.onload = finish;

		document.head.appendChild( script );


	}

	return {
		init: function() {

			defaults( options, defaultOptions );
			defaults( options.tex, defaultOptions.tex );
			defaults( options.options, defaultOptions.options);
			defaults( options.startup, defaultOptions.startup );
			options.mathjax = null;
			window.MathJax = options;

			loadScript( url, function() {
				// Reprocess equations in slides when they turn visible
				Reveal.addEventListener( 'slidechanged', function( event ) {
					MathJax.typeset();
				} );
			} );

		}
	}

})();

Reveal.registerPlugin( 'math3', RevealMath3 );

@wirepatch
Copy link
Author

wirepatch commented May 16, 2020 via email

@burgerga
Copy link

@hakimel I was planning to prepare a pull request, but I see that you want plugins in a separate repository. Is it ok if I create a separate repository for this or do you want to update the math plugin in this repository?

From https://docs.mathjax.org/en/latest/upgrading/v2.html:

MathJax v3 is a complete rewrite of MathJax from the ground up (see What’s New in MathJax v3.0), and so its internal structure is quite different from that of version 2. That means MathJax v3 is not a drop-in replacement for MathJax v2, and upgrading to version 3 takes some adjustment to your web pages.

and

MathJax v3 is still a work in progress; not all features of version 2 have been converted to version 3 yet, and some may not be. MathJax v2 will continue to be maintained as we work to move more features into version 3, but MathJax v2 likely will not see much further development, just maintenance, once MathJax v3 is fully converted.

So it might be smart to keep the plugins separate for a while?

@hakimel
Copy link
Owner

hakimel commented May 18, 2020

I think it's best to create a new plugin in a separate repo. Thanks for asking.

On a related note I've been considering switching the math plugin over to KaTeX instead. From a developer point of view, I've found that easier to work with (we use it in slides.com). Mostly an FYI but if anyone has input I'm all ears :)

@burgerga
Copy link

Thanks for your quick reply, will create a separate repo then.
Never used KaTeX, but I'll have a look! (saw that there was already a plugin: https://github.com/j13z/reveal.js-math-katex-plugin/, but it hasn't been updated in 3 years)

@burgerga
Copy link

burgerga commented May 18, 2020

@hakimel Experimented a bit with KaTeX and it's quite easy to get it to work and quite fast indeed, thanks for the tip! So I was thinking: is it maybe an idea to create a reveal.js-math repo? Then you can have MathJax 2, MathJax 3, and KaTeX side-by-side, and have the documentation for general math stuff still in one place (for example, to point out differences between the three, and where you can explain the need for event bindings to the Reveal 'ready' event).

@hakimel
Copy link
Owner

hakimel commented May 20, 2020

Still thinking about the best way to go about this. Maintaining three separate plugins seems like extra work. If KaTeX has the features people need, then I'd rather just switch to a single 'math' plugin powered by KaTeX.

The existing KaTeX plugin hasn't been updated in a while but maybe we can use that as a starting point: https://github.com/j13z/reveal.js-math-katex-plugin/

@burgerga
Copy link

burgerga commented May 20, 2020

Yes, I see your point... Maybe I'll dump the MathJax 3 plugin code somewhere "as-is".
I browsed through this existing KaTeX plugin, but it seems unnecessarily complex (but maybe that was needed at the time, I don't know), I propose a much simpler plugin using the katex autorenderer script, what do you think?

EDIT: outdated code removed, see pull request below

@hakimel
Copy link
Owner

hakimel commented May 20, 2020

Thanks for sharing @burgerga!

I've added switching to KaTeX to the v4.1 todo-list so it's super helpful to see an implementation of it.

@burgerga
Copy link

burgerga commented May 20, 2020 via email

@hakimel
Copy link
Owner

hakimel commented May 20, 2020

Yes please, I'd love a pull request that replaces the existing math plugin with your KaTeX version.

Once that's in, I'll move the old math plugin to a separate repo so that remains available for people who want to keep using MatJax 2.

@rodrigoalcarazdelaosa
Copy link

Just to leave my two cents here. I'm currently using the Academic theme for Hugo, which uses reveal.js for its slides feature.

Academic uses MathJax v3 with a feature that allows me to use some commands only available through extensions (such as \cancel, which implements some of the features of the cancel package of LaTeX), without having to load anything myself (I guess it has to do with the autoload extension, but haven't figured it out yet).

I noticed that the \cancel command does not work with reveal.js, which is something I'm really missing (I'm planning on using slides for guiding my students through different processes, and cancelling numbers and/or units may be useful).

I don't know if KaTeX has something similar (never used it myself) but I'd be willing to have that feature.

Thanks in advance!

@rodrigoalcarazdelaosa
Copy link

Academic uses MathJax v3 with a feature that allows me to use some commands only available through extensions (such as \cancel, which implements some of the features of the cancel package of LaTeX), without having to load anything myself (I guess it has to do with the autoload extension, but haven't figured it out yet).

I forgot to mention mhchem as well, to write chemical equations, but I see it also has a version for KaTeX 😉.

@burgerga
Copy link

I don't have any experience with Hugo, but mhchem and cancel, should work with MathJax 3 and KaTeX.

mhchem might be a bit harder to get to work with the Reveal math (katex) plugin since it needs te be loaded after katex.js. I will have a look if this is easy to add.
For the time being it is also possible to skip the Reveal math plugin, and load katex using the instructions from https://katex.org/docs/autorender.html#usage, and then add in the mhchem script in the appropriate place.

@burgerga
Copy link

@rodrigoalcarazdelaosa Just updated the pull request, the plugin now allows easy enabling of mhchem!

@royvelich
Copy link

For the newbies among us, can anyone explain how can I use MathJax 3 with Reveal.js? Maybe a short standalone demo?

@burgerga
Copy link

burgerga commented Apr 30, 2021

@xenomarz
https://github.com/burgerga/reveal.js/tree/feat_mathjax3 has Mathjax3 and is minimally changed from the current revealjs release (master...burgerga:feat_mathjax3)
If you pull it, and run it you will see the Lorentz equations.

You can specify options using math3 (instead of math, cf https://revealjs.com/math/), and the most important option is the mathjax url, since in mathjax 3 the config is included in the url. The url I have as default is https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js but, for example, you could also specify

math3: {
  mathjax: "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
}

Other options (see http://docs.mathjax.org/en/latest/options/index.html) can also be specified, currently only

math3: {
  tex: {
    inlineMath: [ [ '$', '$' ], [ '\\(', '\\)' ] ]
  }
}

is used by default.

@edemaine
Copy link

KaTeX developer here, finally taking a look at reveal.js for an upcoming presentation (great stuff!). Let me know if I can help with KaTeX integration.

@rodrigoalcarazdelaosa KaTeX supports \cancel. See https://katex.org/docs/supported.html . There might be specific features in MathJax or its extensions that aren't supported in KaTeX, though, so it could be worth keeping around at least an old MathJax plugin.

@AhmedThahir66
Copy link

Any updates on this?

@edemaine
Copy link

edemaine commented Oct 1, 2021

If you'd like an example of how to use KaTeX with RevealJS (which is very easy, no plugin required), see this template or one of these examples using it: example, example.

I think it would be nicer to include direct support for KaTeX in the RevealMath plugin, or perhaps a separate plugin. This would enable rendering math only slide by slide like with MathJax, which could be a bit faster for decks with many many slides. Oh, I just saw that PR #2677 is one approach to this. If there's more work to be done, I'm willing to help.

@burgerga Maybe you want to turn https://github.com/burgerga/reveal.js/tree/feat_mathjax3 into a PR as well? I agree that offering both MathJax and KaTeX as options would be nice; I'm just not sure whether it should be one plugin or two (or three if we want legacy MathJax v2 support).

@burgerga
Copy link

burgerga commented Oct 3, 2021

@edemaine Since that PR is about 1.5 years old I pulled the trigger and created https://github.com/burgerga/reveal.js-math.
I didn't want to figure out all the rollup/babel stuff, so it probably won't work in older browsers, but if some of you can test this out that would be great!

@hakimel
Copy link
Owner

hakimel commented Oct 28, 2021

@burgerga I really like how you set up reveal.js-math to offer all three options. Would you mind if I copy that into reveal.js core and replace the current math plugin? If so I will also alias Reveal.initialize({mathjax2: ...}) to Reveal.initialize({math: ...}) for backwards compatibility. We make sure all three options are well documented at https://revealjs.com/math/.

@burgerga
Copy link

@hakimel Thanks, and yes go ahead! I think that would be convenient for many people :)

@hakimel
Copy link
Owner

hakimel commented Oct 28, 2021

Thanks @burgerga!

This has been added now. I made the API fully backwards compatible so all existing presentations can continue to initialize with:

Reveal.initialize({
  math: {
    config: 'TeX-AMS_HTML-full'
  },
  plugins: [ RevealMath ]
});

Beyond that everything works as described in the README here: https://github.com/burgerga/reveal.js-math

This needs to be added to the revealjs.com/math docs as well but other than that it should be good to go.

@hakimel hakimel closed this as completed Oct 28, 2021
@burgerga
Copy link

No problem, when you add the docs could you make it a co-authored commit, then I can share in the glory of being a reveal contributor :) When the docs are done, I will delete reveal.js-math, so it doesn't linger around and cause confusion.

@hakimel
Copy link
Owner

hakimel commented Oct 28, 2021

I wasn't aware co-authored commits were a thing, sorry! I only tagged your username in the commit message for the changes I copied over. I'm guessing there's no way to amend that now?

Will make sure to add you as a co-author for the docs update.

@burgerga
Copy link

Probably not possible to amend, but that's fine, no need to worry :)

@hakimel
Copy link
Owner

hakimel commented Nov 10, 2021

The docs have been updated: https://618b8dc985f599ad10a72ac4--revealjs.netlify.app/math/. This is only available in a branch so far but will go live with reveal.js 4.2.0.

The co-authored commit is here: reveal/revealjs.com@be3046e

@burgerga
Copy link

Awesome, and thanks! I have already archived the reveal.js-math repo, and put a message in the description that this is now part of reveal

@DOGCOINKILLER88
Copy link

@api docker Engine

@DOGCOINKILLER88
Copy link

Docker phiên bản 1.4

@DOGCOINKILLER88
Copy link

DSL và Gem

@DOGCOINKILLER88
Copy link

Dockly (giảm khó khăn đóng gói ứng dụng)

@DOGCOINKILLER88
Copy link

Định dạng:test_Docker làm # mã ở dãy cuối

@DOGCOINKILLER88
Copy link

Docker:test_docker lam kho ‘an-awesome-name’cuối

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants