I wanted to be able to use fragments (with specific indices) inside MathJax contents.
I added the following lines to plugin/math/math.js:
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
var TEX = MathJax.InputJax.TeX;
TEX.Definitions.Add({macros: {'fragment': 'FRAGMENT_INDEX_attribute'}});
TEX.Parse.Augment({
FRAGMENT_INDEX_attribute: function (name) {
var index = this.GetArgument(name);
var arg = this.ParseArg(name);
this.Push(arg.With({
'class': 'fragment',
attrNames: ['data-fragment-index'],
attr: {'data-fragment-index':index}
}));
}
});
});
So now I can type in equations like \[ x \fragment{2}{+ y} \fragment{1}{= z} \], where "x" appears first, followed by "x = z" and then "x + y = z".
Would it make sense to include something like this in math.js?
@hakimel opinions? This seems fine to me.
Looks like a useful addition. Happy to add this but we'll need to mention it in the readme too.
Note: with this solution, the fragment indices are relative to the slide's current fragment index. This can be tested with this:
<section>
<ul>
<li>Dummy text</li>
<li class="fragment" data-fragment-index="1">Some math:
$$
\begin{align*}
\fragment{1}{x_1} \\
\fragment{3}{x_3} \\
\fragment{4}{x_4} \\
\end{align*}
$$
</li>
<li class="fragment" data-fragment-index="2">x_1</li>
<li class="fragment" data-fragment-index="3">nop</li>
<li class="fragment" data-fragment-index="4">x_3</li>
<li class="fragment" data-fragment-index="5">x_4</li>
</ul>
</section>
useful addition, thanks @mungerd!
It's not only that the indices are relative like @bchretien said: I think the fragment-parsing code renumbers the fragment indices before the math is parsed. So this doesn't work as expected:
<section>
<ul>
<li>Dummy text</li>
<li class="fragment" data-fragment-index="1">Some math:
$$
\begin{align*}
\fragment{1}{x_1} \\
\fragment{3}{x_3} \\
\fragment{4}{x_4} \\
\end{align*}
$$
</li>
<li class="fragment" data-fragment-index="5">wanted x_4, actually x_1</li>
</ul>
</section>
I worked around it by adding some empty spans with matching fragment indices, but it'd be nice to fix that for real somehow.
I've recently started pre-rendering my mathjax into SVG with mathjax-node. This avoids occasional layout problems with Reveal, reduces the client-side load of rendering, and as a happy side effect fixes the issue with fragment indices I mentioned above (since the data-fragment-index attributes are already present the first time Reveal looks at them).
If others are curious how to set this up, here is my current base code for a talk, using grunt. (I might switch to webpack or something in the future.)

how to fix responsive dimensions for mobile devices?
what is the current state of this - is it currently possible to fragment equations?
what is the current state of this - is it currently possible to fragment equations?
I am interested as well
@dakling @cxkoda you can add fragments to LaTeX equations by tweaking the math.js plugin.
See this version and demo
@gcalmettes thank you very much for sharing. I checked it out some hours ago and it looks quite nice. however, from time to time the equations are not rendered correctly and i have to manually refresh the page to trigger the process again. are you experiencing this too? should we move this discussion over to your repo?
@cxkoda I never had a problem with this. Note however that I usually use reveal.js and this modified math.js version in a framework compiled using webpack (see this repo), so that could explain why I never had a problem. I only made the standalone example to show you, so I can certainly check if that make the behavior unstable.
I took the old code from @gcalmettes (for Mathjax V2, as I need automatic linebreaks) and modified plugin.js to use the new Plug-In API of Reveal 4+
The file and the compiled math.js, math.esm.js are attached.
Place them into the plugin/math folder and the commands provided by @gcalmettes should work:
\texclass{class_name}{element}
\fragment{index}{element}
\fragapply{any_class optional_index}{element}
Most helpful comment
@dakling @cxkoda you can add fragments to LaTeX equations by tweaking the
math.jsplugin.See this version and demo