Search code examples
htmlmathjax

Retrieve original Tex command


I have access to an old private website, as a client.

The original TeX command is not present anywhere in the page_source nor the rendered document. However, it is clearly somewhere in memory, because I can right click on the math expression, select "Show Math as TeX command", and see the original TeX command.

How can I retrieve the original command in javascript, from the browser console?

I tried to do:

window.MathJax.startup.document.getMathItemsWithin(document.body);

and then access the math element, but this shows me the MathML code (which happens to be Math input error, and not the original TeX command (the malformed \( € 0.7 (\ ).


Solution

  • You have the right approach, and the command you have given will return an array of internal structures that contain not just the original TeX, but other data as well. To get the TeX portion, use

    const list = MathJax.startup.document.getMathItemsWithin(document.body);
    for (const item of list) {
      console.log(item.math);
    }
    

    That should get you all the TeX in the page.


    EDIT: Here is an example you can run to see that it works.

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MathJax Test</title>
    <script>
    MathJax = {
      startup: {
        pageReady() {
          return MathJax.startup.defaultPageReady().then(() => {
            const list = MathJax.startup.document.getMathItemsWithin(document.body);
            for (const item of list) {
              alert(item.math);
            }
          });
        }
      }
    };
    </script>
    <script type="text/javascript" defer="" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    </head>
    <body>
        <p>The price: \( €0.7 \)</p>
    </body>
    </html>