Skip to content

Obtaining the original TeX from a rendered page

João Palhoto Matos edited this page Apr 14, 2014 · 3 revisions

Is there any way to reverse the output from the preprocessor and regenerate the original text again once everything's been processed?

I mean in the sense of shutting the MathJax processor "off," restoring the original string of info on the fly, and being able to toggle between MathJax on and off.

Anyone know if this is possible?


I think what the questioner had in mind was to give a toggle on the page that would either show the original TeX code or the typeset code, so that you could switch between the two, rather than simply turning off MathJax entirely. But that is just a guess.

If that is what was requested, there is no direct way to do that, but it could be done with some javascript code. For instance, adding

<script>
function removeTypeset() {
  var HTML = MathJax.HTML, jax = MathJax.Hub.getAllJax();
  for (var i = 0, m = jax.length; i < m; i++) {
    var script = jax[i].SourceElement(), tex = jax[i].originalText;
    if (script.type.match(/display/)) {tex = "\\\\["+tex+"\\\\]"} else {tex = "\\\\("+tex+"\\\\)"}
    jax[i].Remove();
    var preview = script.previousSibling;
    if (preview && preview.className === "MathJax_Preview") {
      preview.parentNode.removeChild(preview);
    }
    preview = HTML.Element("span",{className:"MathJax_Preview"},[tex]);
    script.parentNode.insertBefore(preview,script);
  }
}

function showTypeset(show) {
  MathJax.Hub.Queue(show ? ["Reprocess",MathJax.Hub] : removeTypeset);
}
</script>

<style>
.MathJax_Preview {color:black ! important}
</style>

to the <head> section of your page, and

<label>
  <input type="checkbox" onchange="showTypeset(this.checked)" checked="checked" />
  Show Typeset Mathematics
</label>

to the body of the page will add a checkbox that lets you turn on and off the typeset mathematics. When turned off, the mathematics will show as its original TeX code (with delimiters), and when on as the typeset code.

Hope that does what you need.

Davide

Clone this wiki locally