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

Fragments in MathJax #1365

Open
mungerd opened this issue Sep 21, 2015 · 14 comments
Open

Fragments in MathJax #1365

mungerd opened this issue Sep 21, 2015 · 14 comments

Comments

@mungerd
Copy link

mungerd commented Sep 21, 2015

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?

@mungerd mungerd changed the title Framents in MathJax Fragments in MathJax Sep 23, 2015
@bnjmnt4n
Copy link
Collaborator

bnjmnt4n commented Mar 8, 2016

@hakimel opinions? This seems fine to me.

@hakimel
Copy link
Owner

hakimel commented Mar 9, 2016

Looks like a useful addition. Happy to add this but we'll need to mention it in the readme too.

@bchretien
Copy link
Contributor

bchretien commented Jun 29, 2016

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>

@gcalmettes
Copy link

useful addition, thanks @mungerd!

@djsutherland
Copy link
Contributor

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.

@djsutherland
Copy link
Contributor

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.)

@G1305
Copy link

G1305 commented Dec 16, 2017

image

how to fix responsive dimensions for mobile devices?

@dakling
Copy link

dakling commented May 19, 2019

what is the current state of this - is it currently possible to fragment equations?

@cxkoda
Copy link

cxkoda commented Sep 20, 2019

what is the current state of this - is it currently possible to fragment equations?

I am interested as well

@gcalmettes
Copy link

@dakling @cxkoda you can add fragments to LaTeX equations by tweaking the math.js plugin.
See this version and demo

@cxkoda
Copy link

cxkoda commented Sep 25, 2019

@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?

@gcalmettes
Copy link

@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.

@manuelbb-upb
Copy link

manuelbb-upb commented Nov 16, 2020

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}

@manuelbb-upb
Copy link

The issue might resolve itself soon.
With MathJax3, there already is the "html" extension, allowing to decorate LaTeX expressions with HTML classes, e.g., to create fragments with a^2 + \class{fragment}{b^2} = \class{fragment}{c^2}.
MathJax4 will have an additional \data command, that can additionally be used to set a "data-fragment-index" attribute: \class{fragment}{\data{fragment-index=1}{a}}.

This can be tested now already:

Reveal.initialize({
 hash: true,
 plugins: [ ..., RevealMath.MathJax3 ],
 mathjax3: {
  mathjax: "https://cdn.jsdelivr.net/npm/mathjax@4.0.0-alpha.1/es5/tex-mml-chtml.js",
  loader: {load: ['[tex]/html']},
  tex: {packages: {'[+]': ['html']},
 }
});
...

I also recommend setting sortFragmentsOnSync: false. It appears that else fragment indices get mixed up when changing slides. @hakimel probably can give hints to sort this issue out. My guess: MathJax.typeset() gets called on every slide change, triggering a sync (?). (Is retriggering typesetting still necessary with MathJax3+?) With sortFragmentsOnSync: false the data-fragment-indices also are normalized and sorted when slide is called, but this appears to happen after MathJax has completed initial typesetting.

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

No branches or pull requests

10 participants