2

Does any one know how to force browser to display MathML code instead of equation?

PS: Rendering MathML to view as plain text gives the TeX output.

For example,

The axis on which the point (0,4) lie, is _____

Should be displayed as:

The axis on which the point <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mn>0</mn><mo>,</mo><mn>4</mn><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(0, 4)</annotation></semantics></math> lie, is _____

Lee Mac
  • 15,615
  • 6
  • 32
  • 80
  • 1
    Possible duplicate of [Display HTML code in HTML](https://stackoverflow.com/questions/2820453/display-html-code-in-html) – scraaappy Apr 08 '18 at 09:59
  • @scraaappy, I don't think this is a duplicate of that question. I think the OP is asking how to get MathJax to convert the TeX to MathML and display the MathML code rather than the typeset version (whereas the question you point to is about how to display HTML special characters in general). That would only apply if you already had the MathML to start with. The real question here is how to get MathJax to do it for you. – Davide Cervone Apr 09 '18 at 10:48
  • yes exactly.... I would want to copy paste the content in word and then I can write a word VBA code to find text between and and convert to equations. Automatic conversion of TEX is difficult rather impossible – Insight Academy Private Tutori Apr 09 '18 at 14:33
  • @DavideCervone you're right, I flag a little quickly, this is only a part of the answer. – scraaappy Apr 09 '18 at 19:38

1 Answers1

2

In most common configs, if your ouput is not directly mathML, mathjax stores mathml informations in the attribute data-mathml of a span tag wich wraps the mathJax element

This is what is displayed in the popup when you right click on a mathJax element : show math as -> MathMl Code

If your goal is to grab equations from html in mathml format, you can create a script which parse your document and get all data-mathml attributes. There is many ways to achieve that, this is just an example you may have to adapt:

function grabMathMl(){ 
    var spanMathMl = document.querySelectorAll(".MathJax");

    let results = [];
    let i = 0, ln = spanMathMl.length;
    for ( i; i < ln; ++i){
        if ( spanMathMl[i].hasAttribute("data-mathml") ){
           results.push(spanMathMl[i].dataset.mathml);
            // if you really want to replace content
            spanMathMl[i].innerHTML = "<textarea>"+spanMathMl[i].dataset.mathml+"</textarea>";
      }
    }
    return results;
}

// put this fonction in the mathJax queue for you have to wait until mathJax process is done
MathJax.Hub.Queue(function(){
    let equations = grabMathMl();
    //console.log (equations.toString());// your equations in mathml
});
<script>
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

<div>$$\left|\int_a^b fg\right| \leq \left(\int_a^b
 f^2\right)^{1/2}\left(\int_a^b g^2\right)^{1/2}.$$</div>

<div>
\begin{equation} x+1\over\sqrt{1-x^2} \end{equation}
</div>

Then in word, this link should interest you https://superuser.com/questions/340650/type-math-formulas-in-microsoft-word-the-latex-way#802093

scraaappy
  • 2,830
  • 2
  • 19
  • 29
  • Thank you so much for the guidance, I am trying to understand this. I am newbie to JavaScript. I googled for some suggestions and found that i can use something like JavaScript Injection. But i m afraid, it did not work for me! In one instance, the page on which i wanted to run the script (from the URL: https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML) made the pagfe frooze! Please guide me further as LayMan! – Insight Academy Private Tutori Apr 10 '18 at 05:29
  • @InsightAcademyPrivateTutori, post a full example of your code by editing your question if you want further help, and try to describe the context and the process you want to achieve more clearly. The better you identify the error, the clearer is the question. Is there any message in the console (in most modern browsers, hit 'F12' to inspect element, and click on the tab console)? – scraaappy Apr 10 '18 at 08:56
  • @ scraaappy-Ok, so here it is.... I want to scrape a website which has maths content, but displayed in math typeset. The I want to convert all the maths content to be displayed in mathml code. So basically i want when my Browser (Google Chrome) opens the website, it should convert the maths typeset to mathml code – Insight Academy Private Tutori Apr 10 '18 at 09:28
  • @ scraaappy - can u provide me with the javascript code to do so as i have got a chrome extension that will execute a javascript everytime the page loads – Insight Academy Private Tutori Apr 10 '18 at 10:12
  • @InsightAcademyPrivateTutori can you provide the link please? – scraaappy Apr 10 '18 at 10:28
  • @InsightAcademyPrivateTutori I can't delete your comment ! – scraaappy Apr 10 '18 at 11:29