1

I'm using a Javascript library called MathJax which allows you to use LaTex and other mathematic markup languages like it in HTML. I'm trying to use it in Javascript but the regular expressions are getting in the way. Here is my code:

function sendTex()
{
    var latexStuff = $("#texField").val();
    var latexString = "When $a \ne 0$, there are two solutions to \*(ax^2 + bx + c = 0\*)       and they are
    $$x = {-b \pm \qrt{b^2-4ac} \over 2a}.$$";
    $("#texVersion").append(latexString);
}

1 Answer 1

1

Once you add new mathematics to the page, you need to tell MathJax to process that new math. See the MathJax documentation for details. The main idea is to use

MathJax.Hub.Queue(["Typeset",MathJax.Hub]);

when you want MathJax to process new math on the page. In your case, you might try

function sendTex()
{
  var latexStuff = $("#texField").val();
  var latexString = "When $a \\ne 0$, there are two solutions to \\(ax^2 + bx + c = 0\\)" +
    " and they are $$x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.$$";
  $("#texVersion").append(latexString);
  MathJax.Hub.Queue(["Typeset",MathJax.Hub,"texVersion"]);
}

and see if that works better for you. Note that you need to double your backslashes, since in JavaScript strings, a backslash is a special character, so to get a literal backslash you need to use two of them. You also had \qrt rather than \sqrt, and \*(...\*) rather than the usual \(...\) (but perhaps you have configured MathJax to use alternative delimiters).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.