Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html tags inside mathjax

Tags:

html

mathjax

I already know that mathjax allows very limited html codes inside math areas, however, I have a multiple page document, each having a number of equations, so for \eqref{} to work properly, I have wrapped these commands in a clickable <span> which direct to the page containing the addressed equation, before the page is scrolled to the equation somewhere withing that page.

The relevant part of the markdown application is, first,

content=content
    .replace(/(\\ref\{[^\}]+\})/g, "<span class='eqCitationSpan'>$1</span>")
    .replace(/(\\eqref\{[^\}]+\})/g, "<span class='eqCitationSpan'>$1</span>")

and then,

// to handle cross-referencing the equations in MathJax, due to the multipage nature of the document
var perPageEqs=[];
MathJax.Hub.Queue(
    function () {
        for(pageNum=0; pageNum<totalPageNumber; pageNum++){
            var jax = MathJax.Hub.getAllJax("resultPage-"+pageNum);
            var neWLabelsInPage=[];
            for (var i=0, l=jax.length; i<l; i++) {
                jax[i].originalText.replace(/\\label\{([^\}]+)\}/g, function(x,y){
                    neWLabelsInPage.push(y);
                    return false;
                });
            }
            perPageEqs.push(neWLabelsInPage);
        }


        var eqCitationArray = document.getElementsByClassName("eqCitationSpan");
        var eqsOnWhichPage;
        for(var i=0, l=eqCitationArray.length; i<l; i++) {
            var key=eqCitationArray[i].innerHTML
                .replace(/.*\\ref\{([^\}]+)\}.*/g,"$1").replace(/.*\\eqref\{([^\}]+)\}.*/g,"$1");
            eqsOnWhichPage=perPageEqs.findIndex(function(x) {
                return x.indexOf(key) !== -1;
            });
            eqCitationArray[i].outerHTML=
                "<span class='eqCitationSpan' onclick='currentPage="+eqsOnWhichPage+"; changePage();'>"
                    +eqCitationArray[i].innerHTML
                +"</span>";
        }
    }
);

Everything works well except when I want to refer to an equation from within another equation. In that case, MathJax doesn't compile the equation at all, but only the command \eqref{} inside the equation. I know it is intended to be so, but then any work around for such problems?

like image 512
owari Avatar asked Dec 02 '25 15:12

owari


1 Answers

MathJax will not process math that contains HTML tags (other than a select few), so you will not be able to do the kind of replacements inside an expression like you are attempting to do here.

You can, however, use MathJax itself to handle the cross-page lining that I think you are trying to accomplish. Here's one approach:

<script type="text/x-mathjax-config">
(function () {
  //
  // The values from MathJax.Extensions["TeX/AMSmath"].labels
  //   on all the linked pages.
  //
  var labels = {
    xyz2: {tag: "2", id: "mjx-eqn-xyz2"}
  };
  //
  //  Maps tag IDs to pages on which they occur.
  //
  var urls = {
    "mjx-eqn-xyz2": "link-cross-page2.html"
  };

  //
  //  Add the labels for the other pages when AMSmath is loaded.
  //
  MathJax.Hub.Register.StartupHook("TeX AMSmath Ready", function () {
    var keys = Object.keys(labels);
    for (var i = 0, m = keys.length; i < m; i++) {
      MathJax.Extension["TeX/AMSmath"].labels[keys[i]] = labels[keys[i]];
    }
  });

  //
  //  Configure TeX to use the external page url rather than the base url
  //    if the id is on a different page.
  //
  MathJax.Hub.Config({
    TeX: {
      equationNumbers: {
        formatURL(id, base) {
          return (urls[id] || base) + '#' + id;
        }
      }
    }
  });
})();
</script>

Put this before the script that loads MathJax.js itself. You will need to collect the data from MathJax.Extension["TeX/AMSmath"].labels from all the pages you want to link to and put that in the var labels above (combine the data from all the pages into one object), and create the mapping from ids to page urls in var urls above. In this example, there is \label{xyz2}\tag{2} in the file link-cross-page2.html, and any \ref{xyz2} on the page containing this code will link to link-cross-page2.html#mjx-eqn-2.

The rest of the code simply copies your labels into the AMSmath labels list when it is loaded, and configures TeX's formatURL() function to look up the file in your list and use that if it exists, otherwise it uses the base URL of the page.

Note that this does require that equation numbers are different across the pages, but I assume that is the case for you, otherwise you could not tell which page to link to.

like image 54
Davide Cervone Avatar answered Dec 05 '25 08:12

Davide Cervone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!