Search code examples
mathjax

Does Mathjax substitutes double backslash outside of math environment?


Mathjax seems to substitute double backslash with single backslash even outside of math environment.

Sample:

<!DOCTYPE html> 
<html>
<head> 
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Mathjax</title>
<script type="text/javascript" id="MathJax-script" async
  src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
</script>
</head>

<body>

    \\

</body>
</html>

The resulting webpage displays one backslash. Remove the Mathjax script and the webpage displays two backslash as expected.

This quirk interferes with writing TeX code verbatim on the webpage. Is there a way to stop the behavior?


Solution

  • This issue is described in https://github.com/mathjax/MathJax/issues/2532, which provides the option processEscapes: false as a solution. That Github issue contains the following explanation:

    This is part of the action taken when the TeX option processEscapes is true (the default). That option controls whether \ can be used to escape dollar signs (e.g., \$) rather than using them as inline math delimiters. When this is true, \\ is used to escape \ as well (so that you can enter a literal \$ as \\\$).

    Code:

    <!DOCTYPE html> 
    <html>
    <head> 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>Mathjax</title>
    <script>
    MathJax = {
       tex: {
          inlineMath: [ ['$','$'], ['\\(','\\)'] ],
          processEscapes: false
       }
    };
    </script>
    <script type="text/javascript" id="MathJax-script" async
      src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
    </script>
    </head>
    
    <body>
    \\
    Inline: $\fbox{I'm in a box}$. Equation:
    $$x = \frac{3}{2}$$
    </body>
    </html>
    

    Result:

    enter image description here

    Note that this means that you cannot use a literal dollar sign in your html code anymore, however within a math expression you can still use \$ to render a dollar sign. Also when you remove ['$','$'] from the inline math options you can use $ and \$ in the html without problems.