Search code examples
mathjax

How to apply MathJax LaTeX formatting to <div> programatically?


For example, if I add a <div> to a page via (assume jQuery is included)

$('body').append($('<div>').text('x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.'));

How can I apply the "tex2jax" formatting to that <div>? I don't want MathJax to automatically run on any of my text; I want to apply it manually whenever I add an element to the page.


Solution

  • Add script tag instead of div, then schedule reprocessing of the page:

    $('body').append($('<script type="math/tex">').text('x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.'));
    MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
    

    Fiddle