Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering MathJax updated with .html()

jsfiddle example: https://jsfiddle.net/3qu846tu/

I'm trying to update MathJax-math by means of .html(), however, it seems my code isn't working. My current code looks somewhat like this, but it outputs "1+2=3" unrendered:

$$\class{x}{2}+\class{y}{2}=\class{z}{5}$$
<script>
$( '.x' ).html( '1' );
$( '.y' ).html( '2' );
$( '.z' ).html( '3' );
MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
</script>

I've tried different commands, but none seems to work. ["Rerender", MathJax.Hub] just renders "2+2=5", so it seems like the .html() is reset:

<script>
MathJax.Hub.Queue(["Rerender",MathJax.Hub]);
</script>

The wanted result would look somewhat like this (js omitted), where \class{x}{} (and others) may appear more than once in different places:

<span>You have chosen \(\class{x}{}\) and \(\class{y}{}\)</span>
$$\class{x}{}+\class{y}{}=\class{z}{}$$

Is there any way of rendering "1+2=3" this way? $( '.x' ) may be changed a number of times, not just once.

like image 662
Frank Vel Avatar asked Aug 03 '16 18:08

Frank Vel


1 Answers

Frank, use the following code:

HTML:

<html>
<head>

<script
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML">
</script>
</head>
<body>

<div id="formula"></div>

A: <input type="text" id="valueA">
B: <input type="text" id="valueB">
C: <input type="text" id="valueC">

<p><input type="button" value="Update" onclick="DynamicMJ.update()" /></p>

<script>
var DynamicMJ = {
  formula: document.getElementById("formula"),

  update: function () {
    var a = document.getElementById("valueA").value;
        b = document.getElementById("valueB").value;
    var c = document.getElementById("valueC").value;
    var tex = "\\frac{"+a+"}{2}+ \\frac{"+b+"}{2} = \\frac{"+c+"}{5}";
    this.formula.innerHTML = "\\["+tex+"\\]";
    MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.formula]);
  }
};
DynamicMJ.update();
</script>

</body>
</html>

EXPLANATION:

You need to use an HTML element(div in this example) in order to write the values, and then you can insert the values of the textboxes directly into the formula.

Hope this helps!

like image 173
Joel Hernandez Avatar answered Sep 19 '22 17:09

Joel Hernandez